Equation of a Line



Finding the equation of a Line Given Two End Points



Write a program for finding the equation of a Line Given Two End Points (x1,y1) and (x2, y2)
The equation for a line is Y = MX + C
Where M = Slope of a Line and C = Intercept
To Find the slope of a line
slope = (y2 - y1) / (x2 - x1)
To Find the intercept of the line, intercept = y1 - (slope) * x1
which would be same as, intercept = y2 - (slope) * x2

Input/Output:

Program to find the equation of a line given two end points
Enter X1: 2
Enter Y1: 3
Enter X2: 5
Enter Y2: 7
Equation of the line with end points (2, 3 and (5, 7) : Y = 1.33333X +0.333333

Find 1st three (3) Armstrong Number



Write a program to find 1st three (3) Armstrong Number starting from 1

Hints:

The condition for Armstrong number is,
Sum of the cubes of its digits must equal to the number itself.

For example, 407 is given as input.
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.

Program to built the pattern



Write a program to built the below pattern.

******
*****
****
***
**
*
**
***
****
*****
******


1
12
123
1234
12345
123456
12345
1234
123
12
1

Print 1st 30 Perfect numbers starting from 0 and calculate sum



Write a C program to Print 1st 30 Perfect numbers starting from 0 and calculate sum.
Hints:
Today the usual definition of a perfect number is in terms of its divisors, but early definitions were in terms of the 'aliquot parts' of a number. An aliquot part of a number is a proper quotient of the number. So for example the aliquot parts of 10 are 1, 2 and 5. These occur since 1 = 10/10, 2 = 10/5, and 5 = 10/2. Note that 10 is not an aliquot part of 10 since it is not a proper quotient, i.e. a quotient different from the number itself. A perfect number is defined to be one which is equal to the sum of its aliquot parts.
The four perfect numbers 6, 28, 496 and 8128 seem to have been known from ancient times and there is no record of these discoveries.
6 = 1 + 2 + 3,
28 = 1 + 2 + 4 + 7 + 14,
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
If as many numbers as we please beginning from a unit be set out continuously in double proportion, until the sum of all becomes a prime, and if the sum multiplied into the last make some number, the product will be perfect.
Here 'double proportion' means that each number of the sequence is twice the preceding number. To illustrate this Proposition consider 1 + 2 + 4 = 7 which is prime. Then
(the sum) × (the last) = 7 × 4 = 28,
which is a perfect number. As a second example, 1 + 2 + 4 + 8 + 16 = 31 which is prime. Then 31 × 16 = 496 which is a perfect number.
Now Euclid gives a rigorous proof of the Proposition and we have the first significant result on perfect numbers. We can restate the Proposition in a slightly more modern form by using the fact, known to the Pythagoreans, that
1 + 2 + 4 + ... + 2k-1 = 2k - 1.
The Proposition now reads:-
If, for some k > 1, 2k - 1 is prime then 2k-1(2k - 1) is a perfect number.

Source Code:

#include<stdio.h>
using namespace std;
int j,i;
int perfect()
{
      int sum=0,count=0,sum1=0;
      for( j=1; j <= 50000; j++ )
           {
                 i =1;
                 sum = 0;
                 while ( i < j )
                      {
                            if (  j%i = =0 )
                            sum = sum+i ;
                            i++;
                       }
                if( sum = = j )
                      {
                            cout << j << endl;
                            sum1 += j ;
                       }
           }
   cout << "The sum of the first 4 perfect number is: ";
   cout << endl;
   return sum1;
}
 
int main( )
{
      cout << "First 30 perfect number is:"<< endl;
      cout << perfect( );
      return 0;
}

Check whether the sum of all printed prime numbers are equal to the given input or not?



Write a program to get a range from user and give a list of prime numbers up to that number.
And check whether the sum of all printed prime numbers are equal to the given input or not?

Sample Input: 10

Sample Output:
Prime list: 1, 2, 3, 5, 7
The sum is: 18
The sum is not equal to 10

Hints:
A Prime Number can be divided evenly only by 1, or itself.
Example: 5 can only be divided evenly by 1 or 5, so it is a prime number.
But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a composite number).
 



Source Code:

#include<stdio.h>
int main()
{
     int i, num, p, n, sum=0 ;
     printf ("\n Enter the range :");
     scanf ( "%d", &n);
     printf ("\n Prime list : ");
     for(num =1; num <=n; num++)
     {
           p =1;
               for(i=2; i<= num/2; i++)
                   {
                        if ( num % i= =0)
                            {
                                  p = 0;
                                  break;
                            }
                  }
           if ( p = =1)
                 {
                       printf (" %d ",num);
                       sum = sum+num;
                 }
      }
      printf("\nThe sum of prime number is: %d ",sum);
     
      if( sum = = n)
           {
                 printf ("\nThe sum of prime number is equal to %d",n);
           }
      else
           {
                 printf ("\nThe sum of prime number is not equal to %d ",n);
           }
}