Develop an algorithm and write a Java that must accept two integers N1 and N2 as the input. The program must print the desired pattern as shown in the Sample test cases.Input FormatThe first line contains the two integers N1 and N2 as the input.Output FormatPrint the desired pattern as shown in the Sample test cases.Boundary Condition: 0 < N1, N2 <= 1000Otherwise, print ‘invalid’ Sample TestCase-1:Input5 8Output5 6 7 85 6 75 65
Question
Develop an algorithm and write a Java that must accept two integers N1 and N2 as the input. The program must print the desired pattern as shown in the Sample test cases.Input FormatThe first line contains the two integers N1 and N2 as the input.Output FormatPrint the desired pattern as shown in the Sample test cases.Boundary Condition: 0 < N1, N2 <= 1000Otherwise, print ‘invalid’ Sample TestCase-1:Input5 8Output5 6 7 85 6 75 65
Solution
Here is a simple Java program that accepts two integers N1 and N2 as input and prints the desired pattern.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N1 = scanner.nextInt();
int N2 = scanner.nextInt();
if (N1 <= 0 || N2 <= 0 || N1 > 1000 || N2 > 1000) {
System.out.println("invalid");
} else {
for (int i = N2; i >= N1; i--) {
for (int j = N1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
}
This program works as follows:
- It first reads the two integers N1 and N2 from the input.
- It then checks if the input is valid. If either N1 or N2 is less than or equal to 0, or greater than 1000, it prints "invalid" and ends.
- If the input is valid, it enters a nested loop. The outer loop runs from N2 down to N1, and the inner loop runs from N1 up to the current value of the outer loop.
- In each iteration of the inner loop, it prints the current value followed by a space.
- After each iteration of the outer loop, it prints a newline to start a new row of the pattern.
Similar Questions
Design an algoritm and write a Java program with a class that read an integer ‘n’ as input and print the ‘n’ natural numbers in descending order sequence and their “Sum”. Print ‘invalid’ if the boundary conditions are not met.Boundary Condition: 0 < n > 1000Input Format:The first line contains an integer ‘n’Output Format:The first ‘n’ line contains the natural numbers in descending sequence or invalidThe ‘n+1’th line contains the Sum
Sam is organizing a coding workshop and wants to include a fun exercise involving patterns. The task is to create a program that takes an integer n as input and generates a specific pattern. The pattern is given below.Input: 5Output:Input format :The input consists of an integer n.Output format :The output prints the specified pattern.Code constraints :2 ≤ n ≤ 50Sample test cases :Input 1 :5Output 1 : ** ****** ************* ******* ***** *** *Input 2 :6Output 2 : *** ******** **************** ********* ******* ***** *** *
An integer value N is passed as the input. The program must print YES if N is prime number. Else the program must print NO.Input Format:The first line denotes the value of N.Output Format:YES or NO based on if N is a prime number or not. (The OUTPUT is CASE SENSITIVE).Boundary Conditions:2 <= N <= 9999999Example Input/Output 1:Input:19Output:YESExample Input/Output 2:Input:189210Output:NO
Create a Java program that takes two integer inputs 'a' and 'b' from the user and calculates 'a/b'. Implement exception handling to catch potential arithmetic and input mismatch errors, providing appropriate error messages for each case.Your task is to write a Java Program to Handle Arithmetic Exceptions and InputMisMatchExceptions.Note: Refer to the visible test cases for more clarity.Constraint:0≤𝑎≤1020≤𝑏≤102Input Format: The input consists of two numbers each in a new line (integer).Output Format: The output represents the result of the division of the two numbers or any error that occurred while dividing.Sample Test CasesTest Case 1:Expected Output:Enter·the·value·of·'a':·1Enter·the·value·of·'b':·0Arithmetic·Exception:·/·by·zeroTest Case 2:Expected Output:Enter·the·value·of·'a':·sInput·Mismatch·Exception:·Please·enter·valid·integer·values.Test Case 3:Expected Output:Enter·the·value·of·'a':·50Enter·the·value·of·'b':·4Result·of·a/b:·12
Write a java program to print the division of two integers received from the user. Handle exceptions using nested try-catch blocks that checks if both numbers received are valid integers and print the division value if no exceptions occur during the arithmetic operation. Input 1:Enter Number 1: 10Enter Number 2: 5Output 1:2
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.