🎓 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
Determining the larger value between two numbers is a fundamental operation in both mathematics and programming. This kind of comparison finds its way into various algorithms, data analysis tasks, and daily decision-making scenarios. In this guide, we will create an R program that finds the maximum between two user-provided numbers.
2. Program Overview
The primary aim of this program is to prompt the user for two distinct numbers.
It will then analyze and compare these numbers to decide which one is larger.
Following this, it will display the maximum number to the user.
3. Code Program
# Ask the user for the first number
cat("Enter the first number: ")
number1 <- as.numeric(readLines(n=1))
# Ask the user for the second number
cat("Enter the second number: ")
number2 <- as.numeric(readLines(n=1))
# Compare the two numbers to identify the larger one
if (number1 > number2) {
cat("The maximum between", number1, "and", number2, "is:", number1, "\n")
} else if (number1 < number2) {
cat("The maximum between", number1, "and", number2, "is:", number2, "\n")
} else {
cat("Both numbers are equal.\n")
}
Output:
Enter the first number: 12 Enter the second number: 15 The maximum between 12 and 15 is: 15
4. Step By Step Explanation
1. The program kicks off with the cat function, prompting the user for the first number.
# Ask the user for the first number
cat("Enter the first number: ")
2. readLines(n=1) captures the user's input. Since the default form is a character, the as.numeric function converts it to a numeric value, which is stored in the number1 variable.
number1 <- as.numeric(readLines(n=1))
3. Using a similar approach, the program gathers the second number and saves it in the number2 variable.
# Ask the user for the second number
cat("Enter the second number: ")
number2 <- as.numeric(readLines(n=1))
4. With both numbers in place, the program moves on to the comparison phase. Using an if-else conditional structure, the two numbers are evaluated against each other.
5. If number1 is greater than number2, the program displays number1 as the maximum. Conversely, if number1 is less than number2, number2 is showcased as the larger value. Should both numbers be equal, the program informs the user accordingly.
# Compare the two numbers to identify the larger one
if (number1 > number2) {
cat("The maximum between", number1, "and", number2, "is:", number1, "\n")
} else if (number1 < number2) {
cat("The maximum between", number1, "and", number2, "is:", number2, "\n")
} else {
cat("Both numbers are equal.\n")
}
6. The final result is presented to the user via 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