Select Articles

Program to find the greatest of two numbers and greatest of three numbers

Program to find the greatest of two numbers and greatest of three numbers is discussed here. Input two or integers from the user and find the greatest number among them.

Input & Output format:

Input consists of 2 integers.

Sample Input and Output :

7

9

5

9 is greater.

Algorithm to find the greatest of two numbers and greatest of three numbers

Greatest_of_two_numbers(num1, num2):

If (num1 > num2)

Print num1

Else, print num2

Greatest_of_three_numbers(num1,num2):

If (num1 > num2) and (num1 > num3)

Print num1

Else if (num2 > num1) and (num 2 > num3)

Print num2.

Else, print num3

Program to find the greatest of two numbers and greatest of three numbers are given below :

Program to find the greatest of two numbers

C

// C program to find the greatest of two numbers

#include

int main()

{

//Fill the code

int num1, num2;

scanf(“%d %d”,#1,#2);

if(num1 > num2)

printf(“%d is greater”,num1);

}

else

{

printf(“%d is greater”,num2);

}

return 0;

}

C++

// C++ program to find the greatest of two numbers

#include

using namespace std;

int main()

{

//Fill the code

int num1, num2;

cin >> num1 >> num2;

if(num1 > num2)

{

cout << num1>

}

else

{

cout << num2>

}

return 0;

}

JAVA 8

// Java program to find the greatest of two numbers

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int num1,num2;

num1 = sc.nextInt();

num2 = sc.nextInt();

if(num1 > num2)

System.out.print(num1 + ” is greater”);

else

System.out.print(num2 + ” is greater”);

}

}

PYTHON 3

# Python program to find the greatest of tw numbers

num1 = int(input())

num2 = int(input())

print(max(num1,num2),”is greater”)

OUTPUT

10

15

15 is greater

Program to find the greatest of three numbers

C

// C program to find the greatest of three numbers

#include

int main()

{

//Fill the code

int num1, num2, num3;

scanf(“%d %d %d”,#1,#2,#3);

if(num1 > num2 && num1 > num3)

printf(“%d is greater”,num1);

}

else if(num2 > num1 && num2 > num3)

{

printf(“%d is greater”,num2);

}

else

{

printf(“%d is greater”,num3);

}

return 0;

}

C++

// C++ program to find the greatest of three numbers

#include

using namespace std;

int main()

{

//Fill the code

int num1, num2, num3;

cin >> num1 >> num2 >> num3;

if(num1 > num2 && num1 > num3)

{

cout << num1>

}

else if(num2 > num1 && num2 > num3)

{

cout << num2>

}

else

{

cout << num3>

}

return 0;

}

JAVA 8

// Java program to find the greatest of three numbers

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int num1,num2,num3;

num1 = sc.nextInt();

num2 = sc.nextInt();

num3 = sc.nextInt();

if(num1 > num2 && num1 > num3)

System.out.print(num1 + ” is greater”);

else if(num2 > num1 && num2 > num3)

System.out.print(num2 + ” is greater”);

else

System.out.print(num3 + ” is greater”);

}

}

PYTHON 3

# Python program to find the greatest of three numbers

num1 = int(input())

num2 = int(input())

num3 = int(input())

print(max(num1,num2,num3),”is greater”)

OUTPUT

10

15

12

15 is greater