In: Computer Science
Java
Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu. 4. Write an application that prompts the user to enter an integer limit and then print the numbers from 1 to this limit in the following format. The number read should not be greater than 10 and should not be negative. Enter a number: 3 Enter a number: 6 Enter a number: 2 Enter a number: 0 1.Forward 2.Backward Choose an option: 1 The number is 3620 Enter a number: 3 Enter a number: 6 Enter a number: 2 Enter a number: 0 3.Forward 4.Backward Choose an option: 2 Error!
Here is the completed code for the two programs you asked. Named it Program1.java and Program2.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Program1.java
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
// scanner to read user input
Scanner scanner = new Scanner(System.in);
// cretaing an array of 4 numbers, reading 4 numbers from user
int numbers[] = new int[4];
for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
numbers[i] = scanner.nextInt();
}
// displaying choice (to select if user likes to make numbers by forward
// or backward arrangement)
System.out.println("1. Forward");
System.out.println("2. Backward");
System.out.print("Choose an option: ");
// getting choice, switching it
int option = scanner.nextInt();
switch (option) {
case 1:
if (numbers[0] == 0) {
// first digit is 0
System.out.println("Error!");
} else {
// creating the number by arranging numbers forward, and
// displaying it
int number = numbers[0] * 1000 + numbers[1] * 100 + numbers[2]
* 10 + numbers[3];
System.out.println("The number is " + number);
}
break;
case 2:
if (numbers[3] == 0) {
//last digit cant be 0
System.out.println("Error!");
} else {
// creating the number by arranging numbers backward, and
// displaying it
int number = numbers[3] * 1000 + numbers[2] * 100 + numbers[1]
* 10 + numbers[0];
System.out.println("The number is " + number);
}
break;
default:
System.out.println("Invalid choice!");
}
}
}
// Program2.java
import java.util.Scanner;
public class Program2 {
public static void main(String[] args) {
// scanner to read user input
Scanner scanner = new Scanner(System.in);
// initializing a number to -1
int n = -1;
// looping as long as number is not within 1-10
while (n <= 0 || n > 10) {
System.out.print("Enter the upper limit (1-10): ");
// reading n
n = scanner.nextInt();
// validating
if (n <= 0 || n > 10) {
// invalid
System.out.println("Invalid, try again!");
}
}
// looping from 1 to n, printing each number on separate lines
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}