Dremendo Tag Line

Input 10 numbers in 1d array and reverse the original array in Java

One Dimensional Array - Question 7

In this question, we will see how to input 10 numbers in a one dimensional integer array and reverse the original array and print it on the screen in Java programming. To know more about one dimensional array click on the one dimensional array lesson.

Q7) Write a program in Java to input 10 numbers in a one dimensional integer array and reverse the original array and print it on the screen.

Single Dimension Array Question 7

Program

import java.util.Scanner;

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

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

        //Reversing the array
        for(i=0; i<5; i++)
        {
            t=a[i];
            a[i]=a[9-i];
            a[9-i]=t;
        }

        System.out.println("\nModified array after reversal");
        for(i=0; i<10; i++)
        {
            System.out.print(a[i]+" ");
        }
    }
}

Output

Enter 10 numbers
18
12
5
10
15
45
38
72
64
11

Modified array after reversal
11 64 72 38 45 15 10 5 12 18
video-poster