Find the largest among three numbers in C, C++, Java and Python

Select Articles

Find the largest among three numbers in C, C++, Java and Python

Program to find the largest among three numbers in C, C++, Java and Python is discussed here. Three numbers are given as input and the greatest number among the three numbers is displayed as output.
 

Program to find the largest among three numbers in C, C++, Java and Python using nested-if

C

// C program to find largest of three numbers in C using nested if

#include

int main()

{

int a,b,c;

printf("Enter three numbers : ");

scanf("%d %d %d", &a,&b,&c);

int max = 0;

if(a > b && a > c)

printf("\nThe largest among the three numbers is %d",a);

else if(b > a && b > c)

printf("\nThe largest among the three numbers is %d",b);

else

printf("\nThe largest among the three numbers is %d",c);

printf("\n");

return 0;

}

 

C++

// C++ program to find largest among three numbers using nested if

#include

using namespace std;

int main()

{

int a,b,c;

cout << “nEnter three numbers : “;

cin >> a >> b >> c;

int max = 0;

if(a > b && a > c)

cout << “nThe largest among the three numbers is ” << a>

else if(b > a && b > c)

cout << “nThe largest among the three numbers is ” << b>

else

cout << “nThe largest among the three numbers is ” << c>

cout << endl>

return 0;

}

 

JAVA

import java.util.*;

public class Main

{

public static void main(String[] args) {

int a,b,c;

System.out.print(“Enter three numbers: “);

Scanner sc = new Scanner(System.in);

a = sc.nextInt();

b = sc.nextInt();

c = sc.nextInt();

if(a > b && a > c)

System.out.println(“nThe largest among the three numbers is ” + a);

else if(b > a && b > c)

System.out.println(“nThe largest among the three numbers is ” + b);

else

System.out.println(“nThe largest among the three numbers is ” + c);

System.out.println(“n”);

}

}

 

PYTHON 3

# Python program to find the largest among three numbers using nested-if

print(“Enter three numbers : “)

a = int(input())

b = int(input())

c = int(input())

if(a > b and a > c):

    print(“The largest among the three numbers is “,end = ”)

    print(a)

elif(b > c and b > c):

    print(“The largest among the three numbers is “,end = ”)

    print(b)

else:

    print(“The largest among the three numbers is “,end = ”)

    print(c)

 

Output

Enter three numbers : 3 5 7

The largest among the three numbers is 7
 

Program to find the largest among three numbers in C, C++, Java and Python using functions

C

// C program to find the greatest among three numbers in C using functions

#include >

int largest_among_three(int a, int b, int c)

{

if(a > b && a > c)

return a;

else if(b > a && b > c)

return b;

else

return c;

}

int main()

{

int a,b,c;

printf(“nEnter three numbers : “);

scanf(“%d %d %d”,&a,&b,&c);

int max = largest_among_three(a,b,c);

printf(“nThe largest among the three numbers is %d ” , max);

printf(“n”);

return 0;

}

 

C++

// C++ program to find the greatest among three numbers using functions

#include

using namespace std;

int largest_among_three(int a, int b, int c)

{

if(a > b && a > c)

return a;

else if(b > a && b > c)

return b;

else

return c;

}

int main()

{

int a,b,c;

cout << “nEnter three numbers : “;

cin >> a >> b >> c;

int max = largest_among_three(a,b,c);

cout << “nThe largest among the three numbers is ” << max>

cout << endl>

return 0;

}

 

JAVA

// Java program to find the greatest among three numbers using functions

import java.util.*;

public class Main

{

static int largest_among_three(int a, int b, int c)

{

if(a > b && a > c)

return a;

else if(b > a && b > c)

return b;

else

return c;

}

public static void main(String[] args) {

int a,b,c;

System.out.print(“Enter three numbers: “);

Scanner sc = new Scanner(System.in);

a = sc.nextInt();

b = sc.nextInt();

c = sc.nextInt();

int max = largest_among_three(a,b,c);

System.out.println(“The largest among three numbers is ” + max);

}

}

 

PYTHON 3

# Program to find greatest among three numbers using functions

def largest_among_three(a,b,c):

if(a > b and a > c):

return a

elif(b > c and b > c):

return b

else:

return c

print(“Enter three numbers : “)

a = int(input())

b = int(input())

c = int(input())

max = largest_among_three(a,b,c)

print(“The largest among the three numbers is “,end = ”)

print(max)

 

Output

Enter three numbers : 3 5 7

The largest among the three numbers is 7

 

Program to find greatest of three numbers in C, C++ and Java using conditional operator

C

// C program to find the largest among three numbers in C using the conditional operator

#include

int main()

{

int a,b,c;

printf(“nEnter three numbers : “);

scanf(“%d %d %d”, &a,&b,&c);

int max = 0;

max = ((a > b) && (a > c)) ? a : ((b > a) && (b > c)) ? b : c;

printf(“nThe largest among the three numbers is %d”, max);

printf(“n”);

return 0;

}

 

C++

// C program to find the largest among three numbers in C using the conditional operator

#include

int main()

{

int a,b,c;

printf(“nEnter three numbers : “);

scanf(“%d %d %d”, &a,&b,&c);

int max = 0;

max = ((a > b) && (a > c)) ? a : ((b > a) && (b > c)) ? b : c;

printf(“nThe largest among the three numbers is %d”, max);

printf(“n”);

return 0;

}

 

JAVA

// C program to find the largest among three numbers in C using the conditional operator

#include

int main()

{

int a,b,c;

printf(“nEnter three numbers : “);

scanf(“%d %d %d”, &a,&b,&c);

int max = 0;

max = ((a > b) && (a > c)) ? a : ((b > a) && (b > c)) ? b : c;

printf(“nThe largest among the three numbers is %d”, max);

printf(“n”);

return 0;

}

 

Output

Enter three numbers : 3 5 7

The largest among the three numbers is 7