Menu

Programming for Problem Solving [ Lab Programs ]


Write a C program to find the roots of a Quadratic equation.

The standard form of a quadratic equation is: ax2 + bx + c = 0, where a, b and c are real numbers
The term b2 - 4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.
  If the discriminant is greater than 0, the roots are real and different.
  If the discriminant is equal to 0, the roots are real and equal.
  If the discriminant is less than 0, the roots are complex and different.

Algorithm

start
	step1: Read a,b,c
	step2: discriminant := b2 - 4*a*c
	step3: if discriminant > 0 then
	step4:	root1 := (-b + √(discriminant)) / (2 * a);
	step5:	root2 := (-b - √(discriminant)) / (2 * a);
	step6:   Print root1, root2
	step7: else if discriminant = 0 then
	step8:   root = -b / (2 * a);
	step9:   print root
	step10: else
	step11:	  realPart := -b / (2 * a);
	step12:    imagPart = √(discriminant) / (2 * a);
	step13:	  Print realPart+imagPart"i" and realPart-imagPart"i"
stop

Flowchart


C Programming
//to find the roots of a Quadratic equation

#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, discriminant, realPart, imagPart;

    // Input coefficients a, b, and c
    printf("Enter coefficients a, b and c: ");
    scanf("%f %f %f", &a, &b, &c);

    // Calculate the discriminant
    discriminant = b * b - 4 * a * c;

    if (discriminant > 0) {
        // Two real roots
        float root1 = (-b + sqrt(discriminant)) / (2 * a);
        float root2 = (-b - sqrt(discriminant)) / (2 * a);
        printf("root1 = %.2f and root2 = %.2f\n", root1, root2);
    }
    else if (discriminant == 0) {
        // One real root (both roots are the same)
        float root = -b / (2 * a);
        printf("root1 = root2 = %.2f\n", root);
    }
    else {
        // Complex roots
        realPart = -b / (2 * a);
        imagPart = sqrt(-discriminant) / (2 * a);
        printf("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi\n", realPart, imagPart, realPart, imagPart);
    }

    return 0;
}

OUTPUT-1
Enter coefficients a, b and c: 1 -2 1
root1 = root2 = 1.00
OUTPUT-2
Enter coefficients a, b and c: 1 7 12
root1 = -3.00 and root2 = -4.00
OUTPUT-3
Enter coefficients a, b and c: 5 3 2
root1 = -0.30+0.56i and root2 = -0.30-0.56i

Related Content :

Programming for Problem Solving Lab Programs

   Practice sessions:

1) Write a simple program that prints the results of all the operators available in C (including pre/ post increment , bitwise and/or/not , etc.). Read required operand values from standard input View Solution

2) Write a simple program that converts one given data type to another using auto conversion and casting. Take the values from standard input. View Solution

   Simple numeric problems:

1) Write a program for finding the max and min from the three numbers. View Solution

2) Write the program for the simple, compound interest. View Solution

3) Write a program that declares Class awarded for a given percentage of marks, where mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >= 70% = Distinction.Read percentage from standard input. View Solution

4) Write a program that prints a multiplication table for a given number and the number of rows inthe table. For example, for a number 5 and rows = 3, the output should be:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15 View Solution

5) Write a program that shows the binary equivalent of a given positive number between 0 to 255 View Solution

   Expression Evaluation

1) A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the top of the building. Find the time taken by the ball to reach each floor. (Use the formula s = ut+(1/2)at^2where u and a are the initial velocity in m/sec (= 0) and acceleration in m/sec^2 (= 9.8 m/s^2)). View Solution

2) Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement) View Solution

3) Write a program that finds if a given number is a prime number View Solution

4) Write a C program to find the sum of individual digits of a positive integer and test given numberis palindrome View Solution

5) A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Writea C program to generate the first n terms of the sequence. View Solution

6) Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user View Solution

7) Write a C program to find the roots of a Quadratic equation. View Solution

8) Write a C program to calculate the following, where x is a fractional value.i. 1-x/2 +x^2/4-x^3/6 View Solution

9) Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x^2+x^3+..............+x^n. For example: if n is 3 and x is 5, then the program computes 1+5+25+125. View Solution

   Arrays, Pointers and Functions:

1) Write a C program to find the minimum, maximum and average in an array of integers. View Solution

2) Write a function to compute mean, variance, Standard Deviation, sorting of n elements in a single dimension array. View Solution

3) Write a C program that uses functions to perform Addition of Two Matrices View Solution

4) Write a C program that uses functions to perform Multiplication of Two Matrices View Solution

5) Write a C program that uses functions to perform Transpose of a matrix with memory dynamically allocated for the new matrix as row and column counts may not be the same. View Solution

6) Write C programs that use both recursive and non-recursive functions to find the factorial of a given integer. View Solution

7) Write C programs that use both recursive and non-recursive functions to find the GCD (greatest common divisor) of two given integers. View Solution

8) Write C programs that use both recursive and non-recursive functions to find x^n View Solution

9) Write a program for reading elements using a pointer into an array and display the values using the array View Solution

10) Write a program for display values reverse order from an array using a pointer. View Solution

11) Write a program through a pointer variable to sum of n elements from an array. View Solution

   Files

1) Write a C program to display the contents of a file to standard output device. View Solution

2) Write a C program which copies one file to another, replacing all lowercase characters with their uppercase equivalents. View Solution

3) Write a C program to count the number of times a character occurs in a text file. The file name and the character are supplied as command line arguments View Solution

4) Write a C program that does the following: It should first create a binary file and store 10 integers, where the file name and 10 values are given in the command line. (hint: convert the strings using atoi function) Now the program asks for an index and a value from the user and the value at that index should be changed to the new value in the file. (hint: use fseek function) The program should then read all 10 values and print them back. View Solution

5) Write a C program to merge two files into a third file (i.e., the contents of the first file followed by those of the second are put in the third file). View Solution

   Strings

1) Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent. View Solution

2) Write a C program that converts a number ranging from 1 to 50 to Roman equivalent View Solution

3) Write a C program that uses functions to insert a sub-string into a given main string from a given position View Solution

4) Write a C program that uses functions to delete n Characters from a given position in a given string View Solution

5) Write a C program to determine if the given string is a palindrome or not (Spelled same in both directions with or without a meaning like madam, civic, noon, abcba, etc.) View Solution

6) Write a C program that displays the position of a character ch in the string S or – 1 if S doesn‘t contain ch View Solution

7) Write a C program to count the lines, words and characters in a given text. View Solution

   Miscellaneous:

1) Write a menu driven C program that allows a user to enter n numbers and then choose between finding the smallest, largest, sum, or average. The menu and all the choices are to be functions. Use a switch statement to determine what action to take. Display an error message if an invalid choice is entered. View Solution

2) Write a C program to construct a pyramids. View Solution

   Sorting and Searching

1) Write a C program that uses non recursive function to search for a Key value in a given list of integers using linear search method View Solution

2) Write a C program that uses non recursive function to search for a Key value in a given sorted list of integers using binary search method. View Solution

3) Write a C program that implements the Bubble sort method to sort a given list of integers in ascending order View Solution

4) Write a C program that sorts the given array of integers using selection sort in descending order View Solution

5) Write a C program that sorts the given array of integers using insertion sort in ascending order View Solution

6) Write a C program that sorts a given array of names View Solution