🎓 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
Besides basic arithmetic operations like addition, subtraction, multiplication, and division, finding the remainder of a division is a fundamental task in many mathematical computations. This operation is commonly used in algorithms, and it's particularly helpful for tasks like determining if a number is even or odd. In this guide, we'll construct a simple R program to compute the remainder when one number is divided by another.
2. Program Overview
The purpose of this program is to acquire two numbers from the user, a dividend and a divisor.
It will then determine the remainder when the dividend is divided by the divisor, and subsequently display this remainder.
3. Code Program
# Prompt the user for the dividend (the number to be divided)
cat("Enter the dividend: ")
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: ")
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 remainder using the modulo operation (%%)
remainder <- dividend %% divisor
# Display the result
cat("The remainder when dividing", dividend, "by", divisor, "is:", remainder, "\n")
}
Output:
Enter the dividend: 17 Enter the divisor: 4 The remainder when dividing 17 by 4 is: 1
4. Step By Step Explanation
1. We initiate the program by using the cat function to request the dividend from the user.
# Prompt the user for the dividend (the number to be divided)
cat("Enter the dividend: ")
2. The readLines(n=1) function captures the user's input. Since it's received as a character, we apply as.numeric to convert it to a numeric value and store it in the dividend variable.
dividend <- as.numeric(readLines(n=1))
3. Similarly, the program asks for the divisor and processes the input in the same manner.
cat("Enter the divisor: ")
divisor <- as.numeric(readLines(n=1))
4. Before diving into the computation, the program checks if the divisor is zero since division by zero is undefined and results in an error.
# 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")
}
5. If the divisor is valid (non-zero), the program uses the modulo operation (%%) to calculate the remainder.
# Compute the remainder using the modulo operation (%%)
remainder <- dividend %% divisor
6. Lastly, the cat function is employed to display the calculated remainder.
# Display the result
cat("The remainder when dividing", dividend, "by", divisor, "is:", remainder, "\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