Dremendo Tag Line

Input 10 numbers in 1d array and check all numbers are same or not in Java

One Dimensional Array - Question 2

In this question, we will see how to input 10 numbers in a one dimensional integer array and check whether all numbers in it are same or not in Java programming. To know more about one dimensional array click on the one dimensional array lesson.

Q2) Write a program in Java to input 10 numbers in a one dimensional integer array and check whether all numbers in it are same or not.

Program

import java.util.Scanner;

public class Q2
{
    public static void main(String args[])
    {
        int a[]=new int[10], i,c=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 10 numbers");
        for(i=0; i<10; i++)
        {
            a[i]=sc.nextInt();
        }

        for(i=0; i<10; i++)
        {
            if(a[0]==a[i])
            {
                c++;
            }
        }

        if(c==10)
        {
            System.out.println("All numbers are same");
        }
        else
        {
            System.out.println("All numbers are not same");
        }
    }
}

Output

Enter 10 numbers
2
2
2
2
2
2
2
2
2
2
All numbers are same
video-poster