0% found this document useful (0 votes)
145 views5 pages

POP Using C - VTU Lab Program-4

The document describes a C program to read a number of rows as input and print numbers in ascending and descending order. It contains the algorithm, flowchart and code to implement the logic with input/output examples.

Uploaded by

vandana u
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
145 views5 pages

POP Using C - VTU Lab Program-4

The document describes a C program to read a number of rows as input and print numbers in ascending and descending order. It contains the algorithm, flowchart and code to implement the logic with input/output examples.

Uploaded by

vandana u
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 5

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

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


BPOPS103/203
Principles of Programming using C Lab

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

Flowchart:

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


BPOPS103/203
Principles of Programming using C Lab

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


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

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


BPOPS103/203
Principles of Programming using C Lab

12321
1234321

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

You might also like