Program to Print Triangle Pattern in java

Program to Print Triangle Pattern in java

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:

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 :
Equilateral triangle pattern in Java

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 :
Upside Down Triangle Pattern in Java


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 : 
Blank Space Equilateral Triangle pattern inJava

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 : 
Blank Space Upside down triangle pattern in Java

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!


352 Comments Leave new

«Oldest   ‹Older   1 – 200 of 352   Newer›   Newest»

Great help!
Thanks for sharing these Triangle patterns....

Reply

thank you so much .it was very helpful

Reply

Glad to know you liked it! keep visiting and contact us if you have any other problem related to programming or computers.

Reply

Can these code be simpler. For example can we write "upside down" with two for loops?

Reply

Hi Ravikiran ! Above upside down triangle pattern can't be printed using 2 for loops. We used 4 loops because:
1. for number of lines
2. for blank spaces to align center
3. for left side 'right angled triangle'
4. for right side 'right angled triangle'

I think it can be reduced from 4 to 3 or so using some if-else conditions but that will make in confusing!
If you have simpler code to print the above patterns than share with us! :)

Reply

i need a code that has 4 triangles, like the last 2 sets of code, but all iin one. but it needs to be simple

Reply

Just add 4th program loops below 3rd program!

Reply

1
1 2 3
4 5 6 7 8
9 10 11 12 13 14 15 hi! can someone help me with this output?? java.

Reply

Hello Santhosh!
check this code:
public class series{
    public static void main (String args[]){
        int ln = 4; //number of lines
        for(int i=1,p=1;i<=ln;i++){
            for(int j=0;j < i+i-1;j++,p++){
                System.out.print(p+" ");
            }
            System.out.println();
        }
    }
}

Reply

sir i want to print this pattern can you please help me?????????????????
1
2 3 4
5 6 7 8 9
8 7 6 5 4 3 2
1 2 3 4 5 6 7 8 9

Reply

sir please help me with this also pleaseeeeeeeeeeeeeeeee
Q: write a program to print the following
user input:mirror
required output is:miror

Reply

Hello Srinivasa!
Good pattern logic! here's the pattern code:
public class series{
    public static void main (String args[]){
        int p=1 , q = 9 , r=0 ;
        for( int i=1 , k=0 ; i<=5 ; i++ , k++ ){
            for( int j=1 ; j<=i+k ; j++ ){
                if(r==0){
                    System.out.print(p+" ");
                    ++p;
                    if(i==3 && j==i+k){
                        r=1;
                        p=8;
                    }
                } else{
                    System.out.print(p+" ");
                    p--;
                    if(p==1){
                        r=0;
                        p=1;
                    }
                }
            }
            System.out.println();
        }
    }
}

Reply

Again a program with good logic :)
check this code:
public class series{
    public static void main (String args[]){
        String text = "mirror"; //Enter any text
        String newText = "" , check="";
        for(int i=0 ; i<text.length() ; i++){
            String temp = text.substring(i,i+1);
            if( !temp.equals(check) ){
                check = temp;
                newText = newText + temp;
            }   
        }
        System.out.println(newText);
    }
}

Reply

Thank you...............
Thank you very much sir........................

Reply

Nice to know our code worked well for you! Keep Visiting....
and If you have any other problem related to programming in future, feel free to contact us!

Reply

Can you help me write a code to print a triangle that has two different symbols?
Lets say I have

System.out.print("Size: ");
int size = input.nextInt();

and I read in a triangle with the size of 4.

Can you create a "for loop" to display a triangle with the "@" symbol but the middle line of the triangle to have only an "&" symbol?

and it doesnt matter what size the user inputs, the entire center column of the triangle will still produce "&". Thanks.

Reply

Can you help me write a code to print a triangle that has two different symbols?
Lets say I have

System.out.print("Size: ");
int size = input.nextInt();

and I read in a triangle with the size of 4.

Can you create a "for loop" to display a triangle with the "@" symbol but the middle line of the triangle to have only an "&" symbol?

and it doesnt matter what size the user inputs, the entire center column of the triangle will still produce "&". Thanks.

Reply

Hello Kyle!
You didn't mention the triangle pattern! means do you want the same pattern as in above article (positions 1st) with replaced by @ and &, as you said!

Reply

The below code will produce triangle pattern:
         &
      @&@
   @@&@@
@@@&@@@

Source Code :
import java.io.*;
public class series {
    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!");
        }
        //pattern logic starts here
            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++){
                    if(k==i){
                        System.out.print("&");
                    } else{
                        System.out.print("@");
                    }
                }
                for(int l=1;l<i;l++){
                    System.out.print("@");
                }
                System.out.println();
            }
    }
}


