program to find the greatest of three
Table of Contents
the algorithm to find the greatest of three numbers
- Start
- Read three numbers from the user and store them in variables: num1, num2, num3
- Set a variable named greatest to the initial value of num1
- Compare num2 with greatest
- If num2 is greater than greatest, update the value of greatest to num2
- Compare num3 with greatest
- If num3 is greater than greatest, update the value of greatest to num3
- Print the value of greatest as the result
- End
This algorithm compares each number with the current greatest number and updates the value of the greatest variable accordingly. At the end of the comparisons, the greatest variable will contain the greatest number among the three.
C program
#include <stdio.h> int main() { int num1, num2, num3, greatest; printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); printf("Enter the third number: "); scanf("%d", &num3); greatest = num1; if (num2 > greatest) { greatest = num2; } if (num3 > greatest) { greatest = num3; } printf("The greatest number is: %d\n", greatest); return 0; }
C++
#include <iostream> int main() { int num1, num2, num3, greatest; std::cout << "Enter the first number: "; std::cin >> num1; std::cout << "Enter the second number: "; std::cin >> num2; std::cout << "Enter the third number: "; std::cin >> num3; greatest = num1; if (num2 > greatest) { greatest = num2; } if (num3 > greatest) { greatest = num3; } std::cout << "The greatest number is: " << greatest << std::endl; return 0; }
Both the C and C++ programs follow the same logic. They prompt the user to enter three numbers, compare them using if statements, and update the value of the greatest
variable accordingly. Finally, they display the greatest number to the user.
Java
import java.util.Scanner; public class GreatestOfThreeNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); int greatest = num1; if (num2 > greatest) { greatest = num2; } if (num3 > greatest) { greatest = num3; } System.out.println("The greatest number is: " + greatest); scanner.close(); } }
python
num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) num3 = float(input("Enter the third number: ")) if num1 >= num2 and num1 >= num3: greatest = num1 elif num2 >= num1 and num2 >= num3: greatest = num2 else: greatest = num3 print("The greatest number is:", greatest)
In this Java program, we use the Scanner
class to read the input from the user. The program prompts the user to enter three numbers and stores them in variables num1
, num2
, and num3
.
Then, we set the variable greatest
to the initial value of num1
. We compare num2
with greatest
and update greatest
if num2
is greater. Similarly, we compare num3
with greatest
and update greatest
if num3
is greater.
Finally, we display the value of greatest
as the result using the System.out.println()
method.
Remember to import the java.util.Scanner
class at the beginning of your program to use the Scanner
class for input reading.
javaScript
// Prompt the user to enter three numbers let num1 = parseInt(prompt("Enter the first number:")); let num2 = parseInt(prompt("Enter the second number:")); let num3 = parseInt(prompt("Enter the third number:")); // Assume the first number is the greatest let greatest = num1; // Compare num2 and num3 with the current greatest number if (num2 > greatest) { greatest = num2; } if (num3 > greatest) { greatest = num3; } // Display the greatest number console.log("The greatest number is: " + greatest);
In this JavaScript program, we use the prompt
function to read the input from the user. The program prompts the user to enter three numbers and stores them in variables num1
, num2
, and num3
.
Then, we assume the first number (num1
) is the greatest. We compare num2
and num3
with the current greatest number and update greatest
if either of them is greater.
Finally, we display the value of greatest
using console.log
.
Note: The parseInt
function is used to convert the input strings to integers for proper numerical comparison.