Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Get a input number from user and print a pyramid using that number in java.
For example 5
5
5 5
5 5 5
5 5 5 5

What I have tried:

I have tried getting the input but I can't use the input for printing pyramid.
Posted
Updated 28-Jan-22 21:55pm
Comments
Dave Kreskowiak 28-Jan-22 23:39pm    
Unless you show the code you have, there is little anyone can tell you that's going to help.

Hover the mouse over your question and a green "Improve question" link will show up. Click that and add your code inside HTML pre tags.
Patrice T 29-Jan-22 0:44am    
Show your code to get help.
_Asif_ 29-Jan-22 8:35am    
By the way your pattern is not pyramid!

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
This is not a complicated issue, you just need to decide how many lines to print, and how many numbers on each line
Java
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int nlines = ??; // the number of lines to print
for (int i = 0; i < nlines; i++)
{
    // add the code here to print the numbers
    // and any intervening spaces
}

You should also add some code to check what number the user enters if you have a specific limit.
 
Share this answer
 
Comments
Daniel Remijes 30-Jan-22 1:30am    
What is the code to print the numbers and intervening spaces
Richard MacCutchan 30-Jan-22 3:34am    
That is the part that you are expected to write.
Daniel Remijes 30-Jan-22 3:38am    
I don't get it
Daniel Remijes 30-Jan-22 3:40am    
I have tried looping statement but the output doesn't print the pyramid pattern
Daniel Remijes 30-Jan-22 3:47am    
Please tell me the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900