Roots of Quadratic Equation: C

Quadratic Equations are of the form ax2 + bx + c = 0. To find roots(root1 and root2) of such an equation, we need to use the formula

quadratic-equation


Find Roots of Quadratic Equation: C Program



YouTube Link: https://www.youtube.com/watch?v=zRcFm8AVV7k [Watch the Video In Full Screen.]


To calculate the roots of a quadratic equation in a C program, we need to break down the formula and calculate smaller parts of it and then combine to get the actual solution.

So lets calculate square root of b2 – 4 * a * c and store it in variable root_part. Also store 2 * a in variable denom. Now calculate ( – b + root_part ) / denom and store it in root1 and ( – b – root_part ) / denom in root2. Output the values of root1 and root2 to the console window.

Calculating Roots of Quadratic Equation In C

  1.    
  2. #include < stdio.h >  
  3. #include < math.h >  
  4.   
  5. int main()  
  6. {  
  7.     float a, b, c;  
  8.     float root1, root2;  
  9.     float root_part, denom;  
  10.   
  11.     printf("Enter values of a, b and c\n");  
  12.     scanf("%f%f%f", &a, &b, &c);  
  13.   
  14.     if(a == 0)  
  15.     {  
  16.         printf("If a is zero, equation becomes linear and not quadratic\n");  
  17.         printf("Please enter non-zero number for a\n");  
  18.     }  
  19.     else  
  20.     {  
  21.         root_part = sqrt(b * b - 4 * a * c);  
  22.         denom     = 2 * a;  
  23.   
  24.         root1     = ( - b + root_part ) / denom;  
  25.         root2     = ( - b - root_part ) / denom;  
  26.   
  27.         printf("Root1 = %f\nRoot2 = %f", root1, root2);  
  28.     }  
  29.   
  30.     return 0;  
  31. }  

Output 1
Enter values of a, b and c
1
4
4
Root1 = -2.000000
Root2 = -2.000000

Output 2
Enter values of a, b and c
0
4
4
If a is zero, equation becomes linear and not quadratic
Please enter non-zero number for a

Work Space: Cross Verification of Root values
Quadratic Equation: ax2 + bx + c = 0
Let,
a = 1
b = 4
c = 4
i.e., 1x2 + 4x + 4 =0
=> 1x2 + 2x + 2x + 4 = 0
=> x ( x + 2 ) + 2 ( x + 2 ) = 0
=> ( x + 2 ) + ( x + 2 ) = 0
=> x + 2 = 0 AND x + 2 = 0
=> x = -2 AND x = -2

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find Given Number Is Prime or Not: C

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

A natural number greater than 1 that is not a prime number is called a composite number. For example, 7 is prime, as only 1 and 7 divide it, whereas 10 is composite, since it has the divisors 2 and 5 in addition to 1 and 10.

In this video tutorial we show you 3 methods of finding whether the user entered number is a prime number or not.

Since every number is divisible by 1, we start the division(in for loop) from 2.

Note: A number can not be divided(to get a whole number) by another number which is greater than itself.

First Method:
Since every number is divisible by 1, we start the division(in for loop) from 2 till one number less than the user entered value.
Example: If user entered 10. For loop starts from 2 to 9.
for(i=2; i<10; i++) or for(i=2; i< =9; i++)

But the draw back: If user enters 500, the loop must get executed from 2 till 499. i.e., 497 times.

Second Method:
Here we reduce the number of iterations in for loop.
i.e., we divide the user entered value by 2 and divide the user entered value from 2 till num / 2;
Example: If user entered 500, we start the division from 2 till 500/2 i.e., till 250 times.

Third Method:
Here we still reduce the number of iterations in the for loop.
i.e., we take the square root of the user entered number and divide the user entered number from 2 till square root of number.
Example: If user entered 500, we start the division from 2 till sqrt(500) i.e., till 22 times.

So this third method is most optimum, as it involves least number of looping.

Video Tutorial: Find Given Number Is Prime or Not: C



YouTube Link: https://www.youtube.com/watch?v=3MYx9PrpXEg [Watch the Video In Full Screen.]


Full Free Source Code:

  1. #include < stdio .h >  
  2. #include < conio .h >  
  3. #include < math .h >  
  4.    
  5. void main()  
  6. {  
  7. int num, i, fact, inum;  
  8. clrscr();  
  9.    
  10. printf("Enter a number\n");  
  11. scanf("%d", &num);  
  12.    
  13. inum = sqrt(num);  
  14.    
  15. for(i=2; i <= inum ; i++)  
  16.  if( num % i == 0 )  
  17.  {  
  18.    fact = 0;  
  19.    break;  
  20.  }  
  21.  else  
  22.    fact = 1;  
  23.    
  24. if(fact)  
  25.  printf("%d is Prime\n", num);  
  26. else  
  27.  printf("%d is NOT prime\n", num);  
  28.    
  29. getch();  
  30. }  

Table of all prime numbers up to 1,000: