Question

In: Computer Science

Write a C++ program that will take and validates from the user an integer n between...

Write a C++ program that will take and validates from the user an integer n between 1 and 4. The program will then continue by taking 5 characters from the user. The program will ask the user whether he needs to rotate the characters to the left or to the right. If the user enters a wrong answer, the program will do a rotation to the right. Depending on the answer, the program will rotate the characters "n" times and displays the result on the screen. The program should contain 2 loops. Example 1: The user will enter a value n=3, the characters A B C D E, and rotation to the left. The program will then display DEABC. Example 2: The user will enter a value n=3, the characters A B C D E, and rotation to the right. The program will then display CDEAB.

Solutions

Expert Solution

import java.util.*;
public class Main
{
        public static void main(String[] args) {
                System.out.println("Hello World");
                Scanner sc = new Scanner(System.in);
                int n = sc.nextInt();
                char[] ch = new char[5];
                for(int i=0;i<5;i++){
                    ch[i] = sc.next().charAt(0);
                }
                System.out.print("Rotate left or right : ");
                String rotate = sc.next();
                
            if(rotate.equals("right")){
                System.out.println("Rotated List : ");
                for(int i=0;i<5;i++){
                    System.out.print(ch[(i+n-1)%5] + " ");
                }
            }
            else if(rotate.equals("left")){
                System.out.print("Rotated List : ")
                for(int i=0;i<5;i++){
                System.out.print(ch[(i+n)%5] + " ");
                }
            }
        }
}

Drop a comment for any clarification or update, I would love to assist you.


Related Solutions

Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write in java please Instructions Your goal is to take N integer inputs from the user...
Write in java please Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
In C++, Write a program that accepts exactly ten (10) integer numbers from the user. When...
In C++, Write a program that accepts exactly ten (10) integer numbers from the user. When the user is done inputting these numbers, the program prints back: (i) the sum of the 10 numbers, (ii) the minimum value from the 10 numbers, and (iii) the maximum value from the 10 numbers.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT