Using nested loops write program to generate the following pattern on the screen:
Answers
Solution.
Assuming that the language is Java, the code goes like -
public class Pattern{
public static void main(String s[]){
int rows=6,i,j;
for(i=0;i<rows;i++){
for(j=0;j<i;j++)
System.out.print(" ");
for(j=rows-i;j>=1;j--)
System.out.print(j);
for(j=2;j<=rows-i;j++)
System.out.print(j);
System.out.println();
}
}
}
Output.
65432123456
543212345
4321234
32123
212
1
See the attachment for verification.
Answer:
Program:-
public class Main
{
public static void main(String args[])
{
int c=6,d=6,e=0;
for(int i=1;i<=7;i++)
{
for(int p=0;p<=e;p++)
System.out.print(" ");
for(int j=c;j>=2;j--)
System.out.print(j);
for(int k=1;k<=d;k++)
System.out.print(k);
System.out.println();
c--;
d--;
e++;
}
}
}