R Program to Find Roots of Quadratic Equation

🎓 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:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare