user3168844 user3168844 -4 years ago 132
Java Question

Java program to find the largest & smallest number in n numbers without using arrays

I could get the largest without using arrays but, unable to get the smallest one.

public static void main(String[] args)
int smallest=0;
int large=0;
int num;

System.out.println("enter the number");

Scanner input=new Scanner(;

int n=input.nextInt();

for(int i=0;i<n;i++)
System.out.println("the largest is:"+large);
//gives the largest number in n numbers

code for the smallest..



Answer Source
public static void main(String[] args) {
    int smallest = 0;
    int large = 0;
    int num;
    System.out.println("enter the number");//how many number you want to enter
    Scanner input = new Scanner(;
    int n = input.nextInt();
    num = input.nextInt();
    smallest = num; //assume first entered number as small one
    // i starts from 2 because we already took one num value
    for (int i = 2; i < n; i++) {
        num = input.nextInt();
        //comparing each time entered number with large one
        if (num > large) {
            large = num;
        //comparing each time entered number with smallest one
        if (num < smallest) {
            smallest = num;
    System.out.println("the largest is:" + large);
    System.out.println("Smallest no is : " + smallest);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download