GTU OOP Program - 10
10). Write a test program that prompts the user to enter ten numbers, invoke a method to reverse the numbers, display the numbers.
import java.util.Scanner; class Program_10 { public static void reverse(int list[]) { int temp; for (int i = 0, j = list.length - 1; i < list.length / 2; i++, j--) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } public static void main(String[] args) { Scanner input = new Scanner(System.in); int numbers[] = new int[10]; // prompt the user to enter ten numbers System.out.print("Enter ten numbers : "); for (int i = 0; i < numbers.length; i++) { numbers[i] = input.nextInt(); } // Call reverse nethod to reverse the numbers reverse(numbers); // display the numbers System.out.print("Numbers in Reverse order are : "); for (int j : numbers) { System.out.print(j + " "); } System.out.println(); } }
Output
Comments