🎓 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
Finding the largest among three numbers is a common exercise to understand conditional statements in programming. In this article, we'll walk through a C program that identifies the largest number among the three entered by the user.
2. Program Overview
Our program will:
1. Request the user to input three numbers.
2. Compare these numbers to find the largest.
3. Display the largest number to the user.
3. Code Program
#include <stdio.h> // Incorporate the Standard I/O library for input and output functions
int main() { // Starting point of our program
double n1, n2, n3; // Declare three variables of type double to accommodate potential decimals
// Asking the user for three numbers
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3); // Receive the three numbers
if (n1 >= n2 && n1 >= n3) // Check if n1 is the largest
printf("%.2lf is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3) // Check if n2 is the largest
printf("%.2lf is the largest number.", n2);
else // If n1 and n2 aren't the largest, n3 must be
printf("%.2lf is the largest number.", n3);
return 0; // End the program gracefully
}
Output:
Enter three different numbers: 1.23 2.34 3.45 3.45 is the largest number.
4. Step By Step Explanation
1. #include <stdio.h>: By including this header, we're equipped with standard input and output functions.
2. int main(): This denotes the beginning of our program.
3. Declaring variables: We utilize the double data type so that our program can handle numbers with decimals. This makes the program versatile.
4. User input: We ask the user for three distinct numbers, and then capture this input.
5. The comparison:
- Using conditional statements (if-else), the program checks which number is the largest.
- Initially, it checks if n1 is larger or equal to both n2 and n3. If true, n1 is the largest.
- If the first condition fails, it verifies if n2 is the largest.- If neither n1 nor n2 is the largest, then by default, n3 is.
6. Output: The program displays the largest number among the three.
This program offers a straightforward method to compare three numbers, illustrating the power and efficiency of conditional statements in decision-making processes.
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