Homework 8

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Anika Tasnim

CSE115
ID : 2421540642

Question 1 : Write separate C programs to print the


following patterns for n lines (n is input ) using nested
loop.

Code:

#include <stdio.h>

void main()
{
int i,j,rows=5;
for(i=0; i<rows; i++)
{
for(j=0; j<i; j++ )
{
printf(" ");
}
for(j=0; j<rows; j++)
{
if(j==0 || j==rows-1-i || i==0)
{
printf("*");
}
else
{
printf(" ");
}

printf("\n");

}
Code:

#include <stdio.h>

void main()
{
int i,j,rows;

printf("Enter no. of rows:");


scanf("%d" ,&rows);

for (int i = 0; i<rows; i++)


{

for (j = rows-i-1; j > 0; j--)

{
printf(" ");
}

for (j = 0; j <= i; j++)

{
char letter = 'A' + i;

printf("%c ", letter);


}
printf("\n");
}

Code:

#include <stdio.h>

void main()
{
int i,j,rows,num=1;

printf("Enter no. of rows:");


scanf("%d", &rows);

for(i=1; i<=rows; i++)


{

for(j=1; j<=i; j++)


{
printf("%d" ,num);
num++;

if(num > 9)
{
num = 0;
}
}

printf("\n");

}
Code:

#include <stdio.h>

void main()
{

int i,j,rows,num=1;

printf("Enter no. of rows:");


scanf("%d", &rows);

for(i=1; i<=rows; i++)


{

for(j=rows; j>i; j--)


{
printf(" ");
}

for(j=1; j<=(2*i-1); j++)

{
printf("%d", num);
num++;

if(num > 9)
{
num = 0;
}
}

printf("\n");
}

Code:
#include <stdio.h>

void main()
{
int i,j,k,rows;

printf("Enter no. of rows:");


scanf("%d", &rows);

for(i=0; i<rows; i++)


{

for(j=i; j<rows; j++)


{

printf(" ");

}
for(k=0; k<=i; k++)
{

printf("%d",(k%2));

printf("\n");
}

}
Code:

#include <stdio.h>

void main()
{

int i,j,k,rows;

printf("Enter no. of rows:");


scanf("%d", &rows);
for(i=1; i<=rows; i++)
{

for(j=1; j<i; j++)


{

printf(" ");

}
for(k=1; k<=(rows*2)-(2*i-1); k++)
{

printf("*");
}

printf("\n");

}
Code:

#include<stdio.h>

void main()
{

int i,j,k,rows;

printf("Enter no. of rows:");


scanf("%d", &rows);

for(i=1; i<=rows;i++)
{

for(j=1; j<=i; j++)

printf("* ");

printf("\n");
}

for(i=rows-1; i>=1; i--)


{

for(j=1; j<=i; j++)


{

printf("* ");

printf("\n");

}
}
Code:

#include<stdio.h>

void main()
{

int i,j,k,rows;

printf("Enter no. of rows:");


scanf("%d", &rows);

for(i=1; i<=rows; i++)


{

for(j=rows; j>i; j--)


{

printf(" ");

}
for(k=1; k<=(2*i-1); k++)
{

printf("*");

printf("\n");

for(i=rows-1; i>=1; i--)


{

for(j=rows; j>i; j--)


{

printf(" ");
}

for(k=1; k<=(2*i-1); k++)


{

printf("*");

printf("\n");

You might also like