CS 201 Lab Exercise 07 Due: In Lab, the week of March 21st Main topics: Loop Sta
ID: 3683395 • Letter: C
Question
CS 201 Lab Exercise 07 Due: In Lab, the week of March 21st Main topics: Loop Statements Nested Loops Exercise b is designed to give you more pr actice working with loop statements, and in particular, nested T'his la loops Getting Started To start this exercise, you should 1. Open eclipse and start a new Java project named Lab07 2. Add a Class (named Lab07) to this project. Problem Description » For this exercise you will be creating a small Java class that will use a user validation loop to get a number in the range of 1 to 9, inclusive. Then the program will generate a triangular shaped figure on the screen, based on the user's input » The triangular shaped figure for an input of 1 is: The triangular shaped figure for an input of 2 is: 2112 » The triangular shaped figure for an input of 3 is: 2112 321123 » The triangular shaped figure for an input of n is: 2112 321123 ·Once you have written your cl 1. Make sure that your programs compile and run without errors or warnings. 2. Run your program enough times to check all the choices for correctness. 3. If it runs correctly, then see your TA for a check-off.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int n;
int j=0,i=0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i=0;i<n;i++){
for(int l=n-i;l>=0;l--){
System.out.print(" ");
}
for(j=i;j>=0;j--){
System.out.print(j+1);
}
for(int k=0;k<=i;k++){
System.out.print(k+1);
}
System.out.println(" ");
}
}
}