Question
Write program in Java that produces the number pyramids as shown below:

The programs must prompt the user for how many lines to generate. Your program must be able to generate properly formatted output regardless of the number of lines requested. Notice that the formatting of the output is based upon the maximum number generated in each of the pyramids. The number generated will not be larger than a Long.

You can convert a number to a string like: String str = "" + value;

The format specifier for formatted output (System.out.printf) can be a String variable. You'll need to do this to make sure the output is formatted based on the size of the numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class Main {
public static void main(String[] args) {
  
// TAKING user input of a number
System.out.print("Enter the number of lines: ");
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
  
long i, j;
  
// for numner of lines
for(i=1; i<=n; i++)
{
// for number of spaces at starting
for(j=1; j<= n-i; j++)
{
System.out.print(" ");
}
  
// for first half of the numbers to be printed
for(j=i; j>1; j--)
{
System.out.printf("%-3d",j);
}
  
// for second half of the numbers to be printed
for(j=1; j<=i; j++)
{
System.out.printf("%-3d",j);
}
  
// printing a line
System.out.println();
}
}
}

/*SAMPLE OUTPUTS

Enter the number of lines: 8 2 1 2 3 2 1 2 3 4 3 2 1 2 34 5432 1 2 345 6543 2 1 2 3456 7654 3 21 2 3456 7 87654321 2 345678

Enter the number of lines: 19 4 3 2 1 2 3 4 5 4 3 2 1 2 3 45 6 5 4 3 2 1 2 3 456 7 6 5 432 1 2 3 45 6 7 8 7 6 5 4 3 2 1 2 3 4
*/

Add a comment
Know the answer?
Add Answer to:
Write program in Java that produces the number pyramids as shown below: The programs must prompt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Code in C++ Instructions Write a program which... Prompt the user to enter the number of...

    Code in C++ Instructions Write a program which... Prompt the user to enter the number of values that they would like to insert into a binary search tree. Prompt the user to insert, one-at-a-time, a value into the binary search tree. Print the preorder, postorder, and inorder traversal on the tree. Binary Search Tree The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see...

  • use Java and it must work for any name String Manipulator Write a program to manipulate...

    use Java and it must work for any name String Manipulator Write a program to manipulate Strings. Store your full name into one String variable. It must include first name, middle name, last name, cach separated by spaces. Spaces could vary. If you do not have a middle name make up one For example the string to be processed could be any of the following John Plain Doe John Plain Doc John Plain Doe Your program must be able to...

  • Write a complete Java program with methods that prompt user for the number of floors, rooms,...

    Write a complete Java program with methods that prompt user for the number of floors, rooms, occupied rooms in a hotel. You must validate floors, rooms, occupied rooms. Compute vacant rooms, occupancy rate on each floor and display rooms, occupied rooms, vacant rooms and occupancy rate for each floor. Use the given method names. See validation rules below: 1. getFloors(). This method prompts user for number of floors in a hotel and returns floors to the caller. 2. testFloors(floors). Do...

  • 1. Write and debug a MIPS program that performs the following operations . Prompt for and...

    1. Write and debug a MIPS program that performs the following operations . Prompt for and input three integers "a", "b and "c" using syscalls (3 points) Print all numbers starting from "a" upto "b" (including, but not exceeding "b") at an increment equal to value of"c"(5 points) If ba, then no numbers are printed, and a message is printed to inform it to the user. (3 points) Print the number and average of the generated numbers printed. (3 points)...

  • USE C++ 2.) Write a program that will prompt the use to enter 10 numbers in...

    USE C++ 2.) Write a program that will prompt the use to enter 10 numbers in an array, then display: the 1st, the 5th and the last number. Example, if the user entered 1 2 3 4 5 6 7 8 9 10, the output shall be: 1st is 1 5th is 5 last is 10

  • Write a Java program to input a number of values into an array and then process...

    Write a Java program to input a number of values into an array and then process the array by passing it to various methods that will compute and return information we are interested in. These include: 1. the average of the values in the array 2. the standard deviation of the values in the array 3. the number of items in the array less than the average 4. whether the array’s values are in increasing order or not First, you...

  • This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma...

    This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma separated values) text file. The text file will contain comma separated values in each line. Each line contains data in this format: String(20), String(5),String(10), double. There will be multiple lines with each line containing the same number of values. Ask the user to enter the path for the CSV file for reading the file. After reading the data from the file output the data...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • Java programing Write a program that deals a deck of card into 4 hands – Each...

    Java programing Write a program that deals a deck of card into 4 hands – Each hand having 13 cards. The program should be doing the followings use an array to randomly select 52 cards and deal it into 4 hands. Print each hand unsorted Identify the face value of each hand and display it. You should create a class called Card, and all the methods should be defined within the card class. Hints – Import below java utility to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT