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 1 – 200 of 352 Newer› Newest»Great help!
ReplyThanks for sharing these Triangle patterns....
Keep Visiting!
Replythank you so much .it was very helpful
ReplyGlad to know you liked it! keep visiting and contact us if you have any other problem related to programming or computers.
ReplyCan these code be simpler. For example can we write "upside down" with two for loops?
ReplyHi Ravikiran ! Above upside down triangle pattern can't be printed using 2 for loops. We used 4 loops because:
Reply1. 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! :)
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
ReplyJust add 4th program loops below 3rd program!
Reply1
Reply1 2 3
4 5 6 7 8
9 10 11 12 13 14 15 hi! can someone help me with this output?? java.
Hello Santhosh!
Replycheck 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();
}
}
}
sir i want to print this pattern can you please help me?????????????????
Reply1
2 3 4
5 6 7 8 9
8 7 6 5 4 3 2
1 2 3 4 5 6 7 8 9
sir please help me with this also pleaseeeeeeeeeeeeeeeee
ReplyQ: write a program to print the following
user input:mirror
required output is:miror
Hello Srinivasa!
ReplyGood 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();
}
}
}
Again a program with good logic :)
Replycheck 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);
}
}
Thank you...............
ReplyThank you very much sir........................
Nice to know our code worked well for you! Keep Visiting....
Replyand If you have any other problem related to programming in future, feel free to contact us!
Can you help me write a code to print a triangle that has two different symbols?
ReplyLets 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.
Can you help me write a code to print a triangle that has two different symbols?
ReplyLets 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.
Hello Kyle!
ReplyYou 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!
The below code will produce triangle pattern:
Reply&
@&@
@@&@@
@@@&@@@
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!
can u please help me to print below pattern
ReplyPrint the Pattern
input N=4
output :
4444444
4333334
4322234
4321234
4322234
4333334
4444444
please give a code for this
Reply***********
*********
*******
*****
***
*
at each line two stars are reducing please give me a code for this
Replypublic class stars {
Replypublic 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
Hello!
ReplyThe pattern 2 of this article have the same pattern code... try that code
Check pattern 2 code!
Replywhere ?
Replyi can't get it please type me a code for this pattern
Replypublic class series {
Replypublic 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();
}
}
}
i want to print this pattern please help me sir.............
Replya
eio
uaeio
uaeioua
eiouaeiou
can you please help me to print this pattern.......
Reply1
010
10101
0101010
101010101
Here's the Code:
Replypublic 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();
}
}
}
Hello!
ReplyHere'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();
}
}
}
Thank you sir......
ReplyThank you very much...........................
Nice to help you friend!
ReplyIf you have any problem in future, fell free to contact us... Keep Visiting!
can you please help me with this program?????
ReplyQ: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
Nice Question, Check the code:
Replyimport 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);
}
}
can you help me out in this program????????????
Reply*
**
***
****
*****
****
***
**
*
Check this code Devanshu
Replypublic 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
}
}
}
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.
ReplyHello Luke! We didn't get your question please explain clearly....
Replydo you want to print pattern 3 and 4 both in one?
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!
ReplyHello Sir.. How do I generate a random number with a set rang in java?
Reply**********
Reply**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
How to print this pattern?
pls can you help for coding... without using scanner
Reply1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
Check this code:
Replypublic 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();
}
}
}
how to this pattern sir ?(using for loops,if loops)
Reply1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
how to print this pattern sir ca you help me?
Reply1
2 1
3 2 1
4 3 2 1
and this pattern also
4 3 2 1
3 2 1
2 1
1
Yes we will help you to solve these patterns!
ReplySource 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();
}
}
}
Hello!
Replyfor 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();
}
}
}
sir, i need pattern for this code?(using for loops):
Reply1 2 3 4
1 2 3
12
1
Thank you sir, for your kind response.
ReplyThis pattern code is available here: Creating Number Patterns In Java Pattern 3
Reply543212345
Reply_4321234
__32123
____212
____1
plz help
Hope this is what you want!
Replypublic 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();
}
}
}
please help with this sir...
Replya
a a
a a a
a a a a
a a a
a a
a
Hello Santhosh!
Replyfor 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();
}
}
}
How to print?
Reply1
01
101
0101
10101
pls
Hello!
Replysame pattern solved at our number patten post so check that: Click Here
Hello Sir..!!
ReplyCan 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
@Shweta
Replyfor 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+",");
}
}
}
Thank you Very much for your reply sir..!!
ReplyIts very simple.. You Really Rock..!!!!
Glad to know our code worked well for you!
ReplyThanks for your kind words... Keep visiting and happy coding :)
Can you please help with this one -
ReplyXXXXXXXXX
-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();
}
}
}
public class series {
Replypublic 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();
}
}
}
Hello Sir Can u help me for getting this output.
Reply_______________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
impot java.util.Scanner;
Replyclass 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
Thank u...
Replysir can u help me in this
ReplyTake 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
Hello Sir Can u help me for getting this output.
Reply_______________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
please can u help me to obtain
Reply*****
***
*
and
*********
*** ***
** **
* *
** **
*** ***
********
and for solid circle and hallow too.
second one with atacthing right side too like a diamond space between *'s
ReplyHello ashwin..!!
ReplyHere 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.!!
Plz help me to print this pattern
Reply@
@*@
@*@*@
@*@*@*@
Can you please do a pattern of a string? Like :
ReplyL
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.
Hello Potporri..
Replyhere 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...
i need help with this pattern in java (netbeans)
Reply*
***
*****
*******
*********
can you please help me with this code as servelet
Reply1
01
010
1010
10101
010101
for(i=1;i<=9;i++)
Reply{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println( )'
}
}
for(i=1;i<=4;i++)
Reply{
for(j=1;j<=i;j++)
{
if(j%2==0)
{
System.out.print('*");
}
else
{
System.out.print("@");
}
System.out.println( );
}
}
0
Reply1 1
2 3 5
8 13 21 34
its the fibonacci pattrn i want the code for it
please fast..........
Hello!
ReplyHere'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();
}
Hi
ReplyCheck 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
}
}
}
thank you soo much sir
Replypackage com.test;
Replypublic 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();
}
}
}
package com.test;
Replypublic 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();
}
}
}
1
Reply2*3
4*5*6
4*5*6
2*3
1
algorithm fr right angle triangle
ReplyThese kinds of pattern are printed like this:
Replypublic 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();
}
}
}
There's no specific logic, check this code...
Reply'
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();
}
}
}
can u plz help me for this pattern...
Replyif x=4 then output will be 1/11 + 2/21 +3/31 + 4/41 +........&so on for different values of x
1
Reply121
12321
1234321
plz help me sir for this pattern
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:
Replypublic 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(" + ");
}
}
}
For this pattern check this:
Replyclass 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();
}
}
}
Write a program that allows you to display a number in Second, Display is equivalent in Hour: Minute: Second
Replyex. 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
help me asap
ReplyFor the question you ask at Number pattern post, since there's some problem with our comment system so I am replying that here too....
Replyhere'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
);
}
}
And for this time conversion problem:
Replyimport 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.");
}
}
your code helped me alot thank you sir
Replycan anyone plzz help me for this pattern
Reply- - - 1 - - -
- - 2 - 2 - -
- 3 - 3 - 3 -
4 - 4 - 4 - 4
kk frndzz i have got it
Replyclass 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();
}
}
}
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
Replywrite the code for below pattern using for loops
ReplyABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
pls as soon as posable
Utkarsh Sinha
Replypublic 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();
}
}
}
please help me with this program.
ReplyWAP 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
int x,y;
Replyfor(x=1;x<6;x++)
{
for(y=0;y<x;y++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
WAP to accept the values of a matrix and calculate and print its determinant
Replyhow to print this shape:
Reply************************
*--*---------------------*--*
*----*-----------------*----*
*------*-------------*------*
*--------*---------*--------*
*----------*-----*----------*
*------------*-*------------*
*---------------------------*
*------------*-*------------*
*----------*-----*----------*
*--------*---------*--------*
*------*-------------*------*
*----*-----------------*----*
*--*---------------------*--*
************************
1
Reply1 2
1 2 3
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
1
Reply01
101
0101
10101
how to print below pattern
Reply54321
4321
321
21
1
code for
Replya
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
Sir please tell me code for these two patterns
Reply1)
*
* *
* * *
* * * *
* * * * *
2)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
the 1st pattern in equilateral triangle shape sir. It cant come as original shape in comment...please observe it.
ReplyI think you didn't noticed but the first pattern of this article is same as what you asking for.
Replyi want the following output
Replysuppose we have a string"pallavijadhav" then how to separate this string as "pallavi jadhav"
This's not possible.... How will you find out where first name ends and where to separate!
ReplyIf you mean separating "PallaviJadhav" , that's possible. Do you want This?
ok sir give the answer for this
ReplyThis program simply put a 'space' before every character which is Uppercase.
Replyhope 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());
}
}
thank you sir,you solved my problem thank you very much
ReplyWrite a program in Java which will take an integer as input and print a diamond as shown below.
ReplyInput 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
please give the code for pascal triangle in java
ReplyWe already explained about pascal triangle in Java...
ReplyVisit Here: Pascals Triangle In Java
PLZ NEED CODE FOR THIS
ReplyABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Write the code to print the following pattern.
Reply1 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
sir there is space in 1 and 1
Replythen again space in second line betwwen 1 2 2 1 like this n continue
same Question asked in tcs ignite
Reply1 1
Reply12 21
123 321
1234321
code for this pattern
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
Replygive code for
Reply1
22
333
4444
55555
this pattern
plz give it soon plz plz
sir can you please help me to convert binary to hexadecimal and hexadecimal to binary
ReplyCan you help to display this pattern
Reply11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Can you help to display this pattern
Reply1
21
321
4321
plz send code for
Reply1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
plz send code for
Reply1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
can you help me to do this sir? in 1 code only?
Reply[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
can I have a program for below mentioned output?
Replyt
e
n
d s
u a
l c
k h
a i
r n
How to print below pattern?
Reply----K
---U
--M
-A
R
3 33 333 3333 33333
Reply3 33 333 3333 33333
3 33 333 3333 33333
3 33 333 3333 33333
3 33 333 3333 33333
- - - X
Reply- - X - X
- X - - - X
XXXXXXX
please tell me the logic
height of the triangle is inputted by the user.
Code for this pattern:
Replyimport 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
}
}
}
Hello Sir,
ReplyI want output like:
0
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
Please help me on it.
Hi Help to solve the follwoing pattern
Reply123417181920
--567141516
----891213
------1011
Can you help me to do this sir? Print this output
Reply1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
package practice;
Replyimport 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;
}
}
Can you help me sir?? please , i am confused :/
Replyinput 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
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...:
Replyi)
*
***
*****
*******
*********
ii)
*********
*******
*****
***
*
iii)
*
***
*****
*******
*********
*******
*****
***
*
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...:
Replyi)
*
***
*****
*******
*********
ii)
*********
*******
*****
***
*
iii)
*
***
*****
*******
*********
*******
*****
***
*
HELLO!
Replycan 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!
1
Reply12
123
1234
123
12
1
_____1
Reply____21
___321
__4321
_54321
654321
this also;
123456
12345
1234
123
12
1
with the nested loops :) FOR, WHILE & DO WHILE :)
thankyou :)
please help me sir
ReplyProblem 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)
Please sir help me for this pattern
Reply1
12
123
1234
hello sir
Replyplease tell me code for this pattern
1
2 4
3 6 9
4 8 12 16
1 2 3
Reply4 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
1 2 3
Reply4 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
1
Reply3*2
4*5*6
10*9*8*7
11*12*13*14
please give code for this in java
WAP A PROGRAM TO DISPLAY THE PATTERN
ReplyS
SI
SIM
SIMP
SIMPL
SIMPLE
SIMPL
SIMP
SIM
SI
S
Sir, How can i print keyword W with * symbol.Need to set height and width for the same?
ReplyCan u help me?
Replysir pls help me how to print W(*)
ReplySir can you please tell me the code to add numbers in a mixed string? for eg.{a,m,8,',89,s,l,9, ,w}
ReplyHOw to print following o/p in java
Reply"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..
sir, i need pattern for this code?(using for loops):Help me
Reply1 2 3 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
Krishna Goudar
Replycan u please help me to print below pattern
1
2 6
3 7 10
4 8 11 14
can you please help me to print this pattern
Reply1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
0 1 0 1 0 1
plz help me with the code of:-
Reply654321
65432
6543
654
65
6
65
654
6543
65432
654321
input: n=7
Replyouput: 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
input: n=7
Replyouput:------------------------------------------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
I want to print this pattern
Reply1
2 3
4 5 6
7 8 9 10 ..N *
hi sir
Replyhow to print the output
*
*
* * * * *
*
*
plz help me
Reply1 2 3
4 5 6
7 8 9
import java.io.*;
Replypublic 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();
}
}
}
Can u help with me this???
Reply123456789
12345678
1234567
123456
12345
1234
123
12
Can u help with me this???
Reply123456789
--12345678
----1234567
------123456
--------12345
----------1234
------------123
--------------12
Can u help me???
Reply123456789
--12345678
----1234567
------123456
--------12345
----------1234
------------123
--------------12
And this one too??
Reply13579
--1357
----135
------13
--------1
1 2 3 4 5 4 3 2 1
Reply1 2 3 4 4 3 2 1
1 2 3 3 2 1
12 21
1 1
@
Reply@@@
@@@@@
@@@@@@@
@@@@@
@@@
@
*
Reply*
* * *
*
*
and
* *
* *
* * * *
* *
* *
Sir can you please help me out for this Java Program pattern:
ReplyA
ABA
ABCBA
ABCDCBA
ABCDEDCBA
and also this one pls:
Reply0
12
345
6789
01234
public class TringlerNum {
Reply/**
* @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* * * *
* * * *
* * * *
* * *
@@@1
Reply@@12
@123
1234
any one help me how to do this using
for loop
public class help
Reply{
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");
}
}
can you help me doing this pattern of code?
Reply*
**
* *
**
*
Sir, Plz help me
ReplyQ) 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
1
Reply121
12421
1248421
can you do this.............
Please provide code for this:
Reply#
##
###
####
#####
######
sir i need code for the following program.please help me out
Reply1
765
2
12345
3
1098765
Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.