Printing various patterns in any programming language gives a good start to that language, by printing those patterns we can practice more and its also an important tool to judge your loop knowledge! So today we will learn how to make triangle patterns in JAVA. Many interesting triangle patterns can be made using programming, but in this post I will show you how to make equilateral triangle pattern using Java Language.
1. Lets create a perfect triangle, for that here's a java program source code:
Output of the above program :
2. Now lets create an upside down triangle and to do so, Here's the code :
1. Lets create a perfect triangle, for that here's a java program source code:
import java.io.*; public class triangle { public static void main ( String arg[] ){ InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader read = new BufferedReader(istream) ; System.out.print("Enter Triangle Size : "); int num=0; try{ num=Integer.parseInt( read.readLine() ); } catch(Exception Number){ System.out.println("Invalid Number!"); } for(int i=1;i<=num;i++){ for(int j=1;j<num-(i-1);j++){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); for(int k1=1;k1<k;k1+=k){ System.out.print("*"); } } System.out.println(); } } }
Output of the above program :
2. Now lets create an upside down triangle and to do so, Here's the code :
import java.io.*; public class triangle { public static void main ( String arg[] ){ InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader read = new BufferedReader(istream) ; System.out.print("Enter Triangle Size : "); int num=0; try{ num=Integer.parseInt( read.readLine() ); } catch(Exception Number){ System.out.println("Invalid Number!"); for(int i=1;i<=num;i++){ for(int j=i;j>1;j--){ System.out.print(" "); } for(int k=1;k<=num-(i-1);k++){ System.out.print("*"); for(int k1=1;k1<k;k1+=k){ System.out.print("*"); } } System.out.println(); } }Output of the above program :
Now lets do the same thing in a different way
3. Perfect triangle made by placing blank spaces, Here's the code :
import java.io.*; public class triangle { public static void main ( String arg[] ){ InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader read = new BufferedReader(istream) ; System.out.print("Enter Triangle Size : "); int num=0; try{ num=Integer.parseInt( read.readLine() ); } catch(Exception Number){ System.out.println("Invalid Number!"); } for(int i=1;i<=num;i++){ for(int j=1;j<=num-(i-1);j++){ System.out.print("*"); if(j==num-(i-1)){ for(int k=1;k<=i;k+=1){ System.out.print(" "); } for(int s=1;s<i;s++){ System.out.print(" "); } for(int l=1;l<=num-(i-1);l++){ System.out.print("*"); } } } System.out.println(); } } }
Output of the above program :
4. Upside down triangle made by placing blank spaces, Here's the code :
import java.io.*; public class triangle4 { public static void main ( String arg[] ){ InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader read = new BufferedReader(istream) ; System.out.print("Enter Triangle Size : "); int num=0; try{ num=Integer.parseInt( read.readLine() ); } catch(Exception Number){ System.out.println("Invalid Number!"); } for(int i=1;i<=num;i++){ for(int j=num-i;j<num;j++){ System.out.print("*"); if(j==num-1){ for(int k=i;k<=j+1;k+=1){ System.out.print(" "); } for(int s=1;s<num-(i-1);s++){ System.out.print(" "); } for(int l=num-i;l<num;l++){ System.out.print("*"); } } System.out.println(); } } }
Output of the above program :
You can create triangle of any size ( size here means number of lines ) using these codes.
Various patterns can be printed in Java for more such interesting patterns keep watching the blog!
If you are looking for any other triangle pattern then comment below, we will provide your code as soon as possible!
352 Comments Leave new
«Oldest ‹Older 201 – 352 of 352 Newer› Newest»Hi Sir, please give me code for this output
Reply1 2 3 4 5
5 4 3 2
2 3 4
4 3
3
can you provide code for triangle with only borders an empty inside.
Reply*
* *
* *
* *
* *
for(int i=1;i<n;i++)
Reply{
for(int j=1;j<i;j++)
{
int space++;
System.out.println(space + " '");
}
]
Plz provide code for it
Reply----1
---1 2
--1 2 3
-1 2 3 4
1 2 3 4 5
sorry. this:
Replyb***b
*l**l*
**u**
*e**e*
j***j
* indicates space
code explain the important of forloop but i can't understand the concept so please explain how to derive code?
ReplyCan We create single link list and double link list program in java ?
ReplyPlease can any one produce that output:
Reply1
3 5
7 9 11
13 15 17 19
public class #{
Replypublic static void main(String[]args){
for(int i=1; i<=6; i++){
for(int j=1; j<=i; j++){
System.out.print("#");
}
System.out.println("");
}
}
}
help me please:
Replyouput: using 3 for loops
xoxoxoxoxo
oxoxoxoxox
xoxoxoxoxo
oxoxoxoxox
xoxoxoxoxo
How do i create a program that prints a triangle like this
Reply5
45
345
2345
12345
with it taking an imput of 5 to create this output.
How do i create a program that prints a triangle like this
Reply5
45
345
2345
12345
with it taking an imput of 5 to create this output.
Code for this output :
Reply1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
help me sir....
pleasss solve this patten
Reply4 5 6 7 8 9
9 13 17
22 17
39
use j%2 for printing
ReplyHello sir, i want to print string pattern and string is given as per user input. for example input string is "java ". we have to print like this
Replyj
j a
j a v
j a v a
can you help me please..i want build program the following triangle of numbers like this
Reply1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
Reply2 3 4 5
3 4 5
4 5
5
please help!! :(
*
Reply* *
* * *
* * * *
plz generate a code for me.
import java.util.Scanner ;
Replypublic class NameStar {
public static void main (String[] args) {
int i , j , y ;
Scanner in = new Scanner (System.in);
y = in.nextInt();
space = y ;
for ( i = y ; i >= 1 ; --i){
for ( j = 1 ; j <= i ; ++j){
System.out.print( "*" );
}
System.out.println();
}
}
}
how to print this ? i've try my best but i still can't get it .
Reply****FIK
***FIKFIK
**FIKFIKFIK
*FIKFIKFIKFIK
sir can you please help me with this pattern
Reply5
54
543
5432
54321
sir can you please help me with this pattern
Reply5
54
543
5432
54321
sir can you please help me with this pattern
Reply5
54
543
5432
54321
Can i get help how to do this
Reply*
**
***
****
*****
Can i get help how to do this
Reply*
**
***
****
*****
Can you do this with do while loops
Replyuser input: (any number)
example output
Input: 3
Output:
*
**
***
**
*
Can you do this with do while loops
Replyuser input: (any number)
example output
Input: 3
Output:
*
**
***
**
*
Pls give me a code for this
Reply1
1 2
1 2 5
1 2 5 12
1 2 5 12 29
1 2 5 12 29 70
please send me java script for google map for my web application.
ReplyHi there, I also have a problem like this one, but it's a little bit different. I need to have this output after an INPUT of any letter.
ReplyI need it to be in PHP script.
Enter a letter: E
so this must be the output if letter E is entered.
________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA
if letter F is entered it must be like this and so on.
________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA
___________ABCDEFEDCBA
P.S
(_)underscore are spaces. i just need to put it that way to type it faster. LOL
I have got four programs.
ReplyA. A
A B C
A B C D E
A B C D E F G
B. A B C D E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
C. 123456789
2345678
34567
456
5
456
34567
2345678
123456789
D.
2. 1234554321
1234---4321
123------321
12---------21
1------------1
12---------21
123------321
1234---4321
1234554321
Thank you for your effort and time :)
help me with this pattern
Reply* * * * *
* * * *
* * *
* *
*
can you please help me to print this pattern.......
Reply1
3 3
5 5 5
7 7 7 7
can you please help me to print this pattern.......
Reply1
3 3
5 5 5
7 7 7 7
import java.util.Scanner;
Replyclass FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows of floyd's triangle you want");
n = in.nextInt();
System.out.println("Floyd's triangle :-");
for ( c = 1 ; c <= n ; c++ )
{
for ( d = 1 ; d <= c ; d++ )
{
System.out.print(num+" ");
num++;
}
System.out.println();
}
}
}
can any one help me code for bellow pattern
Reply1 2 3
8 9 4
7 6 5
can any one help me for bellow pattern
Reply1 2 3
8 9 4
7 6 5
Please give me a Java code WAP to display Triangular numbers till n. A Triangular number is formed by the addition of consecutive integers starting from 1.
ReplyExample : 1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
Hence the triangular nos are 3,6,10,….
0
Reply000
00000
0000000
00000
000
0
Please if you know that problem tell me.
Can you help me for this pattern
Reply******
** **
** **
******
Hi, can you help me solve this pattern please?
ReplyGiven an integer N print the numeric pattern. Example:
N = 5
1 1
12 21
123321
12 21
1 1
N = 6
1 1
12 21
123321
123321
12 21
1 1
N = 7
1 1
12 21
123 321
12344321
123 321
12 21
1 1
and so on. Hope you can help! Thank you
15 14 13 12 11
Reply10 9 8 7
6 5 4
3 2
1
* * * *
Reply* * * *
* * * *
* * * *
how about this pattern?
Can u help me for this.
ReplyYour program should display the types of Triangle:
1-Acute Triangle,
2-Right Triangle, and
3-Obtuse Triangle
Sample Run:
Enter type of triangle: 1
0 0 0 * 0
0 0 * 0 0
0 * 0 0 0
* * * * *
Enter type of triangle: 2
0 0 0 0 *
0 0 0 0 *
0 0 0 0 *
* * * * *
Enter type of triangle: 3
0 0 0 0 0
* 0 0 0 0
0 * 0 0 0
0 0 * * *
Can u please explain me how this for loop works(used in the above program)???
ReplyHow both i & k works??(As i'm newbie in java)
for( int i=1 , k=0 ; i<=5 ; i++ , k++ )
class pattern_2
Reply{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=i;j<=5;j++)
{
System.out.print(" * ");
}
System.out.println();
}
}
}
sir,can u help me to print the following series
Reply1)1,5,13,29,49,77,...
2)2,3,5,11,23,29,41,53,83,89,..
3)8,6,23,87,429..
hai can u help me in printing 10 different name using string , scanner and for loop
Replyhai can you help me in printing 10 different name using string , scanner and for loop
Replyhie, The Series is program is,,,,,,,,,,,,,,,,
Replyimport java.util.*;
class Pattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the line no.");
int n=sc.nextInt( );
int i,j,s=0;
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++);
{
s++;
if (s%2==0)
System.out.print("0");
else
System.out.print("1");
}
System.out.println();
}
}
}
1 2 3 4 5
Reply6 7 8
9
10 11 12
13 14 15 16 17
Please help for this pattern
1 2 3 4 5
Reply6 7 8
9
10 11 12
13 14 15 16 17
Please help for this pattern
Hello. I was given the problem of outputting asterisks in the following shape:
Reply*********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
*********
-The instructor also said she would have as "do an explanation" of our program so I would also like to have a detailed explanation of how the code works (I suspect she will ask us what is the sequence of operation of the loop) either attached to the post or sent to me by e-mail at [email protected] if possible. And please inform should anything prevent you from giving me whichever wither the solution or explanation and I will do all I can to do what I need in order to get that code. Thank you!
P.S. Got a sample code from you that would've saved me if the instructor had made it a quiz (when I got to class next day after finding code, I we were told it was just practice and that the above problem was the real homework). Pretty impressive site you have here: all that code for free and such quick replies to questions! This really is a Code Nirvana. I like it. Should I discreetly share about this site to others who need codes?
I need to elaborate on the result. somehow, the asterisks appear closer to each other in the above text box than they should be. The pattern is nine asterisks top row, 8 asterisks(4 on each side, 1 space in between) second row, 6 asterisks third row(three on each side, 3 spaces in the middle), and so on until there are two asterisks with on the left and one on the right side. The pattern is then mirrored. Replacing the space with a "-", the pattern would be:
Reply*********
****-****
***---***
**-----**
*-------*
**-----**
***---***
****-****
*********
Thank you sirs.
* * *
Reply* *
*
wap to dispaly this outupt in java ? any 1
1 1
Reply1 1
1 1
0
1 1
1 1
1 1
1 1
Reply1 1
1 1
0
1 1
1 1
1 1
for( int i=1 , k=0 ; i<=5 ; i++ , k++ ){}
Replyplease explain me the working of this line..\
/* package whatever; // don't place package name! */
Replyimport java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
for(int i=5;i>=0;i--)
{
for(int j=5;j>= i*-1;j--)
{
if(j<=i && j>=i*-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
sir,can u tell me the help me for the logic of the below pattern
Replywhen the number of rows is given as 4,it should print like:
1
2 3
4 5 6
7 8 9 10
7 8 9 10
4 5 6
2 3
1
Sir can you please help me to find out this program
Reply#
*#*
**#**
***#***
Sir can you please help me to find out this program
Reply#
*#*
**#**
***#***
can anyone please tell me the code for the below pattern
Reply*
**
***
****
*****
i wanna code for this output
Reply*****
****
***
**
*
2
Reply2 2
2 4 2
2 8 6 2
2 10 12 8 2
please givw the logic
1
Reply2 3
6 5 4
7 8 9 10
14 13 12 11,i need code for this
1
Reply2 3
6 5 4
7 8 9 10
14 13 12 11,i need code for this
class loafer
Reply{
public static void main(String agrs[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1&&i==1)
{
System.out.println("@");
}
else if(j==2&&i==2)
{
System.out.println("@*@");
}
else if(j==3&&i==3)
{
System.out.println("@*@*@");
}
else if(j==4&&i==4)
{
System.out.println("@*@*@*@");
}
}
}
}
}
class loafer
Reply{
public static void main(String agrs[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1&&i==1)
{
System.out.println("@");
}
else if(j==2&&i==2)
{
System.out.println("@*@");
}
else if(j==3&&i==3)
{
System.out.println("@*@*@");
}
else if(j==4&&i==4)
{
System.out.println("@*@*@*@");
}
}
}
}
}
class loafer
Reply{
public static void main(String agrs[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
if(j==1&&i==1)
{
System.out.println("@");
}
else if(j==2&&i==2)
{
System.out.println("@*@");
}
else if(j==3&&i==3)
{
System.out.println("@*@*@");
}
else if(j==4&&i==4)
{
System.out.println("@*@*@*@");
}
}
}
}
}
Sir, Plz help me
Reply10001
0 0
0 0
0 0
10001
Hi...
ReplyThe triangle pattern coding was the helpful...
Could you please help me by merging codes 3&4
write a program to display the following pattern.
Reply__*
_*_*
*___*
_*_*
__*
please give me the loop code please.
thanks in advance.
write a program to display the following pattern.
Reply__*
_*_*
*___*
_*_*
__*
please give me the loop code please.
thanks in advance.
write a program to display the following pattern.
Reply__*
_*_*
*___*
_*_*
__*
please give me the loop code please.
thanks in advance.
public class Pattern {
Replypublic static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j=1;i--)
{
for(int j=1;j<i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
How to remove duplicate characters from String with out using build function?
ReplyGive me the code of this pattern,if any one know
Reply*
* *
* * *
* * * *
please tell this pattern
ReplyC
C O
C O M
C O M P
C O M P U
C O M P U T
C O M P U T E
C O M P U T E R
PLEASEEEEEEEEEEEEEE!!!
Need the code of this pattern ?
ReplyTwo triangle side by side....
Need the code of this pattern ?
ReplyTwo triangle side by side....
____*_______*____
Reply___***____***_____
_*****__*****_____
**************_____
Need the code for the above pattern please....
hello sir,
Replyi want to print a robot shape as follows
-------
| 0 0 |
|
___
|
/|\
|
|
/|\
Could you please help me in writing code for this shape
Thanks in advance
hello sir,
Replyi want to print a robot shape as follows
-------
| 0 0 |
|
___
|
/|\
|
|
/|\
Could you please help me in writing code for this shape
Thanks in advance
sir can u pls help for this pattern
Reply* * * * * *
* welcome *
* to *
* banglr *
* * * * * *
I want the following pattern
Replya
b
c
d
e
Hi Sir,
ReplyCan you please give me the code for the pattern
----------------11
-------------11 10 11
----------11 10 9 10 11
-------11 10 9 8 9 10 11
please can you help to the earliest possible. It would be great help Sir.
Thanks in advance.
4 3 2 1
Reply4 3 2
4 3
4
4 3
4 3 2
4 3 2 1
sir help do while outer loop ,,for loop inner loop
Code Nirvana Sir I want a program which will print this series :-
Reply1 11 22 33 44 55 66 .....n
I want a program which will print the following series:-
Reply1 11 22 33 44 55 66 ...nn
. please mail this program to :- [email protected]
*
Reply* *
* * *
* * * *
how to print this output ...........
please help me............
input : computer
Replyoutput: co*mp*ut*er*
how to write the code for this type of program
Hi, I have conceived an alg for this pyramid pattern some time ago and i can't figure out the logic anymore, so please someone explain to me my own work :) Here goes the code:
Replychar star = '*';
char[] starsArray = new char[21];
int size = starsArray.length;
for (int i = 0;i < size;i++){
starsArray[i] = star;
for (int j = 0;j <= i;j++){
System.out.print(starsArray[(size - j + i ) / 2 ]);
}
System.out.println();
}
System.out.println();
The pyramid pattern starts with one star at the top if (size % 2 = 1) and with two stars if (size % 2 = 0).
need floyd's triangle logic
ReplyHi could you help me to figure out the code for this:
Reply1
123
12345
123
1
for odd number
now for even number:
1
123
123
1
Diamond shaped
2
Reply4 6
8 10 12
14 16 18 20
2
Reply4 6
8 10 12
14 16 18 20
hi Pankaj, can you please make a code fo this one ? i really need your help
Reply3
3 3
3 3 3
3 3 3 3
1) 1
Reply11
121
1331
14641
2)
1
31
531
7531
97531
97531
3)
@
@@@
@@@@@
4)
*
***
*****
***
*
sir wanna java code for this
Reply9 9 9 9 9 9 9 9 9
7 7 7 7 7 7 7
5 5 5 5 5
3 3 3
1
Sir need a java code for this pattern
Reply9 9 9 9 9 9 9 9 9
7 7 7 7 7 7 7
5 5 5 5 5
3 3 3
1
how to print ?
Reply1
21
321
4321
54321
how to print ?
Reply1
21
321
4321
54321
please can u jst give the function to this...
Reply1
01
101
0101
10101
123
Reply8 4
765
and
----1
---2-
--3--
-4---
5----
and
For Input-4
01
02 03
04 05 06
07 08 09 10
06 05 04
03 02
01
how to print
Reply1
3 8 5
7 9 40 11 13
1
Reply3 2 4
5 6 7 8 9
4 2 3
1
Plz sir reply this patin
1
Reply3 2 4
5 6 7 8 9
4 2 3
1
Plz sir reply this patin
1
Reply1 2
1 2 5
1 2 5 10
1 2 5 10 17
java program to print factorial of given n numbers using recursion
Replyjava program to find the largest and smallest number in an array
ReplyHere is the list of all the programs :
Reply1. Automorphic numbers within a range
2. Neon numbers within a range
3. Special numbers within a range
4. Twin Prime series within a range
5. Unique numbers within a range
6. Duck numbers within a range
7. Disarium numbers within a range
8. Pronic numbers within range
9. Harshad numbers within a range
10. QuickSort Program
11. Twisted Prime Numbers within a range
12. Sum of series - 1*1+2*2+3*3_________upto n terms
13. Sum of series - 1*1+2*2+3*3*3+4*4*4________upto n terms
14. Sum of series - 1-(1+2)-(1+2+3)_____________upto n terms
15. Commission of salesman
Sorry for the long list of programs but these are all the programs i need right now.
All using Scanner class without any functions and methods.
Yours faithfully,
Anugrah Kapoor
can you help me with this?
Reply1
4 9 16
25 36 49 64 81
hello !
ReplyIf I type 9
out put are
1 3
2
4 6
5
7 9
8
1
Reply2 2
3 3 3
4 4 4 4
5 5 5 5 5
hw to do is rply fast plzzzzz
input:1591
Replyoutput:
*
*****
*********
*
if i want to print the pattern COMPUTER
ReplyCOMPUTE
COMPUT
COMPU
COMP
COM
CO
C
IF U CAN FIND A WAY PLEASE HELP ME !!
if i want to print the pattern COMPUTER
ReplyCOMPUTE
COMPUT
COMPU
COMP
COM
CO
C
IF U CAN FIND A WAY PLEASE HELP ME !!
*
Reply*
* *
* *
* * *
* *
* *
*
*
can you help me to this sir..
B
ReplyBA
BAN
......
........
BANGLADESH
Can you please do a pattern of a string using C source code? Like :
Can you help me with this program:
Reply1. Write a java program that will print out following pattern.
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Q6 WAp in java which display the following pattern
ReplyI
C C
S S S
E E E E
please help me to solve following pattern...
Reply*
--*
*--*
--*--*
*--*--*
--*--*
*--*
--*
*
Can anyone help me print this code?:
Reply*
**
* *
* *
* *
* *
*******
Thank you
Hi I need help making a code that can print this out:
Reply*
**
***
****
*****
Can anyone help me out pls
the opposite of that as in from the right side to the left side pls
ReplyThankuu soo much but please kindly tell me the pattern of
Reply3
33
333
and
3
333
3 also
anybody can help me with the operating system scheduling question ??
Replysir can u help me with the operating system scheduling question ?
ReplyPlz... Help me...
ReplyPrint the pattern..
5
55
555
5555
Please tell me java code for this:
Reply*********
**** ****
*** ***
** **
* *
I also want code for 2nd
ReplyI also want code for 2nd
Reply1
Reply1 2
1 2 3
1 2 3 4
can someone help with this output
Replyx
xkx
xkxk
xkxkx
xkxkxk
code for this pattern
Reply1
2 4 6
3 5 7 9 11
A
ReplyA B C
A B C D
A B C D E
This is the pattern please help me print it.
1
Reply2 3 4
5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21
22 23 24
25
Please Give The Write Answers
Generate the following pattern for an input say 1110111 where equal
Replynumber of 1's can be prefixed and suffixed where 0 must be at the middle
position.
Generate the following pattern for an input say 1110111 where equal
Replynumber of 1's can be prefixed and suffixed where 0 must be at the middle
position.
Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.