Print Diamond Number Pattern Program Type 1

Pattern22

PRINTING PATTERN:

3

44

555

6666

555

44

3

PREREQUISITE:

Basic knowledge of C language and use of loops.

ALGORITHM:

  1. Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
  2. Run a loop ‘r’ number of times to iterate through each of the rows. From i=0 to i<r. The loop should be structured as for( i=0 ; i
  3. Use an if condition to to print the top half of the pyramid. if (i<=r/2). Then run a loop from j=0 to j<=i. The loop should be structured as for(j=0 ; j<=i ; j++)
  4. Inside this loop print i+3.
  5. Else run a different loop from j=i to jThe loop should be structured as for( j=i ; j
  6. Inside this loop print (r-i+2).
  7. Inside the main loop print a newline to move to the next line after each row is printed.

Input:
              3  4
Output:
               3
              44
              555
              6666
              555
              44
              3

Input :
              4  4
Output:
              4
              55
              666
              7777
              666
              55
              4

Code 1

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i,j,count=0;
int s=sc.nextInt();
int N=sc.nextInt();
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
System.out.print(i);
System.out.println();
i=i+1;
}

for(i=s+N-2;count>0;count--)
{
for(j=0;j<count-1;j++)
System.out.print(i);
System.out.println();
i=i-1;
}
}
}

2 comments on “Print Diamond Number Pattern Program Type 1”


  • harsh

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {

    int i,j,n;
    Scanner in= new Scanner(System.in);
    System.out.println(“enter no of rows: “);
    n= in.nextInt();
    int num=3;
    for(i=1;i<=n;i++)
    {
    for(j=1;j=1;i–)
    {
    for(j=1;j<=i;j++)
    {
    System.out.print(num-2);

    }
    num–;
    System.out.println();
    }
    }
    }