Creating number or symbol patterns is used to check command of a programmer or a student on for loop. In short its done just for practicing for loop.
Here are some common number patterns :
Here are some common number patterns :
public class numPtr { public static void main( String arg[]){ for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
Pattern 2.
To Print this pattern use the following code:
public class numPtr { public static void main( String arg[]){ for(int i=1;i<=5;i++){ for(int j=5;j>=i;j--){ System.out.print(j); } System.out.println(); } } }
Pattern 3.
To Print this pattern use the following code:
public class numPtr { public static void main( String arg[]){ for(int i=1,r=5;i<=5;i++,r--){ for(int j=1;j<=r;j++){ System.out.print(j); } System.out.println(); } } }
Pattern 4.
public class numPtr { public static void main( String arg[]){ int ck=0,c=2; while(c>0){ if(ck==0){ for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } ck++; } else{ for(int i=1,r=5-1;i<=5-1;i++,r--){ for(int j=1;j<=r;j++){ System.out.print(j); } System.out.println(); } } c--; } } }Pattern 5.
public class numPtr { public static void main( String arg[]){ int ck=0,c=2; while(c>0){ if(ck==0){ for(int i=1,r=5;i<=5;i++,r--){ for(int j=1;j<=r;j++){ System.out.print(j); } System.out.println(); } ck++; } else{ for(int i=2;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } c--; } } }Author Note: All the above examples gives output till 5, you can change it accordingly by changing the conditions.
1223 Comments Leave new
«Oldest ‹Older 1201 – 1223 of 1223 Newer› Newest»can you help me out to get the following logics?
Reply1
ABC
45678
DEFGHIJ
int n,i,j;
Replyprintf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==0)
{
if(j%2!=0)
{
printf("0");
}
else
printf("1");
}
if(i%2!=0)
{
if(j%2==0)
printf("0");
else
printf("1");
}
}
printf("\n");
}
please help me to code this:
Reply1
32
654
10987
1
Reply32
654
10987
how to make the following patterns :-
Replya) 1 2 3 4 5
4 5 6
7 8
15
b) 2
3 4
5 6 7
7 8 9 10
11 12 13 14 15
13 14 15 16 17 18
hi im kartik,pls tell this
Reply1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1
Reply2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
What will be logic for :
Reply0
1 2
3 4 5
6 7 8 9
please code for this
Reply123
45678
99876
54321
123
Write a Java Program to that prints the following output using single for loop.
Reply20 50
15 55
10 65
5 70
0 75
Pls help me in this one
can you help to get this output
Replyinput:7
output:
1010101
0101010
1010101
0101010
1010101
0101010
1010101
5
Reply5 10
5 10 15
Please give me answer
import java.util.*;
Replypublic class five {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(5*j+" ");
}
System.out.println();
}
}
}
Put n as number of lines. In your case, n=3.
import java.util.*;
Replypublic class five {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
for(int j=1;j<=n;j++)
{
if(j%2==0)
System.out.print("1");
else
System.out.print("0");
}
}
else
{
for(int j=1;j<=n;j++)
{
if(j%2==0)
System.out.print("0");
else
System.out.print("1");
}
}
System.out.println();
}
}
}
Hii Sir,
ReplyBelow program is giving me output like this:
5
53
531
but, I want output like this:
5
53
531
Syntax:
package Patterns;
public class Pttn5
{
public static void main(String[] args)
{
for (int i=1; i<=1; i++)
{
int m=3;
while(m>1)
{
int j=m-1;
{
System.out.print(" ");
}
m=m-1;
}
int a=4;
while(a>=0)
{
for (int k=5; k>=a; k--)
{
if (k%2==0)
{
System.out.print("");
}
else
{
System.out.print(k+"");
}
}
a=a-2;
System.out.println("");
}
}
}
}
So can you, pls help me to correct the error
I do not understand what you want and what are you looking for.
ReplyAlso, your code looks quite complex. I think it is unnecessarily complex for a simple program, using many loops.
Assuming I understood your program correctly, here is a much simpler version.
https://onlinegdb.com/rkBCT_VI7
public class Main
{
public static void main(String[] args) {
int m = 5;
for (int i=m; i>=1; i--)
{
for (int j=0; j<=m-i; j++)
System.out.print(2*(m-j)-1);
System.out.println();
}
}
}
Hi please solve this pattern
Reply1
2 9
3 10 16
4 11 17 22
5 12 18 23 27
Thankyou...
int a=1;
Replyint b =7;
int c=b;
For(int i =1;i<=5;i++)
{
a=i;
For(int j=1;j<=I;j++)
{
System.out.print(a+" ");
a=a+b;
b--;
}
b=c;
}
System.out.println();
}
If you have any doubt please do comment.
Thank you.
Thanks Nitish ... for your solution
ReplyI also tried with another solution ::
please check if it is correct or not
for (int i = 1; i <=5; i++) {
int result =i;
for (int j=1;j<=i;j++) {
System.out.print(result+" ");
result= result+8-j;
}
System.out.println();
}
Yes yours also right..
ReplyMake sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.