🎓 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 the world of mathematics and programming, determining whether a number is even or odd is a foundational check that often serves as a stepping stone to more intricate operations or algorithms. In this tutorial, we will create a simple R program that assesses if a given number is even or odd.
2. Program Overview
The program's objective is to request a number from the user.
It then ascertains whether the provided number is even or odd and displays the result accordingly.
3. Code Program
# Prompt the user to enter a number
cat("Enter a number: ")
number <- as.integer(readLines(n=1))
# Check if the number is even or odd using the modulo operation
if (number %% 2 == 0) {
cat("The number", number, "is even.\n")
} else {
cat("The number", number, "is odd.\n")
}
Output:
Enter a number: 7 The number 7 is odd.
4. Step By Step Explanation
1. We commence the program by prompting the user for a number with the cat function.
# Prompt the user to enter a number
cat("Enter a number: ")
2. The readLines(n=1) function is used to capture the input from the user. As it's received in character format, as.integer is employed to convert it into an integer. This value is saved in the number variable.
number <- as.integer(readLines(n=1))
3. To determine if the number is even or odd, the program harnesses the modulo operation (%%). This operation returns the remainder when one number is divided by another.
if (number %% 2 == 0) {
4. If the number modulo 2 yields 0 (i.e., number %% 2 == 0), then the number is even. Otherwise, the number is odd.
5. The program then uses the cat function to display the result, informing the user whether the inputted number is even or odd.
# Check if the number is even or odd using the modulo operation
if (number %% 2 == 0) {
cat("The number", number, "is even.\n")
} else {
cat("The number", number, "is odd.\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