JAVA Pyramids
In the last few tutorials we have discussed some basic concepts of programming and in next few posts we will be using them to make some simple programs.
Reverse Pyramid: The next program we will be making is to print a reverse pyramid. The reverse pyramid will look like:-
The code for printing a reverse pyramid is not very different from the previous one. Infact we can actually do it just by changing one line. Take a look at the code below.
If you look at the code above, we have just made a slight change in the declaration of outer loop. This it the outer loop will start from the entered number and end at 1. After every iteration we decrease the value by one.
Output of Program:-
Some more examples of different Pyramids:-
If you look at this example, it can look a bit confusing, but it is not as much difficult as you might think. Lets take a look below.
Now its easy to understand the logic for this program. For this program we have to need to an extra for loop in our previous code of pyramid. Here is the code :
Output of Program:-
Pyramids are very common examples that I think every programmer had made at the time of start learning programming. Pyramids are very useful in teaching the use of loops. There are numerous types of pyramids example. Lets start with an basic example.
* ** *** **** ***** |
Creating the Pyramid Program:- We will make an simple program that will take an integer input from user and print the asterisks pyramid with that much number of rows.
import java.util.Scanner;public class Pyramid { |
In this program, first we take an integer input form user using Scanner class and than nested for loop is declared (i.e. a loop inside another loop). In this the outer for loop will run the same number of times as enter by user(from 1 to the enter number) and after every iteration it will print a new row and increment the loop control variable by 1. Inside the outer loop a new inner for loop is declared which will print the same number of asterisk as the current iteration value of outer loop control variable.
Output of Program:-
Reverse Pyramid: The next program we will be making is to print a reverse pyramid. The reverse pyramid will look like:-
***** **** *** ** * |
The code for printing a reverse pyramid is not very different from the previous one. Infact we can actually do it just by changing one line. Take a look at the code below.
import java.util.Scanner; public class Pyramid { |
If you look at the code above, we have just made a slight change in the declaration of outer loop. This it the outer loop will start from the entered number and end at 1. After every iteration we decrease the value by one.
Output of Program:-
Some more examples of different Pyramids:-
* ** *** **** ***** |
----* ---** --*** |
import java.util.Scanner; public class Pyramid { |
Output of Program:-
For more pyramid examples go to : JAVA Pyramids 2
how to get code like this
ReplyDelete1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
use x for the "*"
Deletehow about this code?
ReplyDelete12345
-1234
--123
---12
----1
Check this one to form different patterns of pyramid
ReplyDeletehttp://itschoolstudents.blogspot.com/
How about this figure?
ReplyDelete*
***
*****
*******
hi
ReplyDeletemayo clinic diet fatty liver disease mayo clinic
ReplyDeletediet fatty liver disease mayo clinic diet fatty liver disease
Also visit my weblog - fatty liver cure and diet
how to write a simple code for this?
ReplyDelete*****
***
*
how about this?
ReplyDelete*
***
*****
***
*
how about this?
ReplyDelete*
***
*****
***
*
http://javainterviewcracker.blogspot.in/
ReplyDeleteIn the context of Java programming, "pyramids" typically refers to a common programming exercise where you create a pattern resembling a pyramid How Play Games using a combination of loops and print statements.
ReplyDelete