🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this post, we will explore a C program that finds the roots of a quadratic equation using functions.
2. Program Overview
Our program will:
1. Define a function to calculate the discriminant.
2. Define a function to compute the real and complex roots.
3. In the main function, receive the coefficients ( a, b,) and ( c ) from the user and utilize the above functions to display the roots.
3. Code Program
#include<stdio.h>
#include<math.h>
// Function to calculate the discriminant
double discriminant(double a, double b, double c) {
return (b * b) - (4 * a * c);
}
// Function to compute the roots
void computeRoots(double a, double b, double c, double *root1, double *root2) {
double disc = discriminant(a, b, c);
if (disc > 0) {
*root1 = (-b + sqrt(disc)) / (2 * a);
*root2 = (-b - sqrt(disc)) / (2 * a);
} else if (disc == 0) {
*root1 = *root2 = -b / (2 * a);
} else {
printf("Roots are complex.\n");
*root1 = *root2 = -b / (2 * a);
double imaginary = sqrt(-disc) / (2 * a);
printf("Root 1: %lf + %lf i\n", *root1, imaginary);
printf("Root 2: %lf - %lf i\n", *root1, imaginary);
}
}
int main() {
double a, b, c, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
computeRoots(a, b, c, &root1, &root2);
if (discriminant(a, b, c) >= 0) {
printf("Root 1: %lf\n", root1);
printf("Root 2: %lf\n", root2);
}
return 0;
}
Output:
For a=1, b=-5, and c=6, the program would output: Root 1: 3.000000 Root 2: 2.000000
4. Step By Step Explanation
1. We first calculate the discriminant, which helps us determine the nature of the roots (real and different, real and same, or complex).
2. The computeRoots function computes and prints the roots based on the discriminant's value. For a positive discriminant, we have two distinct real roots. For a discriminant of zero, we have two identical real roots. For a negative discriminant, the roots are complex.
3. In the main function, we capture the input coefficients and then call the functions to display the roots to the user.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment