Question

In: Computer Science

Write a program using a Scanner that asks the user for a number n between 1...

Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with 2n - 1 rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth until n rows are printed. Starting at row n + 1, the squares between (n - 1) to 1 are printed, again right justified.  Row n + 2 prints the squares between (n -2) to 1.

Assuming the user enters 4, the program prints the following triangle of 7 rows to the console

              1
          4  1
      9  4  1
16  9  4  1
      9  4  1
          4  1
              1

If the user enters 7, the following 13-row triangle is printed

                              1
                          4  1
                      9  4  1 
                16  9  4  1
           25 16  9  4  1
      36 25 16  9  4  1
 49 36 25 16  9  4  1
      36 25 16  9  4  1
           25 16  9  4  1
                16  9  4  1 
                      9  4  1
                          4  1
                              1
 

Notes

  • Your solution should NOT use the conditionaloperator
  • Your solution should NOT use tabs
  • Your solutions SHOULD have at least one nested loop

Hint

Don't think of the output as a triangle. Think of it as two rectangular tables: one of the first n rows, the second of the last (n-1) rows.

Within each table, some cells are three spaces, some are one space and two digits, and some are two spaces and one digit.

Start by printing an entire table with each cell its appropriate square value. Then figure out how to replace the cells that should "empty" with three spaces instead of a number.

Finally, figure out how to print the one-digit numbers as two spaces and one digit, and the two-digit numbers as one space and two digits.

Grading Elements

  • Program asks the user for a number in the specified range
  • Program displays the correct number of rows
  • Program displays the correct number of columns
  • Each column entry contains the appropriate square number
  • Each row is right-justified, with 1 in the right-most column
  • Row spacing is consistent, with each column's entry directly below the preceding row
  • Program does not use the conditional operator or tabs
  • Program has a nested loop

Solutions

Expert Solution

/* Read the comments and see the output */

import java.util.Scanner; //  Package for Scanner Class

class TriangleSolution { // Name of the Solution Class having main Method

    public static void printTriangle(int n) { // Method to do the printing work based on input

        for(int i=1;i<=n;i++){

            for(int j=n+1;j>i;j--)

                System.out.printf("%2s"," "); // Print space first

            for(int k=i;k>=1;k--)

                System.out.printf("%d ",k*k); // Print square of numbers

            System.out.println(); // Print new line

        }

        for(int i=1;i<=n;i++){ // Do reverse work here

            for(int j=1;j<=i+1;j++)

                System.out.printf("%2s"," "); // Print space first

            for(int k=n-i;k>=1;k--)

                System.out.printf("%d ",k*k); // Print square of number

            System.out.println();// Print new line

        }

    }

    public static void main(String arg[]) { //main method or driver method

        //Declare Scanner class for the input

        Scanner sc = new Scanner(System.in);

        int number; // Variable to read number from 1 to 9 inclusive

        do {

            System.out.print("Enter the Number: ");

            number = sc.nextInt();

        } while(!(number>=1 && number<=9));

        printTriangle(number);

    }

}

/* Here is output */


Related Solutions

Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT