R Program to Find the Largest of Three Numbers

Introduction

Finding the largest of three numbers is a common task in programming, often used in scenarios where comparisons are necessary. This can be achieved using conditional statements to compare the values of the three numbers. This guide will walk you through writing an R program that finds and displays the largest of three user-provided numbers.

Problem Statement

Create an R program that:

  • Prompts the user to enter three numbers.
  • Compares the three numbers to find the largest one.
  • Displays the largest number.

Example:

  • Input: Three numbers: 5, 10, 3
  • Output: "The largest number is 10."

Solution Steps

  1. Prompt the User for Input: Use the readline() function to take three numbers as input from the user.
  2. Convert the Inputs to Numeric: Convert the input strings to numeric values using the as.numeric() function.
  3. Compare the Numbers: Use if, else if, and else statements to compare the three numbers.
  4. Display the Largest Number: Use the print() function to display the largest number.

R Program

# R Program to Find the Largest of Three Numbers

# Step 1: Prompt the user to enter three numbers
num1 <- as.numeric(readline(prompt = "Enter the first number: "))
num2 <- as.numeric(readline(prompt = "Enter the second number: "))
num3 <- as.numeric(readline(prompt = "Enter the third number: "))

# Step 2: Compare the numbers to find the largest one
if (num1 >= num2 && num1 >= num3) {
  largest <- num1
} else if (num2 >= num1 && num2 >= num3) {
  largest <- num2
} else {
  largest <- num3
}

# Step 3: Display the largest number
print(paste("The largest number is", largest))

Explanation

Step 1: Prompt the User to Enter Three Numbers

  • The readline() function prompts the user to enter three numbers. Each input is converted to a numeric value using as.numeric() and stored in the variables num1, num2, and num3.

Step 2: Compare the Numbers to Find the Largest One

  • The program uses if, else if, and else statements to compare the three numbers:
    • If num1 is greater than or equal to both num2 and num3, it is assigned as the largest.
    • If num2 is greater than or equal to both num1 and num3, it is assigned as the largest.
    • If neither num1 nor num2 is the largest, then num3 is the largest.

Step 3: Display the Largest Number

  • The print() function is used to display the largest number along with a descriptive message.

Output Example

Example 1:

Enter the first number: 5
Enter the second number: 10
Enter the third number: 3
[1] "The largest number is 10"

Example 2:

Enter the first number: 15
Enter the second number: 7
Enter the third number: 12
[1] "The largest number is 15"

Example 3:

Enter the first number: 4
Enter the second number: 4
Enter the third number: 4
[1] "The largest number is 4"

Conclusion

This R program demonstrates how to find the largest of three numbers using conditional statements (if, else if, else). It covers essential programming concepts such as user input, numeric conversion, and comparison operations. This example is particularly useful for beginners learning R programming and control flow techniques.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top