If this is not the pattern you was asking for than print the desired pattern!

Reply

can u please help me to print below pattern

Print the Pattern
input N=4
output :
4444444
4333334
4322234
4321234
4322234
4333334
4444444

Reply

please give a code for this
***********
*********
*******
*****
***
*

Reply

at each line two stars are reducing please give me a code for this

Reply

public class stars {


public static void main(String[] args) {

int c=1;
for(int i=1;i<=6;i++)
{
for(int j=i;j<6;j++)
{
System.out.print(" ");
}
for(int j1=1;j1<=c;j1++)
{
if(j1%2==2)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
c+=2;
this is the code and its working too but it is printing the stars from first to last i want to print it in the i gave you the code please make it fast its urgent..
thanku

Reply

Hello!
The pattern 2 of this article have the same pattern code... try that code

Reply

Check pattern 2 code!

Reply

i can't get it please type me a code for this pattern

Reply

public class series {
    public static void main ( String arg[] ){
        int ln = 6; //Number of lines
        for(int i=1,k=1;i<=ln;i++,k+=2){
            for(int s=ln-i;s<ln-1;s++)
                System.out.print(" ");  //Spaces
            for(int j=ln*2-1;j>=k;j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Reply

i want to print this pattern please help me sir.............
a
eio
uaeio
uaeioua
eiouaeiou

Reply

can you please help me to print this pattern.......
1
010
10101
0101010
101010101

Reply

Here's the Code:
public class series {
    public static void main ( String arg[] ){
        int ln = 5 , p=1;
        for(int i=0;i<ln;i++){
            for(int s=ln-1;s>i;s--){
                System.out.print("*");
            }
            for(int j=0;j<=i*2;j++){
                System.out.print(p);
                p = (p==1)?0:1;
            }
            System.out.println();
        }
    }
}

Reply

Hello!
Here's the code, check this out:
public class series {
    public static void main ( String arg[] ){
        char c[] = {'a','e','i','o','u'};
        int ln = 5; 
        for(int i=0 , k=0 ; i<ln ; i++){
            for(int j=0 ; j<=i*2 ; j++,k++){
                System.out.print(c[k]);
                if(k==4) k=-1;  //reset value of k
            }
            System.out.println();
        }
    }

Reply

Thank you sir......
Thank you very much...........................

Reply

Nice to help you friend!
If you have any problem in future, fell free to contact us... Keep Visiting!

Reply

can you please help me with this program?????
Q:You have to give a sentence as an input and the output should be reverse of each word in that sentence but we should not use any predefined methods in the logic???????
for example:
input:this book is nice
output:siht koob si ecin

Reply

Nice Question, Check the code:
import java.io.*;
public class series {
    public static void main ( String arg[] ) throws IOException{
        InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(istream) ;
        System.out.print("Enter Text: ");
        String text = read.readLine();
        String words[] = text.split(" ");
        for(int i=0;i<words.length;i++){
            System.out.print(revTxt(words[i])+" ");
        }
    }
    public static String revTxt( String string){
        String rev="";
        for(int i=string.length();i>=1;i--){
            rev=rev+string.substring(i-1 , i);
            }
        return (rev);
    }
}

Reply

can you help me out in this program????????????
*
**
***
****
*****
****
***
**
*

Reply

Check this code Devanshu
public class series {
    public static void main ( String arg[] ){
        int ln=9;   //Number of lines/rows
        for(int i=1;i<=ln;i++){
        if(i<=ln/2+1)
            for(int j=1;j<=i;j++)   System.out.print("*");
        else
            for(int j=ln-i+1;j>0;j--)   System.out.print("*");
        
        System.out.println();   //New Line
    }
    }
}

Reply

Can you post that code? I'm having trouble setting it up. I'm looking for all of the triangles on one line with the program 3 triangles inside of the program 4 ones.

Reply

Hello Luke! We didn't get your question please explain clearly....
do you want to print pattern 3 and 4 both in one?

Reply

I would like to print both into one. The 4th program triangles on the outermost left and right and the 3rd program triangles on the innermost left and right. The result would be (left to right all on one line) 4th program left triangle, 3rd program left triangle, 3rd program right triangle, 4th program right triangle. Thank you!

Reply

Hello Sir.. How do I generate a random number with a set rang in java?

Reply

**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
How to print this pattern?

Reply

pls can you help for coding... without using scanner

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

Reply

Check this code:
public class series{
    public static void main(String args[]) {
        int ln = 4;    //Number of lines/rows
        for(int i=1; i<=ln; i++){
            //Spacing Logic
            for(int s=i;s<=ln;s++){
                System.out.print("  ");
            }
            for(int j=1,p=i;j<=i;j++,p++){
                System.out.print(p+" ");
                if(j==i){
                    p--;
                    for(int k=1;k<i;k++,--p){
                        System.out.print(p+" ");
                    }
                }
            }
            System.out.println();
        }
    }
}

Reply

how to this pattern sir ?(using for loops,if loops)
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

Reply

how to print this pattern sir ca you help me?
1
2 1
3 2 1
4 3 2 1

and this pattern also
4 3 2 1
3 2 1
2 1
1

Reply

Yes we will help you to solve these patterns!
Source Code for the first pattern:
public class series {
    public static void main (String args[]){
        for(int i=1;i<=4;i++){
            for(int j=i;j>=1;j--){
                System.out.print(j+" ");
            }
        System.out.println();
        }
    }
}


Source Code for the second pattern:
public class series {
    public static void main (String args[]){
        for(int i=0;i<4;i++){
            for(int j=4-i;j>=1;j--){
                 System.out.print(j+" ");
            }
        System.out.println();
        }
    }
}

Reply

Hello!
for this pattern check this code:
public class series {
    public static void main (String args[]){
        for(int i=0;i<4;i++){
            for(int j=1;j<=4-i;j++){
                 System.out.print(j+" ");
            }
            for(int j=4-i;j>1;j--){
                 System.out.print(j-1+" ");
            }
        System.out.println();
        }
    }
}

Reply

sir, i need pattern for this code?(using for loops):
1 2 3 4
1 2 3
12
1

Reply

Thank you sir, for your kind response.

Reply

This pattern code is available here: Creating Number Patterns In Java Pattern 3

Reply

543212345
_4321234
__32123
____212
____1
plz help

Reply

Hope this is what you want!
public class series {
    public static void main ( String arg[] ){
        int ln=5;
        for(int i=0;i<ln;i++){
            for(int s=1;s<=i;s++)    System.out.print(" ");
            for(int j=ln-i;j>0;j--)  System.out.print(j);
            for(int j=2;j<=ln-i;j++) System.out.print(j);
            System.out.println();
        }
    }
}

Reply

please help with this sir...
a
a a
a a a
a a a a
a a a
a a
a

Reply

Hello Santhosh!
for the above patter, this is the code:

public class series {
    public static void main ( String arg[] ){
        int ln=4;
        for(int i=0;i<ln*2-1;i++){
            if(i<ln){
                for(int j=0;j<=i;j++)   System.out.print("a ");
            } else{
                for(int j=ln-1;j>i-ln;j--)   System.out.print("a ");
            }
            System.out.println();
        }
    }
}

Reply

Hello!
same pattern solved at our number patten post so check that: Click Here

Reply

Hello Sir..!!
Can you plz help me for the following program.
* For a given String the output has to be separated by the comma in java lang.
Ex: Input:- apple Output:- a,p,p,l,e

Reply

@Shweta
for your program check the following code:
public class series {
    public static void main ( String arg[] ){
        String txt = "apple";
        for(int i=0;i<txt.length();i++){
            String temp = txt.substring(i,i+1);
            System.out.print(temp+",");
        }
    }
}

Reply

Thank you Very much for your reply sir..!!
Its very simple.. You Really Rock..!!!!

Reply

Glad to know our code worked well for you!
Thanks for your kind words... Keep visiting and happy coding :)

Reply

Can you please help with this one -


XXXXXXXXX
-X- - - - -X
--X - - - X
- - -X - X
- - - -X

The dashes are the spaces due to the pattern not coming properly...(SUBHRANIL DAS) posted this yesterday !

My code is -


But there's some problem with it !


public class abcd
{
public static void main(int x)
{
int i,j,k,l, m, temp=(x-4);
for(i=1;i<=x;i++)
{
System.out.print("X");
}
System.out.println();
for(l=1;l>=(x/2);l--)
{
for(j=1;j<=l;j++)
{
System.out.print(" ");
}
System.out.print("X");
}
for(m=(x-6);m>=1;m--)
{
for (k=temp;k<=m;k--)
{
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
}

Reply

public class series {
    public static void main ( String arg[] ){
        String txt = "COMPUTER";
        for(int i=0;i<txt.length();i++){
            for(int j=0;j<=i;j++)
                System.out.print(txt.substring(j, j+1)+" ");
            System.out.println();
        }
    }
}

Reply

Hello Sir Can u help me for getting this output.
_______________1
_____________2 3 2
___________3 4 5 4 3
_________4 5 6 7 6 5 4
_______5 6 7 8 9 8 7 6 5
_____6 7 8 9 0 1 0 9 8 7 6
___7 8 9 0 1 2 3 2 1 0 9 8 7
_8 9 0 1 2 3 4 5 4 3 2 1 0 9 8

Reply

impot java.util.Scanner;
class code{
public static void main(String [] args){
int b;
for(int i=1; i<=8; i++){
for(int s=i;s<=8;s++){
System.out.print(" ");
}
for(int j=1,p=i;j<=i;j++,p++){
b=p%10;
System.out.print(b+" ");
if(j==i){
p--;
for(int k=1;k<i;k++,--p){
b=p%10;
System.out.print(b+" ");
}
}
}
System.out.println();
}
}
}


good luck

Reply

sir can u help me in this
Take as input an array of integers. Print the array in ascending and descending order.

Input:
1st line will contain the number of elements in the array (n)
Next n lines will contain the elements of the array.

Output:
1st line will contain elements of the array sorted in ascending order, with one space between each element.
2nd line will contain elements of the array sorted in descending order, with one space between each element.

For Example
Input:
5
10 54 23 56 85

Output:
10 23 54 56 85
85 56 54 23 10

Reply

Hello Sir Can u help me for getting this output.
_______________1
_____________2 3 2
___________3 4 5 4 3
_________4 5 6 7 6 5 4
_______5 6 7 8 9 8 7 6 5
_____6 7 8 9 0 1 0 9 8 7 6
___7 8 9 0 1 2 3 2 1 0 9 8 7
_8 9 0 1 2 3 4 5 4 3 2 1 0 9 8

Reply

please can u help me to obtain
*****
***
*
and
*********
*** ***
** **
* *
** **
*** ***
********
and for solid circle and hallow too.

Reply

second one with atacthing right side too like a diamond space between *'s

Reply

Hello ashwin..!!
Here is the code ::

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Arrays1 {
public static void main(String[] args) {
System.out.println("Enter the Size of the Array");
Scanner scan = new Scanner(System.in) ;
int size = scan.nextInt();
Integer[] array = new Integer[size];
for(int i=0;i<array.length;i++){
System.out.print("Enter index "+i+" value");
array[i] = scan.nextInt();
}
System.out.println("--Ascending order--");
Arrays.sort(array);

for(int k:array){
System.out.print(" "+k);
}
System.out.println("--Descending order--");
Arrays.sort(array, Collections.reverseOrder());
for(int j:array){
System.out.print(" "+j);
}
}

}

Good Luck.!!

Reply

Plz help me to print this pattern
@
@*@
@*@*@
@*@*@*@

Reply

Can you please do a pattern of a string? Like :
L
L O
L O T
L O T U
L O T U S
The number of spaces keep on decreasing, that is 5 4 3 2 and then 1.
Please help.

Reply

Hello Potporri..
here is the code for you...

class PatternForPorpourri
{
  public static void main(String h[])
  {
      String s="LOTUS"; //initialize the string
      int n=s.length();//get the length
      int j;
      for(int i=0;i<n;i++)
      {
          for(j=0;j<=i;j++)
          System.out.println(s.charAt(j)+" ");
          System.out.println();
       }
    }
}

Visit Again...

Reply

i need help with this pattern in java (netbeans)
*
***
*****
*******
*********

Reply

can you please help me with this code as servelet
1
01
010
1010
10101
010101

Reply

for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println( )'
}
}

Reply

for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0)
{
System.out.print('*");
}
else
{
System.out.print("@");
}
System.out.println( );
}
}

Reply

0
1 1
2 3 5
8 13 21 34
its the fibonacci pattrn i want the code for it
please fast..........

Reply

Hello!
Here's the easy logic for this pattern,


int a=1,b=0,c=0;
for(int i=0;i<4;i++){
    for(int j=0;j<=i;j++){
        System.out.print(c);
        a=b; b=c;
    }
    System.out.println();
}

Reply

Hi
Check the code given below:

public class series{
    public static void main (String args[]){
        int ln = 6;    //change this accordingly
        int print = 1; //var. with value 0 & 1
        for(int i=1;i<=ln;i++){
            for(int j=1;j<=i;j++){
                System.out.print(print);
                print = (print==0)?1:0; //change value-> 1 if 0 and 0 if 1
            }
            System.out.println();       //New line
        }
    }
}

Reply

package com.test;

public class Pattern1 {

public static void main(String[] args) {
int c=0;
for (int i =1;i<5;i++) {
for (int j =5;j>i;j--) {
System.out.print(" ");
}
for (int j =1; j <=i; j++) {
System.out.print(++c);
}

for (int j =1; j <i; j++) {
System.out.print(--c);
}
System.out.println();
}
}
}

Reply

package com.test;

public class Pattern1 {
public static void main(String[] args) {
int c=0;
for (int i =1;i<5;i++) {
for (int j =5;j>i;j--) {
System.out.print(" ");
}
for (int j =1; j <=i; j++) {
System.out.print(++c);
}

for (int j =1; j <i; j++) {
System.out.print(--c);
}
System.out.println();
}
}
}

Reply

algorithm fr right angle triangle

Reply

These kinds of pattern are printed like this:

public class series{
    public static void main (String args[]){
        int row = 3;    //change the rows
        for(int i=0,p=1;i<row;i++){
            for(int j=0;j<=i;j++,p++){
                System.out.print(p);
                if(j+1<=i) System.out.print("*");
            }
            System.out.println();
        }
    }
}

Reply

There's no specific logic, check this code...
'
public class series{
    public static void main (String args[]){
        int height = 5;         //change to increase height
        int perpendicular = 1;  //change to increase perpendicular
        for(int i=0; i<height; i++){
            for(int j=0; j<=i*perpendicular; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Reply

can u plz help me for this pattern...
if x=4 then output will be 1/11 + 2/21 +3/31 + 4/41 +........&so on for different values of x

Reply

1
121
12321
1234321
plz help me sir for this pattern

Reply

I hope you just want the pattern not the resultant of the series! In case needed just create another float variable and add series inside the loop. by the way check the code for your series:

public class series{
    public static void main (String args[]){
        int x = 4;
        for(int i=1;i<=x;i++){
            System.out.print( i + "/" + (i*10+1) );
            if(i+1<=x)  System.out.print(" + ");
        }
    }
}

Reply

For this pattern check this:

class series {
    public static void main(String Args[]) {
        int max = 4;
        for(int i=1;i<=max;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j);
            }
            for(int k=i-1;k>=1;k--){
                System.out.print(k);
            }
            System.out.println();
        }
    }
}

Reply

Write a program that allows you to display a number in Second, Display is equivalent in Hour: Minute: Second

ex. Enter a value in Second: 4000

Note: 4000 second is equivalent to 1 hour 6 minutes and 40 seconds.

The equivalent is : 1 hour 6 minutes and 40 seconds.

how can you do this using scanner

Reply

For the question you ask at Number pattern post, since there's some problem with our comment system so I am replying that here too....
here's for your other question (continues input till 0 program):

import java.util.Scanner;

class series {
    public static void main(String Args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Numbers, 0 to terminate!\n");
        int num , count , sum=0;
        float avg;
        num = scan.nextInt();
        for(count=0;num!=0;count++){
            sum+=num;
            num = scan.nextInt();
        }
        avg = (float)sum/count;
        System.out.print("Numbers Entered:"+count+"\n"
                        +"Sum: "+sum+"\n"
                        +"Average:"+avg
                        );
    }
}

Reply

And for this time conversion problem:

import java.util.Scanner;

class series {
    public static void main(String Args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Seconds:");
        int seconds = scan.nextInt();
        int hr = seconds/3600;
        int rem = seconds%3600;
        int mn = rem/60;
        int sec = rem%60;
        String hrs = (hr<10 ? "0" : "")+hr;
        String mins = (mn<10 ? "0" : "")+mn;
        String secs = (sec<10 ? "0" : "")+sec; 
        System.out.print(hrs+" Hours "+mins+" Minutes "+secs+" Seconds.");
        
    }
}

Reply

your code helped me alot thank you sir

Reply

can anyone plzz help me for this pattern

- - - 1 - - -
- - 2 - 2 - -
- 3 - 3 - 3 -
4 - 4 - 4 - 4

Reply

kk frndzz i have got it


class Series
{
public static void main(String args[])
{
int c=1;
for(int i=1;i<5;i++)
{
for(int j=5;j>=i;j--)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{

System.out.print(c+" ");
}c++;

System.out.println();
}
}
}

Reply

please can anyone tell me how to wrrite java program for this
*
* *
* * *
* * * *

Reply

if possible, then please tell me before 8 August 2014. I am an engineering student. I have to submit java lab file on 8 August 2014

Reply

write the code for below pattern using for loops

ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
pls as soon as posable

Reply

Utkarsh Sinha


public class PyramidStar
{

public static void main(String[] args)
{
for (int i=0; i<=4; i++)
{
//outer for loop
for (int j=1; j<=i; j++)
//inner for loop
{
System.out.print("*");
}
System.out.println();

}

}
}

Reply

please help me with this program.
WAP in Java that reads a positive integer from the user and displays its word format. Example – if the input number is 1234 then the output is One two three four. Make use of 1D array in this program

Reply

int x,y;

for(x=1;x<6;x++)
{
for(y=0;y<x;y++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Reply

WAP to accept the values of a matrix and calculate and print its determinant

Reply

how to print this shape:

************************
*--*---------------------*--*
*----*-----------------*----*
*------*-------------*------*
*--------*---------*--------*
*----------*-----*----------*
*------------*-*------------*
*---------------------------*
*------------*-*------------*
*----------*-----*----------*
*--------*---------*--------*
*------*-------------*------*
*----*-----------------*----*
*--*---------------------*--*
************************

Reply

1
1 2
1 2 3

1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4

Reply

how to print below pattern

54321
4321
321
21
1

Reply

code for
a
0 b
1 c 2
d 3 e 4
f 5 g 6 h
f 5 g 6 h
d 3 e 4
1 c 2
0 b
a

Reply

Sir please tell me code for these two patterns

1)
*
* *
* * *
* * * *
* * * * *


2)

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Reply

the 1st pattern in equilateral triangle shape sir. It cant come as original shape in comment...please observe it.

Reply

I think you didn't noticed but the first pattern of this article is same as what you asking for.

Reply

i want the following output
suppose we have a string"pallavijadhav" then how to separate this string as "pallavi jadhav"

Reply

This's not possible.... How will you find out where first name ends and where to separate!
If you mean separating "PallaviJadhav" , that's possible. Do you want This?

Reply

ok sir give the answer for this

Reply

This program simply put a 'space' before every character which is Uppercase.
hope you got the logic! Check the code below:

public class series{
    public static void main(String args[]) {
        String name = "PallaviJadhav" , NAME = "";
        for(int i=0;i<name.length();i++){
            if( Character.isUpperCase(name.charAt(i)) )
                NAME += " ";
            NAME += name.charAt(i);
        }
        System.out.print(NAME.trim());
    }
}

Reply

thank you sir,you solved my problem thank you very much

Reply

Write a program in Java which will take an integer as input and print a diamond as shown below.
Input Specifications :
Input should be an integer n within the range 1-9 (including 1 and 9).
Output Specifications :
Output should be a diamond containing the numbers from 1 to n as shown in the example. If the input is out of range then the output should be "Invalid Input" .

Input :
5
Output :
55555
4444
333
22
1
22
333
4444
55555

Input :
1
Output :
1
Input :
20
Output :
Invalid Input

Reply

please give the code for pascal triangle in java

Reply

We already explained about pascal triangle in Java...
Visit Here: Pascals Triangle In Java

Reply

PLZ NEED CODE FOR THIS


ABCDEDCBA
ABCDCBA
ABCBA
ABA
A

Reply

Write the code to print the following pattern.
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1

Reply

sir there is space in 1 and 1
then again space in second line betwwen 1 2 2 1 like this n continue

Reply

same Question asked in tcs ignite

Reply

1 1
12 21
123 321
1234321
code for this pattern

Reply

the pattren which i want to design is not accepting on ur comment box..is thi sany way to sen u the pattern snapshot so i can send u snapshot of the pattern

Reply

give code for
1
22
333
4444
55555
this pattern
plz give it soon plz plz

Reply

sir can you please help me to convert binary to hexadecimal and hexadecimal to binary

Reply

Can you help to display this pattern

11 12 13 14 15
7 8 9 10
4 5 6
2 3
1

Reply

Can you help to display this pattern
1
21
321
4321

Reply

plz send code for

1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9

Reply

plz send code for
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9

Reply

can you help me to do this sir? in 1 code only?
[1] TRIANGLE [2] LOTTO [3] BINGO
[1] pattern 1 [1] 6/42 [1] regular
[2] pattern 2 [2] 6/45 [2] black out
[3] pattern 3 [3] 6/49 [0] back/exit
[0] back/exit [0] back/exit





Reply

can I have a program for below mentioned output?


t
e
n
d s
u a
l c
k h
a i
r n

Reply

How to print below pattern?
----K
---U
--M
-A
R

Reply

3 33 333 3333 33333
3 33 333 3333 33333
3 33 333 3333 33333
3 33 333 3333 33333
3 33 333 3333 33333

Reply

- - - X
- - X - X
- X - - - X
XXXXXXX

please tell me the logic
height of the triangle is inputted by the user.

Reply

Code for this pattern:

import java.util.*;

class Main1 {
  public static void main(String[] args){  
    Scanner in = new Scanner(System.in);
    int h=in.nextInt();    //height <- read input
    for(int i=0;i<h;i++){
        for(int s=1;s<h-i;s++)  //spacing
            System.out.print("-");
        for(int j=0;j<=i*2;j++){
            if(j==i*2 || j==0)
                System.out.print("X");  //first and last pos
            else if(i==h-1)
                System.out.print("X");  //last line
            else
                System.out.print("-");  //middle
        }
        System.out.println();   //new line
    }
   
  }
}

Reply

Hello Sir,

I want output like:

0
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890

Please help me on it.

Reply

Hi Help to solve the follwoing pattern
123417181920
--567141516
----891213
------1011

Reply

Can you help me to do this sir? Print this output

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Reply

package practice;
import java.io.*;

public class Triangle {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// TODO Auto-generated method stub
int i, n, c;

System.out.println("Enter the number of rows you wish to see in pascal triangle\n");
n=5;

for (i = 0; i < n; i++)
{

for (c = 0 ; c <= i; c++)
{
System.out.print(factorial(i)/(factorial(c)*factorial(i-c)));
}

System.out.println();
}

}

public static long factorial(int n)
{
int c;
long result = 1;

for (c = 1; c <= n; c++)
result = result*c;

return result;
}
}

Reply
This comment has been removed by the author.

Can you help me sir?? please , i am confused :/
input number : 8

1*************1
12###########21
123*********321
1234#######4321
12345*****54321
123456###654321
1234567*7654321
123456787654321

if odd print " * "
if even print "#"

help me please sir :/ i dont know ,i am newbie in java :/ thank you

Reply

Write a program to print the diagrams given below... I need all these Programs to be done by using Only TWO LOOPS... Not More than or Less than TWO LOOPS... Please Help as soon as possible... It's Urgent... I need the Complete Code...:
i)
*
***
*****
*******
*********

ii)
*********
*******
*****
***
*
iii)
*
***
*****
*******
*********
*******
*****
***
*

Reply

Write a program to print the diagrams given below... I need all these Programs to be done by using Only TWO LOOPS... Not More than or Less than TWO LOOPS... All are EQUILATERAL TRIANGLES...Please Help as soon as possible... It's Urgent... I need the Complete Code...:
i)
*
***
*****
*******
*********

ii)
*********
*******
*****
***
*
iii)
*
***
*****
*******
*********
*******
*****
***
*

Reply
This comment has been removed by the author.

HELLO!
can you help me guys to solve this!
my problem is:
the user prompts to input a number and it should be print like this:

ENTER NUMBER: 4
1
1 1
1 1
1 1
PLEASE HELP ME! THANK YOU!

Reply

*
*
* *
* *
* *
* *
* *
*
can you please help me with this pattern!

Reply

_____1
____21
___321
__4321
_54321
654321

this also;
123456
12345
1234
123
12
1

with the nested loops :) FOR, WHILE & DO WHILE :)
thankyou :)

