🎓 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'll walk you through a simple R program that divides one number by another, as provided by the user. Even though R is predominantly celebrated for its prowess in data analysis, it's also very handy for fundamental programming exercises like the one we're delving into.
2. Program Overview
The primary goal of this program is to obtain two numbers from the user: a dividend and a divisor.
The program then carries out the division operation and displays the quotient to the user.
3. Code Program
# Prompt the user for the dividend (the number to be divided)
cat("Enter the dividend (the number to be divided): ")
dividend <- as.numeric(readLines(n=1))
# Prompt the user for the divisor (the number by which division is to be performed)
cat("Enter the divisor (the number by which division is to be performed): ")
divisor <- as.numeric(readLines(n=1))
# Ensure that the divisor is not zero to prevent division by zero error
if (divisor == 0) {
cat("Error: Division by zero is not allowed.\n")
} else {
# Compute the quotient of the division
quotient <- dividend / divisor
# Display the result
cat("The result of dividing", dividend, "by", divisor, "is:", quotient, "\n")
}
Output:
Enter the dividend (the number to be divided): 20 Enter the divisor (the number by which division is to be performed): 5 The result of dividing 20 by 5 is: 4
4. Step By Step Explanation
1. We start the program using the cat function to prompt the user for the dividend.
2. The input is captured using readLines(n=1), which reads one line of input from the console. Since the input is a character string by default, as.numeric is utilized to convert it into a numeric value. This is then stored in the dividend variable.
# Prompt the user for the dividend (the number to be divided)
cat("Enter the dividend (the number to be divided): ")
dividend <- as.numeric(readLines(n=1))
3. The same process is repeated to obtain the divisor from the user.
cat("Enter the divisor (the number by which division is to be performed): ")
divisor <- as.numeric(readLines(n=1))
4. Before performing the division, it's imperative to ensure that the divisor is not zero. Dividing by zero is mathematically undefined and would result in an error in most programming contexts.
if (divisor == 0) {
cat("Error: Division by zero is not allowed.\n")
}
5. If the divisor isn't zero, the division operation proceeds, and the result (quotient) is displayed using the cat function.
# Compute the quotient of the division
quotient <- dividend / divisor
6. If the divisor is zero, an error message is presented to the user.
# Display the result
cat("The result of dividing", dividend, "by", divisor, "is:", quotient, "\n")
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