Question

In: Computer Science

8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...

8.26 LAB: Output numbers in reverse

Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers.

Ex: If the input is:

5 2 4 6 8 10

the output is:

10 8 6 4 2

To achieve the above, first read the integers into an array. Then output the array in reverse.

255960.1500692

LAB ACTIVITY

8.26.1: LAB: Output numbers in reverse

0 / 10

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userList = new int[20]; // List of numElement integers specified by the user
int numElements; // Number of integers in user's list
// Add more variables as needed

numElements = scnr.nextInt(); // Input begins with number of integers that follow
  
/* Type your code here. */
}
}

Solutions

Expert Solution

Here I have first sorted the arry and then reverse of the array is printed.

import java.util.Scanner;

public class Sorting 
{

        public static void main(String[] args) 
        {
                // TODO Auto-generated method stub
                Scanner sc = new Scanner(System.in);
                System.out.print("ENTER THE SIZE OF ARRAY :");
                int size = sc.nextInt();
                System.out.println("ENTER THE ELEMENTS OF ARRAY :");
                int arr[]=new int[size];
                int i ,j;
                for(i=0;i<size;i++)
                {
                        arr[i]=sc.nextInt();
                }
                
                //sorting an array in ascending
                int temp=0;
                for(i=0;i<size;i++)
                {
                        for(j=1;j<size-i;j++)
                        {
                                if(arr[j-1]>arr[j])
                                {
                                        temp = arr[j-1];
                                        arr[j-1]=arr[j];
                                        arr[j]=temp;
                                }
                        }
                }
                //displaying the sorted array
                System.out.println("THE SORTED ARRAY IS:");
                for(i=0;i<size;i++)
                {
                        System.out.print(arr[i]+" ");
                }
                
                  System.out.println();  
                System.out.println("Array in reverse order: ");  
                //Loop through the array in reverse order  
                for (i = size-1; i >= 0; i--) {  
                    System.out.print(arr[i] + " ");  
                }

        }

}

output:


Related Solutions

Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 The output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order. def swap_values(user_val1, user_val2) **in Python, please
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT