Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

BPOPS103/203 Principles of Programming using C Lab

4. Write a C Program to display the following by reading the number of rows as input.

Algorithm:

Step 1: START
Step 2 : Read the number of rows to be entered , say , ‘n’
Step 3: [For inputting the number of rows]
for i=1 to n
Step 4:[For printing spaces]
for j=1 to n-i
[Print the spaces]
printf(“ “)
Step 5: [Display numbers in the ascending order upto the middle]
for j=1 to i
print j
Step 6: [Display numbers in the reverse order after middle]
for j=i-1 to 1 (Decrement j)
print j
Step 7: STOP

Dept. of AI & DS, SMVITM, Bantakal Page 1


BPOPS103/203 Principles of Programming using C Lab

Flowchart:

Dept. of AI & DS, SMVITM, Bantakal Page 2


BPOPS103/203 Principles of Programming using C Lab

Program:
#include<stdio.h>
void main()
{
int i,j,n;
printf(“enter the number of rows\n”);
scanf("%d",&n);
/*for inputting the number of rows*/
for(i=1;i<=n; i++)
{
/*Printing blank spaces*/
for(j=1;j<=n-i; j++)
printf(" ");
/*Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
printf("%d",j);
/*Display number in reverse order after middle*/
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
}
Command to execute the Program:
$ gcc lab4.c -lm
$ ./a.out

Output:
enter the number of rows 4

1
121
12321
1234321

Dept. of AI & DS, SMVITM, Bantakal Page 3

You might also like