Reply

please help me sir

Problem Statement:

Generate the following pattern for an input say 1110111 where equal number of 1's can be prefixed and suffixed where 0 must be at the middle position and the total number of digits must be odd.
1 1
1 1
1 1
0
1 1
1 1
1 1

Dos and Donts

Minimum no. of digits should be 3, say 101.
Maximum no. of digits should be 19.
Input should be read as long data type.
(Don't read the input as String type)

Reply

Please sir help me for this pattern
1
12
123
1234

Reply

hello sir
please tell me code for this pattern
1
2 4
3 6 9
4 8 12 16

Reply

1 2 3
4 5 6
7 8 9
In this matrix how can I find adjacent element means I/P:-5 then adjacent elements are 2,4,6,8
and if we take I/P :-7 then adjacent elements are 4,8
please give code for this in java

Reply

1 2 3
4 5 6
7 8 9
In this matrix how can I find adjacent element means I/P:-5 then adjacent elements are 2,4,6,8
and if we take I/P :-7 then adjacent elements are 4,8
please give code for this in java

Reply

1
3*2
4*5*6
10*9*8*7
11*12*13*14

please give code for this in java

Reply

WAP A PROGRAM TO DISPLAY THE PATTERN
S
SI
SIM
SIMP
SIMPL
SIMPLE
SIMPL
SIMP
SIM
SI
S

Reply

Sir, How can i print keyword W with * symbol.Need to set height and width for the same?

Reply
This comment has been removed by the author.

sir pls help me how to print W(*)

Reply

Sir can you please tell me the code to add numbers in a mixed string? for eg.{a,m,8,',89,s,l,9, ,w}

Reply

HOw to print following o/p in java
"1,2,3,4,5,8,7,16,9,32,11,64,13,128"

Reply

* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
plz tell me how to print that triangle using thread after certain time slot (500ms) ..first one triangle and then second one..

Reply

sir, i need pattern for this code?(using for loops):Help me
1 2 3 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1

Reply

Krishna Goudar
can u please help me to print below pattern
1
2 6
3 7 10
4 8 11 14

Reply

can you please help me to print this pattern
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
0 1 0 1 0 1

Reply

plz help me with the code of:-
654321
65432
6543
654
65
6
65
654
6543
65432
654321

Reply
This comment has been removed by the author.

input: n=7
ouput: 13
7 12 19
3 6 11 18 23
1 2 5 10 17 22 25
4 8 14 20 24
9 15 21
16

Reply

input: n=7
ouput:------------------------------------------13
-----------------------------------------------7 12 19
--------------------------------------------3 6 11 18 23
----------------------------------------1 2 5 10 17 22 25
---------------------------------------------4 8 14 20 24
------------------------------------------------9 15 21
----------------------------------------------------16

Reply

I want to print this pattern

1
2 3
4 5 6
7 8 9 10 ..N *

Reply

hi sir
how to print the output


*
*
* * * * *
*
*

Reply
This comment has been removed by the author.

import java.io.*;
public class Pat_tri {
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(" ");
}
}
System.out.println();
}
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(" ");
}
}
System.out.println();
}
for(int i=1;i<=num;i++){
for(int j=1;j<i;j++)
System.out.print(" ");
for(int k=1;k<=num-(i-1);k++)
System.out.print("*");
System.out.println();
}
for(int i=1;i<=num;i++){
for(int j=1;j<num-(i-1);j++)
System.out.print(" ");
for(int k=num-i;k<num;k++)
System.out.print("*");
System.out.println();
}
}
}

