Site icon Codeforcoding

Java program:find smallest of three numbers using method

 Java program:find the smallest of three numbers using method

In this tutorial, we will discuss the concept of Java program:find the smallest of three numbers using the method.

In this post, we will learn how to find the smallest number among three numbers using a user-defined method in the Java programming language

Java program:find smallest

 

In my previous post, I have explained the various approaches to find the smallest number of among  three numbers in Java language

However, in this program, we embed the logic of the “find the smallest of three numbers” program in the method.

Here, I have explained how to find the smallest number from the given three number and how to embed this logic in the method.

Program 1

Find the smallest of three numbers using if else if statements

This program allows the user to enter three numbers and compare to select the smallest number using if-else-if statements

import java.util.Scanner;
class SmallestNum2{
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

//calling the method
findSmallest(num1,num2,num3);

}

static void findSmallest(int num1,int num2, int num3){//method definition
    if(num1<=num2 && num1<=num3){
        System.out.println(num1+" is the smallest number");
        
    }
    else if(num2<=num1 && num2<=num3){
        System.out.println(num2+" is the smallest number");
        
    }
    else{
        System.out.println(num3+" is the smallest number");
        
    }
    
}

}

When the above code is executed, it produces the following results

Enter the first number
45
Enter the second number
89
Enter the third number
23
23 is the smallest number

Program 1

Find the smallest of three numbers using nested if statements

This program allows the user to enter three numbers and compare to select the smallest number using nested-if statements

import java.util.Scanner;
class SmallestNum3{
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

//calling the method
findSmallest(num1,num2,num3);

}
static void findSmallest(int num1,int num2, int num3){//method definition
    if(num1<=num2){
    if(num1<=num3)
        System.out.println(num1+" is the smallest number");
        else
        System.out.println(num3+" is the smallest number");
    }
    else{
    
    if(num2<=num3)
        System.out.println(num2+" is the smallest number");
        
    else
        System.out.println(num3+" is the smallest number");
    }
    
}

}

When the above code is executed, it produces the following results

Enter the first number
79
Enter the second number
23
Enter the third number
56
23 is the smallest number

 

 

Smiler post

C++ code to find the smallest of three numbers using the function

C program to find the smallest among three numbers using the function

Python code to find the smallest of three numbers using the method

Java code to find the smallest of three numbers using the method

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

The operator in C++ language

If statement in C++ language

Nested if statement in C++ language

Data type in C++ language

Variable in C++ language

 

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

The operator in Python language

If statement in Python language

function in Python

Data types and Variable in Python

 

Datatype and variables in Java programming language

Operator in Java language

If statements in Java language

Method in Java language

Nested if statements in the Java language

The method in Java language

 

 

Cpp program:find smallest of three numbers using function
Python program: find smallest of three numbers using the function
Exit mobile version