Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Print Diamond Number Pattern Program Type 1
September 23, 2021
Pattern22
PRINTING PATTERN:
3
44
555
6666
555
44
3
PREREQUISITE:
Basic knowledge of C language and use of loops.
ALGORITHM:
Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
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
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++)
Inside this loop print i+3.
Else run a different loop from j=i to jThe loop should be structured as for( j=i ; j
Inside this loop print (r-i+2).
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; }
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);
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();
}
}
}
Very usefull pattern programmes for claring the amcat test