🎓 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
Multiplication is another fundamental arithmetic operation that is frequently used in both everyday scenarios and computational tasks. This blog post will guide you through creating a basic R program that multiplies two numbers, input by the user. While R is commonly known for its data analysis and statistical modeling capabilities, it is also quite useful for elementary programming tasks like this.
2. Program Overview
The aim of this program is to ask the user for two numbers and then calculate the product of these numbers. The result will be displayed on the console.
3. Code Program
# Prompt the user for the first number
cat("Enter the first number: ")
number1 <- as.numeric(readLines(n=1))
# Prompt the user for the second number
cat("Enter the second number: ")
number2 <- as.numeric(readLines(n=1))
# Compute the product of the two numbers
product <- number1 * number2
# Display the result
cat("The product of", number1, "and", number2, "is:", product, "\n")
Output:
Enter the first number: 6 Enter the second number: 7 The product of 6 and 7 is: 42
4. Step By Step Explanation
1. The program begins by using the cat function to display a message prompting the user for the first number.
# Prompt the user for the first number
cat("Enter the first number: ")
2. readLines(n=1) is used to capture one line of input from the user. The input is initially in character format, so we use as.numeric to convert it into a numeric value. This numeric value is stored in the variable number1.
number1 <- as.numeric(readLines(n=1))
3. A similar approach is used to collect the second number from the user, which is stored in the variable number2.
# Prompt the user for the second number
cat("Enter the second number: ")
number2 <- as.numeric(readLines(n=1))
4. The multiplication operation is performed by multiplying number1 by number2, and the result is stored in the variable product.
# Compute the product of the two numbers
product <- number1 * number2
5. Finally, we display the calculated product to the user using the cat function, which is capable of concatenating and displaying multiple arguments.
# Display the result
cat("The product of", number1, "and", number2, "is:", product, "\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