🎓 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
Introduction
In many programming scenarios, it's essential to compare multiple values and determine which is the largest. This task involves finding the largest number from three provided numbers using basic comparison operators in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts three numbers.
- Determines which of the three numbers is the largest.
- Returns and displays the largest number.
Example:
Input:
5,10,15Output:
15 is the largest numberInput:
-7,0,-3Output:
0 is the largest number
Solution Steps
- Read the Input Numbers: Provide three numbers either as part of user input or directly within the code.
- Compare the Numbers: Use basic conditional logic (
if-elsestatements) to compare the three numbers and identify the largest. - Display the Result: Print the largest number.
JavaScript Program
// JavaScript Program to Find the Largest Among Three Numbers
// Author: https://www.javaguides.net/
function findLargestNumber(num1, num2, num3) {
let largest;
// Step 1: Compare the three numbers
if (num1 >= num2 && num1 >= num3) {
largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
largest = num2;
} else {
largest = num3;
}
// Step 2: Display the result
console.log(`${largest} is the largest number`);
}
// Example input
let number1 = 5;
let number2 = 10;
let number3 = 15;
findLargestNumber(number1, number2, number3);
Output
15 is the largest number
Example with Different Input
let number1 = -7;
let number2 = 0;
let number3 = -3;
findLargestNumber(number1, number2, number3);
Output:
0 is the largest number
Explanation
Step 1: Compare the Three Numbers
- The function
findLargestNumber()compares three numbers usingif-elsestatements.- First, it checks if
num1is greater than or equal to bothnum2andnum3. - If not, it checks if
num2is greater than or equal to bothnum1andnum3. - If both conditions fail,
num3is considered the largest.
- First, it checks if
Step 2: Display the Result
- The largest number is stored in the variable
largestand displayed usingconsole.log().
Conclusion
This JavaScript program demonstrates how to find the largest number among three given numbers using comparison operators. This approach is essential in scenarios where comparisons need to be made between multiple values. By understanding how to implement conditional logic, you can extend this program to compare even more numbers or add more complex conditions.
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