🎓 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 design an R program to evaluate a given number to discern if it's positive, negative, or zero. Determining the sign of a number is a fundamental operation in numerous mathematical and real-world applications.
2. Program Overview
Our R program will:
1. Prompt the user to input a number.
2. Assess if the number is greater than, less than, or equal to zero.
3. Notify the user if the number is positive, negative, or zero.
3. Code Program
# Prompt the user for the number
number <- as.numeric(readline(prompt = "Enter a number: "))
# Determine and display whether the number is positive, negative, or zero
if (number > 0) {
cat("The number is positive.")
} else if (number < 0) {
cat("The number is negative.")
} else {
cat("The number is zero.")
}
4. Step By Step Explanation
1. We begin by asking the user to provide a number. This is achieved using the readline() function, which captures input from the console. We then convert this input to a numeric value using as.numeric().
2. With a conditional if-else structure, we evaluate the input number. The if statement checks if the number is greater than zero. If this condition holds, we notify the user that the number is positive using the cat() function.
3. The else if statement examines if the number is less than zero. Should this condition be true, we notify the user that the number is negative.
4. If neither of the two conditions are met (the number isn't greater than or less than zero), we default to the final else clause, indicating that the number is zero.
By using this simple R program, you can effortlessly identify whether a number is positive, negative, or zero. This forms a foundational step for a myriad of mathematical processes and analyses in R.
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