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

a)     Write a program that reads a list of integers, and outputs all even integers in the...
a)     Write a program that reads a list of integers, and outputs all even integers in the list. For example, if the input list is [1, 2, 13, 14, 25], the output list should be [2, 14]. b)    Based on your code for part (a), write a program that reads a list of integers, and reports True if all numbers in the list are even, and False otherwise. Example: For input [1, 2, 13, 14, 25], the output should be False. Your...
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 in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
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++ 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT