🎓 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 tutorial, we will develop an R program to compute and display the roots of a quadratic equation.
2. Program Overview
The program will:
1. Prompt the user to input the values for coefficients ( a ), ( b ), and ( c ).
2. Calculate the discriminant.
3. Determine the nature of the roots based on the value of the discriminant.
4. Calculate and display the roots accordingly.
3. Code Program
# Prompt the user to input the coefficients of the quadratic equation
cat("Enter the coefficients a, b, and c separated by spaces: ")
coefficients <- as.numeric(unlist(strsplit(readLines(n=1), " ")))
# Assigning individual coefficients to variables a, b, and c
a <- coefficients[1]
b <- coefficients[2]
c <- coefficients[3]
# Calculate the discriminant
delta <- b^2 - 4*a*c
# Check the nature of the roots based on the discriminant and calculate the roots
if(delta > 0) {
root1 <- (-b + sqrt(delta)) / (2*a)
root2 <- (-b - sqrt(delta)) / (2*a)
cat("The equation has two distinct real roots:", root1, "and", root2, "\n")
} else if(delta == 0) {
root1 <- -b / (2*a)
cat("The equation has one real repeated root:", root1, "\n")
} else {
realPart <- -b / (2*a)
imaginaryPart <- sqrt(abs(delta)) / (2*a)
cat("The equation has complex roots:", realPart, "+", imaginaryPart, "i and", realPart, "-", imaginaryPart, "i\n")
}
Output:
Enter the coefficients a, b, and c separated by spaces: 1 -3 2 The equation has two distinct real roots: 2 and 1
4. Step By Step Explanation
1. The program commences by prompting the user to input the coefficients ( a ), ( b ), and ( c ) using the cat function.
2. The coefficients are captured as a string, split based on spaces, and converted to numeric format. They are then assigned to individual variables for ease of computation.
3. The discriminant ((Delta)) is computed using the formula ( b^2 - 4ac ).
4. The nature of the roots is then determined based on the value of the discriminant:
- If ( Delta > 0 ), the equation has two distinct real roots.- If ( Delta = 0 ), the equation has a single real repeated root.- If ( Delta < 0 ), the equation has complex conjugate roots.
5. Using appropriate formulas, the roots are computed and displayed to the user using the cat function.
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