(10) Write a test program that prompts the user to
enter ten numbers, invoke a method to reverse the numbers, display the
numbers.
Program Code:
import java.util.Scanner;
public class OOP_10
{
public static void reverse(int numbers[])
{
int j=0,temp;
while(j<=numbers.length/2)
{
temp=numbers[j];
numbers[j]=numbers[numbers.length-1-j];
numbers[numbers.length-1-j]=temp;
j++;
}
}
public static void main(String[] args)
{
int i=0;
int num_array[]=new int[10];
Scanner input = new Scanner(System.in);
for(i=0;i<10;i++)
{
System.out.print("Enter at Location Number"+ (i+1) + "=");
num_array[i] = input.nextInt();
}
reverse(num_array);
System.out.println("\n Reverse numbers in an Array are= \n ");
for(i=0;i<10;i++)
{
System.out.println("Value at Location "+ (i+1) + " : "+num_array[i]);
}
}
Output:
No comments:
Post a Comment