Reply

Can u help with me this???

123456789
12345678
1234567
123456
12345
1234
123
12

Reply

Can u help with me this???

123456789
--12345678
----1234567
------123456
--------12345
----------1234
------------123
--------------12

Reply

Can u help me???

123456789
--12345678
----1234567
------123456
--------12345
----------1234
------------123
--------------12

Reply

And this one too??

13579
--1357
----135
------13
--------1

Reply

1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
12 21
1 1

Reply

@
@@@
@@@@@
@@@@@@@
@@@@@
@@@
@

Reply

*
*
* * *
*
*


and

* *
* *
* * * *
* *
* *

Reply

Sir can you please help me out for this Java Program pattern:

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

Reply

and also this one pls:
0
12
345
6789
01234

Reply

public class TringlerNum {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

/*
* First loop n times execute the inner look m
* Second loop m number has to print
*/
int n=7, m=7, k, l=7;

for(int i=1;i<=n;i++,l--)
{
k=7;
System.out.println();
for(int j=1;j<=n;j++,k--)
{
if(j<l)
System.out.print(" ");
else
System.out.print(k +" ");

}
}
}

}

Reply

* * * *
* * * *
* * * *
* * * *
* * *

Reply

@@@1
@@12
@123
1234
any one help me how to do this using
for loop

Reply

public class help
{
void main()
{
for(int i = 1;i<=3;i++)
{
for(int j = 3;j >= i;j--)
{
System.out.print("@");
}
for(int j = 1;j<=i;j++)
{
System.out.print(j);

}
System.out.println();

}System.out.print("1" + "2" + "3" + "4");
}
}

Reply

can you help me doing this pattern of code?
*
**
* *
**
*

Reply

Sir, Plz help me
Q) Write a java program using String function and without using string function
i/p: my name is hari
o/p: hari eman si my
Thank you

Reply
This comment has been removed by the author.

1
121
12421
1248421
can you do this.............

Reply

Please provide code for this:
#
##
###
####
#####
######

Reply

sir i need code for the following program.please help me out
1
765
2
12345
3
1098765


Reply
«Oldest   ‹Older   1 – 200 of 352   Newer›   Newest»

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.