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...

  • 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

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • 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...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

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
ADVERTISEMENT