In this tutorial, we will discuss the concept of Java program:find greatest of three numbers using method
In this post, we will learn how to find the greatest number among three numbers using a user-defined method in the Java programming language
In my previous post, I have explained the various approaches to find the largest number of among three numbers in Java language
However, in this program, we embed the logic of the “find the greatest of three number” program in the function.
Here, I have explained how to find the greatest number from the given three number and how to embed this logic in the function.
Program 1
This program allows the user to enter three numbers and compare to select the largest number using if-else-if statements
import java.util.Scanner; class greatestNum{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number\n"); int num1=scan.nextInt();//reads num1 from user System.out.print("Enter the second number\n"); int num2=scan.nextInt();;//reads num2 from user System.out.print("Enter the Third number\n"); int num3=scan.nextInt();;//reads num3 from user findLargest(num1,num2,num3);//calling the method } static void findLargest(int num1,int num2, int num3){//method definition if(num1>=num2 && num1>=num3){ System.out.println(num1+" is the biggest number"); } else if(num2>=num1 && num2>=num3){ System.out.println(num2+" is the biggest number"); } else{ System.out.println(num3+" is the biggest number"); } } }
When the above code is executed, it produces the following results
Enter the first number 123 Enter the second number 789 Enter the third number 543 789 is the biggest number
Program 2
This program allows the user to enter three numbers and compare to select the largest number using nested if statements
import java.util.Scanner; class greatestNum1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number\n"); int num1=scan.nextInt();//reads num1 from user System.out.print("Enter the second number\n"); int num2=scan.nextInt();;//reads num2 from user System.out.print("Enter the Third number\n"); int num3=scan.nextInt();;//reads num3 from user findLargest(num1,num2,num3);//calling the method } static void findLargest(int num1,int num2, int num3){//method definition if(num1>=num2){ if(num1>=num3) System.out.println(num1+" is the biggest number"); else System.out.println(num3+" is the biggest number"); } else{ if(num2>=num3) System.out.println(num2+" is the biggest number"); else System.out.println(num3+" is the biggest number"); } } }
When the above code is executed, it produces the following results
Enter the first number 987 Eneter the second number 456 Enter the third number 789 987 is the biggest number
Similar post
C program to find the largest among three numbers using function
Python code to find the largest of three numbers using the function
C++ code to find the largest of three numbers using the function
Java code to find the largest of three numbers
C code to find the largest of three numbers
C++ code to find the largest of three numbers
Python code to find the largest of three numbers
Java program to find the middle of three number using Method
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Python code to find the middle of three number using the function
Java code to find middle of three numbers
C code to find middle of three numbers
C++ code to find middle of three numbers
Python code to find middle of three numbers
Java code to find largest of three numbers in an array
Java code to find smallest of three numbers in an array
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Suggested for you
Nested if statement in C++ language
The operator in Python language
If statement in Python language
Data types and Variable in Python
Datatype and variables in Java programming language
If statements in Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…