Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Print the roots of Bhaskara’s formula from the given three floating numbers

C Basic Declarations and Expressions: Exercise-20 with Solution

Write a C program to print the roots of Bhaskara’s formula from the given three floating numbers. Display a message if it is not possible to find the roots.

Pictorial Presentation:

C Programming: Print the roots of Bhaskara’s formula from the given three floating numbers

C Code:

#include <stdio.h>
#include <math.h>    
int main() {
	double a, b, c, pr1;  	
	printf("\nInput the first number(a): "); 
    scanf("%lf", &a);
    printf("\nInput the second number(b): "); 
    scanf("%lf", &b);
    printf("\nInput the third number(c): "); 
    scanf("%lf", &c);
    
	pr1 = (b*b) - (4*(a)*(c));
		
    if(pr1 > 0 && a != 0) {
		double x, y;
		pr1 = sqrt(pr1);
		x = (-b + pr1)/(2*a);
		y = (-b - pr1)/(2*a);
		printf("Root1 = %.5lf\n", x);
		printf("Root2 = %.5lf\n", y);
	} 
	else
	 {
		printf("\nImpossible to find the roots.\n");
	}
	
	return 0;
}

Sample Output:

Input the first number(a): 25                                          
                                                                       
Input the second number(b): 35                                         
                                                                       
Input the third number(c): 12                                          
Root1 = -0.60000                                                       
Root2 = -0.80000 

Flowchart:

C Programming Flowchart: Print the roots of Bhaskara’s formula from the given three floating numbers

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program that accepts 4 integers p, q, r, s from the user where q, r and s are positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q print "Correct values", otherwise print "Wrong values".
Next: Write a C program that reads an integer and check the specified range where it belongs. Print an error message if the number is negative and greater than 80.
Specified Range: [0, 20], [21, 40], [41, 60], [61, 80]

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP