Pattern based exercises are a good way to learn nested loops in Java. There are many pattern based exercises and one of them is printing Pyramid structure as shown below:
*
* *
* * *
* * * *
* * * * *
You need to write a Java program to print the above pyramid pattern. How many levels the pyramid triangle would have will be decided by the user input. You can print this kind of pattern by using print() and println() method from System.out object. System.out.print() just prints the String or character you passed to it, without adding a new line, useful to print stars in the same line.
*
* *
* * *
* * * *
* * * * *
You need to write a Java program to print the above pyramid pattern. How many levels the pyramid triangle would have will be decided by the user input. You can print this kind of pattern by using print() and println() method from System.out object. System.out.print() just prints the String or character you passed to it, without adding a new line, useful to print stars in the same line.
While, System.out.println() print characters followed by a newline character, which is useful to move to the next line. You can also use the Scanner class to get input from the user and draw a pyramid up to that level only. For example in the above diagram, the pyramid has 5 levels.
Analysis
If you look at the problem then you will find that you need to print the star (*) character in the same line as well as a new line to generate a pyramidical pattern. You can also see that * are separated by space. In programming, to do a task repeatedly e.g. printing star, you can use a loop.This kind of problem, which require printing in row and column usually require two loops, one inside another. Also, known as nested loops. You can use for() loop to create this pattern as shown below:
There are three loops nested at two level, first is for printing each line and inner loops for printing pattern in each line.
public static void drawPyramidPattern() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { System.out.print(" "); } for (int k = 0; k <= i; k++) { System.out.print("* "); } System.out.println(); } }
There are three loops nested at two level, first is for printing each line and inner loops for printing pattern in each line.
Java Program to Print Pyramid Pattern
Here is our Java program to draw the pyramid pattern as shown in the problem statement. In this program, we have two examples of printing pyramids, in the first, we have a printed pyramid of star characters, while, in the second example, we have drawn a pyramid of numbers.The key here is to use both print() and println() method from PrintStream class, which is also easily accessible as System.out object. We have also used nested for loop to draw the pyramid which you will often use to solve this kind of problem.
And, if you want to do more coding practice, here are many more pyramid and star pattern exercises which you can try to solve, this will improve your coding skills significantly:
Sample code in Java to Print the Pyramid Pattern
import java.util.Scanner; /** * Simple Java Program to draw a pyramid pattern. We have used both * System.out.println() and System.out.print() methods to draw stars(*) * in pyramid shape. * * @author WINDOWS 8 * */ public class PrintPyramidTest { public static void main(String args[]) { System.out.println("Pyramid pattern of star in Java : "); drawPyramidPattern(); System.out.println("Pyramid of numbers in Java : "); drawPyramidOfNumbers(); } /** * This method draws a pyramid pattern using asterisk character. You can * replace the asterisk with any other character to draw a pyramid of that. */ public static void drawPyramidPattern() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { System.out.print(" "); } for (int k = 0; k <= i; k++) { System.out.print("* "); } System.out.println(); } } /** * This method draws a pyramid of numbers. */ public static void drawPyramidOfNumbers() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { System.out.print(" "); } for (int k = 0; k <= i; k++) { System.out.print(k + " "); } System.out.println(); } } } Output : Pyramid pattern of star in Java : * * * * * * * * * * * * * * * Pyramid of numbers in Java : 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4
image_credit - geeksforgeeks.org |
That's all about how to print Pyramid using Java pattern. You can find many pattern printing exercises in Java or C++ programming books. You can further refine this program to print any other character instead of * or you can ask the user to enter the number of rows. You can even modify this program to print a pyramid of numbers.
Thanks for reading this article. If you like then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note. If you have any questions or doubt then please let us know and I'll try to find an answer for you.
Btw, what is your favorite coding exercise? prime number, palindrome or this one?
Hi,
ReplyDeleteWe can print triangle using two loops only. :)
for (int i=0;i<=5;i++) {
for (int j=0;j<i;j++) {
System.out.print("* ");
}
System.out.println();
}
Yes, you can and code is fine as well but looks couple of more adjustments
Delete- it prints empty line in first iteration, to avoid that check for j <=i in second loop
- since you are starting from zero in first loop condition should be i<5 , no need of equal otherwise it will print 6 lines.
... just one cycle is enough:
DeleteStringBuilder sb = new StringBuilder();
String sep = "";
for (int i=0; i< 5; i++) {
System.out.println( sb.append(sep).append("*") );
sep = " ";
}
sir pls print this for me
Delete**0**
*0*0*
0*0*0
*0*0*
**0**
1 0 0 0 0 0 0 1
Delete3 3 0 0 0 0 3 3
5 5 5 0 0 5 5 5
7 7 7 7 7 7 7 7
5 5 5 0 0 5 5 5
3 3 0 0 0 0 3 3
1 0 0 0 0 0 0 1
I want pattern like this...Pls anyone can do this...???
public class simple3 {
ReplyDeletepublic static void main(String[] args)
{
for(int i =0;i<=5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(" "+i);
}
System.out.println(" ");
}
}
}
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
for(int i=1; i<=5; i++)
Delete{
int countr = 1;
while(i>=countr)
{
System.out.print(i + " ");
countr++;
}
System.out.println("");
}
how to write program to display
Delete####
###
##
#
int i;
Deletefor( i = 0; i <4; i++)
{
for (int j = 0;j<4-i;j++)
{
System.out.print("#");
}
for(int k = 0; k <=i; k++)
{
System.out.print("");
}
System.out.println();
}
for (int i = 4; i >= 1; i--) {
Deletefor (int j = 1; j <= i; j++) {
System.out.print(" # ");
}
System.out.println();
}
what is the code to get output as
Delete* *
** **
*** ***
**** ****
*********
Can anyone help me.. How to print two star pyramid triangular connecting each other....
ReplyDeletefor (int i = 0; i < 5; i++)
Delete{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
for (int l = 0; l < 5 - i; l++)
{
System.out.print("^ ");
}
System.out.println();
}
Can anyone say how to print two star pyramid with spaces
ReplyDeleteclass Pattern1
Delete{
void main(int n)
{
for(int i=n;i>=1;i--)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int j=i-1;j>=1;j--)
{
System.out.print("*");
}
System.out.println();
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int j=i-1;j>=1;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
Display the code for this:
ReplyDelete* *
* * * *
* * * * * * *
public class abc
Delete{
public static void main(String args[])
{
for(int i=2;i<=4;i+=2)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
for(int k=1;k<=7;k++)
{
System.out.print("*");
}
System.out.println();
}
It was very simple
Deletepublic class Pattern {
Deletepublic static void main(String[] args) {
int k = 2;
for (int i = 2; i <= 4; i++) {
for (int j = 1; j <= k; j++) {
System.out.print("*");
}
k += i;
System.out.println();
}
}
}
public class Pattern {
Deletepublic static void main(String[] args) {
int k = 2;
for (int i = 2; i <= 4; i++) {
for (int j = 1; j <= k; j++) {
System.out.print("*");
}
k += i;
System.out.println();
}
}
}
public class Pattern8 {
Deletepublic static void main(String[] args) {
int k = 2;
for (int i = 2; i <= 4; i++) {
for (int j = 1; j <= k; j++) {
System.out.print("*");
}
k += i;
System.out.println();
}
}
}
Display the code for this:
ReplyDelete* *
* * * *
* * * * * * *
In third line if we have to print six star then code is:
Deletefor(int i=0;i<4;i++)
{
for(int j=0;j<i*2;j++)
{
print("*");
}
println();
}
*
ReplyDelete**
***
****
*****
print this with only two loops
for (int i = 0; i < n; i++)
Delete{
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
for(int i=1;i<=5;i++)
Delete{
for(int j=1;j<=i;j++)
{
sop("*");
}
sopln();
}
No I'm as asking for two pyramids not for triangles
ReplyDeleteI asked for double pyramids not triangle
ReplyDelete1 2 3 4
ReplyDelete8 7 6 5
9 10 11 12
16 15 14 13
Help me to solve this problem
int count = 1;
Deletefor(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i%2!=0)
{
System.out.print(count++);
}
else
{
System.out.print(count--);
}
}
if(i%2!=0)
{
count = count+3;
}
else
{
count = count+5;
}
System.out.println();
}
int m = 1;
Deletefor(int i=1; i<=5; i++)
{
if(i % 2 != 0)
{
for(int j = m;j <= m+4;j++)
{
System.out.print( j+ " ");
}
}
else
{
for(int j = m+4;j >= m;j--)
{
System.out.print( j+ " ");
}
}
System.out.println("");
m= m+5;
}
public static void main(int n)
Delete{int c=1,j=0,k=1,l=0;
for(int i=1;i<=n;i++)
{
if(i%2!=0)
{for( j=k;j<=(n*i);j++)
System.out.print(j+" ");
}
else
{
for( k=j, l=(n*i);k<=(n*i);k++,l--)
System.out.print(l+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Delete// TODO Auto-generated method stub
int l = 1;
int n = 4;
for (int i = 1; i <=n; i++) {
if(i%2!=0){
for (int j = 1; j <=n; j++) {
System.out.print(l++);
}
}
else
{
for (int j = 1; j <=n; j++) {
System.out.print(l+4-j);
}
l=l+4;
}
System.out.println("");
}
}
package com.test;
Deletepublic class Test {
public static void main(String[] args) {
int count=0; // TODO Auto-generated method stub
int k;
for(int i=1;i<16;i++)
{
if(count==4)
{
count=1;System.out.println("");
int count1=0;
for(k=i+3;k<=16;k--)
{
i++;
System.out.print(k+" ");
count1++;
if(count1==4)
{
System.out.println("");
break;
}
}
}
else{count++;}
if(i>16)
break;
System.out.print(i+" ");
}
}
}
How to print these patterns:
ReplyDelete(i)
*
*
*
*
*
(ii)
* *
* *
**
* *
* *
for(i=0;i<5;i++)
Delete{
for(j=0;j<1;j++)
System.out.print(" * ");
System.out.println();
}
(ii)
Deletefor(i=0;i<5;i++)
{
for(j=0;j<2;j++)
System.out.print(" * ");
System.out.println();
}
(I)
Deleteclass Pattern
{
public static void main (String[] args)
{
for(int i =0;i<=5;i++)
{
System.out.print(" "+i);
System.out.println(" ");
}
}
}
public static void main(String[] args) {
Deletefor(int i=1;i<=5;i++)
{
for( int j=1;j<=2;j++){
if(i%3!=0){
System.out.print("*");
System.out.print(" ");
}
else
{
System.out.print("*");
}}
System.out.println();
}}
(i)
ReplyDeletefor(i=0;<5;i++)
{
for(j=0;j<1;j++)
System.out.print(" * ");
System.out.println();
}
compilaction error..!!
Deletebcz illegal start of type
illegal start of type...!!
Deletebcz for(i=0;<5;i++) wrong
for(i=0;i<5;i++);
Any one please help me to send below Java pattern program on my email id
ReplyDeletesinghkar1387@gmail.com
*
++
***
++++
*****
class test
Delete{
public static void main(String args[])
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int row=sc.nextInt();
for(int i=0;i<=row;i++)
{
for(int j=0;j<=i;j++)
{
if((i%2)!=0)
System.out.print("*");
else
System.out.print("+");
}
System.out.println();
}
}
}
you r rocking bro
DeleteClass test
Delete{
Public ststic void main(string args)
{
For(i=1;i<=5;i++)
{
For(j=1;j<=i;j++)
{
If(i%2==0)
{
System.out.println("+");
}else{
System.out.println("*");
}
}
System.out.println(/n);
}
}
}
}
public class Pyramid {
ReplyDeletepublic static void main(String []args)
{
for(int i=0;i<10;i++)
{if (i%2!=0)
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
else
for(int k=0;k<i;k++)
{
System.out.print("+ ");
}
System.out.println();
}
}
}
AAAAA
ReplyDeleteBBBB
CCC
DD
E
class Test4
Delete{
public static void main(String[] args)
{
int a=65;
for (int i=1;i<=5;i++)
{
for (int j=5;j>=i;j--)
{
char ch=(char)a;
System.out.print(ch);
}
a++;
System.out.println();
}
}
}
please solve this below two problems
ReplyDelete(i)12345
1234
123
12
1
(ii)5555
4444
3333
2222
1111
For your first question following is the program:
Deletepublic class PyramidNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i=5;i>=0;i--){
for (int j=1;j<=i;j++){
System.out.print(j);
}//End of j loop
System.out.println("");
}//End of i loop
}
}
For your second question , here you don't have to user nested loop. only one loop is enough , i have commented the first loop in the program:
public class PyramidSamerowNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
//for (int i=5;i>0;i--){
for (int j=5;j>=1;j--){
System.out.print(j);
System.out.print(j);
System.out.print(j);
System.out.print(j);
System.out.println("");
}
System.out.println("");
//}
}
}
public static void main(String[] args) {
Delete// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
for (int j = 1; j <=5-i ; j++) {
System.out.print(j);
}
System.out.println("");
}
}
public static void main(String[] args) {
Deletefor(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(""+j);
}
System.out.println("");
}
}
}
public static void main(String[] args) {
Deletefor(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(""+j);
}
System.out.println("");
}
}
}
how to print this
ReplyDelete*****
* *
* *
* *
*****
class Star2
Delete{
public static void main(String[] args)
{
int n=5;
for(int i=0;i<n;i++)
{
int c=0;
for(int j=0;j<n;j++)
{
if(i==0||i==n-1)
{
System.out.print("*");
}
else
{
if(c<2)
{
System.out.print("* ");
c++;
}
}
}
System.out.println();
}
}
}
1
ReplyDelete2 6
3 7 10
4 8 11 13
5 9 12 14 15
how to create
for(int i=1; i<=5; i++)
Delete{
int s = 4,cnt=1;
int r=i;
int sum = i;
while(cnt= 0 )
{
System.out.print( sum + " ");
sum = sum+s;
s--;
}
}
System.out.println("");
}
for(int i=1; i<=5; i++)
Delete{
int s = 4,cnt=1;
int r=i,sum = i;
while(cnt= 0 )
{ System.out.print( sum + " ");
sum = sum+s;
s--;
}
}
System.out.println("");
}
while(cnt=0)----- may be it's error.....!!!....???
Deletefor (int i = 1; i < 6; i++) {
Deleteif(i==1)
{
System.out.print(i);
//System.out.println();
}
else
{
int sum=i;
int k=4;
System.out.print(i);
for (int j = 1; j < i; j++) {
sum=sum+k;
System.out.print(sum);
k--;
}
}
System.out.println();
1
ReplyDelete12
123
1234
12345
1234
123
12
1
plz can any one give code for this program
class Pattern
Delete{
public static void main(String[] args)
{
int ck=0,c=2;
while(c>0)
{
if(ck==0)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for(int i=1,r=5-1;i<=5-1;i++,r--)
{
for(int j=1;j<=r;j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
package manoj2;
Deletepublic class Patern {
public static void main(String[] args)
{
int m=5;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(m);
}
m--;
System.out.println("");
}
int n=2;
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print(n);
}
n++;
System.out.println("");
}
}
}
Output:
5
44
333
2222
11111
2222
333
44
5
public static void main(String []args){
Deletefor (int i = 1; i <= 6; i++)
{
for(int j=1;j<i;j++)
{
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 6; i++)
{
for(int k=1;k<6-i;k++)
{
System.out.print(k);
}
System.out.println();
}
}
* *
ReplyDelete* * * *
* * * * * *
* * * * * * * *
how to print this pattern in java ,sonupatel984@gmail.com
public class Pattern4{
Deletepublic static void main(String args[]){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<i*2;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Hi how can you display this output?
ReplyDelete*
**
***
****
*****
when the input is 5?
import java.util.*;
Deletepublic class Pattern5{
static Scanner s=new Scanner(System.in);
public static void main(String args[]){
int i,j,n;
System.out.print("Input Number : ");
n=s.nextInt();
for(i=0;i<=n;i++){
for(j=0;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Please help me print this pattern
ReplyDelete5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
package manoj2;
Deletepublic class Patern {
public static void main(String[] args)
{
int m=5;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(m);
}
m--;
System.out.println("");
}
int n=2;
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print(n);
}
n++;
System.out.println("");
}
}
}
public static void main(String[] args) {
Deletefor (int i=5;i>=1;i--){
for (int j=1;j<=6-i;j++){
System.out.print(i);
}System.out.println();
}
for (int i=1;i<5;i++){
for (int j=1;j<=5-i;j++){
System.out.print(1+i);}
System.out.println();
}}
How do you print following pyramid pattern of stars in Java:
ReplyDelete******
*******
********
*******
******
**********
*********
*******
Deletepublic class Pattern10 {
public static void main(String[] args) {
int k=8;
for (int i = 6; i <=7 ;i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
while(k<=12)
{
for (int i = 1; i <=3; i++) {
for (int j = 1; j <=k; j++) {
System.out.print("*");
}
k--;
System.out.println();
}
k=k+5;
}
}
}
Hello Javin, I am big fan of your both Javarevisited. I have been reading from years and it inspired me to create my own blog. I would like to share my latest article with you about printing patterns of numbers in Java, please have a look when you get some time.
ReplyDeleteCan you write Java code for the given program. Ex: jayanthraman94@gmail.com replace all the characters with '*'except'j'and '@gmail.com' output: j*****.....@gmail.com
ReplyDeleteWrite a Java prg given below. Input: jayanthraman@gmail.com. output: j******....@gmail.com
ReplyDeleteCan you write java program for this pattern.
ReplyDeletecan anybody write a java program for this pattern:
ReplyDeletehttps://1.bp.blogspot.com/-XwSWmFZ9o6k/V05e57pzA5I/AAAAAAAAHRc/jwbfL9kLGIoCPpeIlY6BDSZtorlE-kwngCLcB/s640/tempFileForShare_2016-06-01-09-19-47.jpg
Hi how can you display this output?
ReplyDeleteabcde abcde
abcd bcde
abc cde
ab de
a e
are you kidding ?
Deleteimport java.io.*;
Deleteclass patt1{
public static void main(String args[]){
char a=(char)97,b,c;
int i,j,n=5;
for(i=0;ii;j--){
System.out.print(b);
b++;
}
for(int k=0;ki;j--){
System.out.print(c);
c++;
}
System.out.print("\n");
}
}
}
just print it. hahahaha
Deleteplease help me with this....
ReplyDelete*
*A*
*A*A*
*A*A*A*
class pat
Delete{
public static void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0)
System.out.print("A");
else
System.out.print("*");
}
System.out.println();
}
}
}
can u please write the code for below pattern
ReplyDeleteA B C D E A B C D E
A B C D A B C D
A B C A B C
A B A B
A A
A B A B
A B C A B C
A B C D A B C D
A B C D E A B C D E
999999999999999999
Delete88888888 88888888
7777777 7777777
55555 55555
4444 4444
333 333
22 22
1 1
public static void main(String[] args) {
int number = 9;
for(int i=0;i<9;i++){
for(int j=0;j<18;j++){
if(number==6)
continue;
System.out.print(number);
}
if(number != 6)
System.out.println();
number--;
}
}
public static void main(String[] args) {
Delete// TODO code application logic here
int i, j,l;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(j);
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();}
for(i=2;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(j);
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();}
}
}
I want to write a program in Java that was published the following output :
ReplyDelete1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
u got the answer for this?
DeleteThis comment has been removed by the author.
ReplyDeletehow to code this
ReplyDelete*
**
***
****
*****
******
*
*
***
*
ReplyDelete**
***
****
*****
******
*
*
***
How to print the below pattern
ReplyDelete*!
*! *-
*! *- *!
*! *- *! *-
*! *- *! *- *!
How to print this...
ReplyDelete1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Guys i have found easy way to print pattern
ReplyDeletepublic class Try {
public static void main(String[] args) {
int count =6;
for(int i=5;i>0;i--)
{
count--;
for(int j=count;j>0;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
===================output==========================
*****
****
***
**
*
how to print
ReplyDelete*
* *
* * *
* * * *
* * *
* *
*
public class Star3 {
Deletepublic static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
how to reverse the logic i mean to print in decreasing order from 10stars to 1 and from 1start to 10 could anyone help me out in doin this
Deletewrite a program in java that prints the following pyramid:
ReplyDelete----------------------1
--------------------1 2 1
------------------1 2 4 2 1
----------------1 2 4 8 4 2 1
-------------1 2 4 8 16 8 4 2 1
---------1 2 4 8 16 32 16 8 4 2 1
------1 2 4 8 16 32 64 32 16 8 4 2 1
--1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
/* program 11: Full Number Pattern 11 Pyramid
Delete*/
//imports
import java.util.Scanner;
class FullNumberPatternPyramid11
{
public static void main(String[] args)
{
//declarations
int input,RefNum;
System.out.println("enter the size of the pyramid");
input=new Scanner(System.in).nextInt();
if(input>0)
{
for (int rows=1;rows<=input ;rows++ )
{
RefNum=rows;
for(int spaces=1;spaces<=input-rows;spaces++)
{
System.out.print(" ");
}//for
for (int col=1;col<=(2*rows)-1 ;col++ )
{
if(col<=rows)
{
System.out.print(col);
System.out.print(" ");
}//if
else
{
RefNum--;
System.out.print(RefNum);
System.out.print(" ");
}//else
}//for
System.out.println();
}//for
}//if
else
{
System.out.println("enter positive integers only");
}//else
}//main
}//class
Can somebody write a program in java that prints the following pyramid:
ReplyDelete----------------------1
--------------------1 2 1
------------------1 2 4 2 1
----------------1 2 4 8 4 2 1
-------------1 2 4 8 16 8 4 2 1
---------1 2 4 8 16 32 16 8 4 2 1
------1 2 4 8 16 32 64 32 16 8 4 2 1
--1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
int n = 10;
Delete// Loop through the lines from 1 to n
for (int i = 1; i <= n; i++) {
// printing spaces, 4 at a time from j=0 to j= n-i
for (int j = 1; j <= (n - i); j++)
{
System.out.print(" ");
}
// Printing number increamentally from 0 to i-1
for (int j = 0; j < i; j++) {
System.out.printf("%4d", (int) Math.pow(2, j));
}
// Printing number decreamentally from i-2 to 0
for (int j = i - 2; j >= 0; j--) {
System.out.printf("%4d", (int) Math.pow(2, j));
}
System.out.println();
}
}
public class Star3 {
ReplyDeletepublic static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Star3 {
ReplyDeletepublic static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
00
ReplyDelete1001
010010
10100101
0101001010
101010010101
101010010101
0101001010
10100101
010010
1001
00
print this please using loops
ReplyDelete*
*
* *
* *
* * *
public class Star3 {
Deletepublic static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
ReplyDelete**
***
**
* give me the program pls
*
HOW TO PRINT *
Delete***
******
****
**
*
How to print
ReplyDeleteA
AB
ABC
ABCD
ABCDE
please help this pattern
ReplyDelete*
*-*
*-*-*
*-*-*-*
1
ReplyDelete01
101
0101
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
if((i+j)%2==0)
{
System.out.print("1");
}
else
{
System.out.print("0");
}}
System.out.println();
}
}
}
* *
ReplyDelete** **
*****
** **
* *
please given the Logic........?????
how to print
ReplyDelete* * *
* * *
* * *
1
ReplyDelete23
345
4567
56789
class patt5
Delete{
public static void main(String[]args)
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<i;j++)
{
System.out.print(i+j);
}
System.out.println();
}
}
}
in first line, it will print 0
Delete1
ReplyDelete22 22
333 333 333
4444 4444 4444 4444
/*
DeletePrint this output
1
22 22
333 333 333
4444 4444 4444 4444
*/
public class pattern {
public static void main(String[] args) {
int i=0;
int a=8; // its ur number to be patterned
System.out.println("========");
for (i=1; i<=a;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i>1){
for(int k=2; k<=i;k++){
System.out.print(i);
}
System.out.print(" ");
}
}
System.out.println();
}
}
}
i want a pattern of boat ....can anyone help me...pattern should like..
ReplyDelete* * *
* * *** * *
* * ******* * *
* ************* *
* * * * * * * *
* * * * * * *
i want
ReplyDelete1
23
456
78910
int x=1;
Deletefor(int i=0;i<=4;i++)
{
for(int j=0; j<=i;j++)
{
System.out.print(x);
x++;
}
System.out.println();
}
1 1
ReplyDelete1 0 0 1
1 0 1 0 1
1 0 0 1
Hi how can you display this output?
ReplyDelete*
**
***
****
*****
when the input is 5 and using only while loop ?
Thanks.
How to print below java pattern?
ReplyDelete1
72
863
10954
* * *
ReplyDelete* * * * *
* * * * *
* *** *
*
pls slove
ReplyDelete5--
45-
345
21-
1--
HOW To Print??
ReplyDelete1******
12*****
123****
1234***
12345**
123456*
1234567
int i,j,n=1,k;
Deletefor(i=1;i<=7;i++){
for(j=1;j<=i;j++){
System.out.print(""+j);
}
for(k=n;k<=6;k++){
System.out.print("*");
}
n++;
System.out.println(" ");
}
1
ReplyDelete2 6
3 7 10
4 8 11 13
5 9 12 14 15
Hi how to print *
ReplyDelete*****
***
*
Mean to say asterik symbol with asteriks
How to print
Delete* *
* *
*
* *
* *
Pls help
public static void main(String[] args) {
Deleteint i, j;
for(i=3;i>1;i--){
for(j=1;j<=2;j++){
System.out.print("*");
}
System.out.println(" ");
}
for(i=1;i>=1;i--){
System.out.println("*");
}
for(i=3;i>1;i--){
for(j=1;j<=2;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
**
Delete**
*
**
**
can u pls tell me how to print
ReplyDelete*****
* *
* *
* *
*****
===A Hollow square??
How to print\
ReplyDelete*
**
***
****
*****
import java.io.*;
Deleteclass Pat3
{
public static void main(String s[])
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.println(" ");
}
}
}
***********
ReplyDelete***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
***********
public class Aryan
ReplyDelete{
public static void main(String[] args) {
for (int i = 11; i >=1; i--) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <=11; i++) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
sir how to print
ReplyDelete**********
********
******
****
**
****
******
********
**********
what about this?
ReplyDeleteABCDEF
BCDEF
CDEF
DEF
EF
F
Thanks for this site, an another way to print the star pattern in triangular shape is:
ReplyDeletepublic static void main(String[] args) {
for(int i=0;i<=4;i++)
{
for(int j=0;j<=4;j++)
{
if(j<4-i)
{
System.out.print(" ");
}
else
{
System.out.print("* ");
}
}
System.out.println();
}
}
how can print this pattern:
ReplyDelete1
21
321
4321
54321
How to print this pattern
ReplyDelete---*
--**
-***
****
in Java (excluding that dashes)
hi...how to print the following output
ReplyDelete####
###
##
#
1
ReplyDelete12
123
1234
123
12
1
please solve this?
2
ReplyDelete4 4
6 6 6
8 8 8 8
hey.... How to print this output??
class PritStarTraingle{
Deletepublic static void main(String args[]) {
int i,j,k=2;
for(i=0;i<4;i++)
{
for(j=0;j<=i;j++){
System.out.print(k);
}
k=k+2;
System.out.println();
}
}
}
how to print this pattern pls ... i need it badly please
ReplyDelete* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
using for and while loop with bufferedreader?
1 1
ReplyDelete1 2 1
1 2 2 1
1 2 2 2 1
1 2 2 2 2 1
How to print this output??
print 1
ReplyDelete1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1
ReplyDelete1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1
ReplyDelete12
123
1234
12345
How to print this output?
1
ReplyDelete12
123
1234
12345
How to print this output??
import java.io.*;
Deleteclass Pat4
{
public static void main(String s[])
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(j+1);
}
System.out.println(" ");
}
}
}
class PritStarTraingle{
Deletepublic static void main(String args[]) {
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
public static void main(String[] args) {
ReplyDeleteint i,j,n=1,k;
for(i=1;i<=7;i++){
for(j=1;j<=i;j++){
System.out.print(""+i*2);
}
for(k=n;k<=6;k++){
System.out.print("*");
}
n++;
System.out.println(" ");
}
}
can anyone plz help me out with this pattern
ReplyDelete7
14 15
28 29 30 31
56 57 58 59 60 61 62 63
hi does anyone know ho to print a triangle with a shape chosen by the user ?? (with textIO or scanner) thanks alot
ReplyDelete+++++
ReplyDelete+ +
+++++
Help me in printing this:
ReplyDelete1
32
456
10987
1112131415
1
ReplyDelete22
333
4444
55555
how to printing this? plz help
ReplyDelete1
22
333
4444
55555
class PritStarTraingle{
Deletepublic static void main(String args[]) {
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++){
System.out.print(i);
}
System.out.println();
}
}
}
This program to print a triangle..
ReplyDeleteclass StarTriangle
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
9Apps Apk
*
Delete***
*****
*******
*********
How to print that pattern(excluding '-'):
ReplyDelete--------*
------*-*
----*-*-*
--*-*-*-*
*-*-*-*-*
Thanks in advance.
public class PrintStar {
Deletepublic static void main(String a[])
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(" ");
}
for(k=5;k>=i;k--)
{
System.out.print("*");
}
System.out.println();
}
}
}
printing alphabetically charaters in orders of triangle shape
ReplyDeletefor (char i = 'A'; i <= 'Z'; i++) {
for (char j = 'Z'; j >i; j--) {
System.out.print(" ");
}
for (char l = 'A'; l < (2*i-64); l++) {
System.out.print(i+"");
}
System.out.println(" ");
}
1
ReplyDelete121
12321
1234321
How to do this pattern ?? //Actually it is an equilateral triangle type pattern
I need pattern of
ReplyDelete*#*#*
*#*#
*#*
*#
*
149
ReplyDelete28598
7864
189
12
How solve this problem
How to print that pattern(excluding '-'):
ReplyDelete*
***
*****
*******
*** ***
*** ***
*** ***
******
***
*
Guys help needed
ReplyDelete1 5
12 45
123 345
1234 2345
1234512345
how to print the pattern :
ReplyDelete*
* *
* * *
* *
*
Hi, here i represents row, and j represents column but what represents k?
ReplyDeleteHow to print pattern like this-:
ReplyDeleteint array[]={4,1,2,4,3};
* * * * *
* * * *
* * *
* *
1
ReplyDelete23
456
78910
how can print this pattern:
ReplyDelete1
101
10001
100001
1000001
Give Solution of:
ReplyDelete1
23
456
78910
print this :
ReplyDelete1
12
121
1212
12121
* * * * *
ReplyDelete* * *
*
* * *
* * * * *
pattern program in c
print this pls
ReplyDelete1
21
321
4321
54321
how to print this pattern
ReplyDelete1
21
321
4321
54321
The method takes an integer argument and prints the following pattern, shown n = 4.
ReplyDeleteabcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcdcba
Write a program for the below given pattern:
ReplyDelete11
11 10 11
11 10 9 10 11
11 10 9 8 9 10 11
Can somebody please help me?
can anyone do this program please.
ReplyDelete**0**
*0*0*
0*0*0
*0*0*
**0**
can anyone do this program please
ReplyDelete1 2 3 4 3 2 1
1 2 3 * 3 2 1
1 2 * * * 2 1
1 * * * * * 1
*
ReplyDelete-*
-*-
*-*-
*-*-*
-*-*-*
1
ReplyDelete2 3
4 5 6
7 8 9 10
please solve this
hello brother and sister i just have a problem on solving this?
ReplyDelete*********
*********
*********
*********
*********
*********
********
ReplyDelete********
********
********
how is this?
how to print this kind of pattern i need hel guys?
ReplyDelete* * * * *
* * * *
* * *
* *
*
public class Demo {
Deletepublic static void main(String[] args) {
int i,j,k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
}
}
}
how to print this ? help me please
ReplyDelete*****
*****
*****
*****
*****
http://javaconceptoftheday.com/number-pattern-programs-in-java/
ReplyDeletecheck link bro there is 20 numbers pattern which will help u lot
13579
ReplyDelete35791
57913
79135
91357
What will be the Program..??
ReplyDelete1 0 0 0 0 0 0 1
3 3 0 0 0 0 3 3
5 5 5 0 0 5 5 5
7 7 7 7 7 7 7 7
5 5 5 0 0 5 5 5
3 3 0 0 0 0 3 3
1 0 0 0 0 0 0 1
1 3 5 7 9 11 13
ReplyDelete* 2 4 6 8 10 *
* * 3 5 7 * *
* * * 4 * * *
* * 3 5 7 * *
* 2 4 6 8 10 *
1 3 5 7 9 11 13