CSE1202 Structured Programming Lab ULAB
CSE1202 Structured Programming Lab ULAB
Lab Report 04
Submitted to:
Jannatul Ferdous Ruma
Lecturer
Department of Computer Science and Engineering (CSE)
ULAB School of Science & Engineering
University of Liberal Arts Bangladesh
Submitted by:
Name:
1|Page
Problem Statement-01:
Write a program to print the following series up to n terms where n is a user
input.
(1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) + ...........
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int t, i, sum = 0;
printf("Enter a range for the series: ");
scanf("%d", &t);
printf("The series is: ");
for(i = 1; i <= t; i++)
{
sum += i;
printf("%d + ", sum);
}
return 0;
}
Output Snippet:
2|Page
Algorithm:
Step 1: Start
Step 7: End
Conclusion:
This program takes a number from the user as a range and generates a series
where each term is the sum of all previous numbers. The program runs correctly
and prints the series as expected. However, the output format includes an extra
" + " at the end, which could be improved for better readability. Overall, the
program functions properly without errors.
Problem Statement-02(a):
Write separate programs to print the following patterns taking number of rows
as user input
(a)
*----
**---
***--
****-
*****
3|Page
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
printf("* ");
}
for(k = rows - i; k >= 1; k--)
{
printf("- ");
}
printf("\n");
}
return 0;
}
Output Snippet:
4|Page
Algorithm:
Step 1: Start
Step 2: Declare integer variables i, j, k, and rows.
Step 3: Ask the user to enter the number of rows.
Step 4: Read the input value and store it in the variable rows.
Step 5: Start a loop from i = 1 to rows:
Conclusion:
This program takes an input from the user to determine the number of rows and
then prints a pattern of stars (*) followed by dashes (-). The number of stars
increases in each row, while the number of dashes decreases. The program runs
correctly and displays the pattern as expected. If a valid number is entered, the
output is generated without errors.
Problem Statement-02(b):
Write separate programs to print the following patterns taking number of rows
as user input
(b)
a
ab
abc
abcd
abcde
5|Page
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
printf("%c ", j + 96);
}
printf("\n");
}
return 0;
}
Output Snippet:
6|Page
Algorithm:
Step 1: Start
Step 5: Use a loop (i) from 1 to rows to control the number of rows.
Step 6: Inside the first loop, use another loop (j) from 1 to i:
Step 9: End.
Conclusion:
Problem Statement-02(c):
Write separate programs to print the following patterns taking number of rows
as user input
(c)
1
23
456
7 8 9 10
7|Page
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, cnt = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d ", cnt);
cnt++;
}
printf("\n");
}
return 0;
}
Output Snippet:
8|Page
Algorithm:
Step 1: Start
Step 2: Declare integer variables i, j, rows, and cnt (initialize cnt to 1).
Step 4: Read the input value and store it in the rows variable.
Step 5.1: Use another for loop inside the first loop to iterate from j = 1
to i:
o Step 5.1.1: Print the value of cnt.
o Step 5.1.2: Increment cnt by 1.
Step 5.2: Print a new line after each row.
Step 6: End
Conclusion:
Problem Statement-03(a):
Write separate programs to print the following patterns taking number of rows
as user input
(a)
*****
****
***
**
*
9|Page
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 0; i <= rows; i++)
{
for(j = rows - i; j >= 1; j--)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output Snippet:
10 | P a g e
Algorithm:
Step 1: Start
Step 4: Read the input value and store it in the variable rows.
Step 5: Use a for loop with i starting from 0 and running up to rows:
Step 5.1: Inside the loop, use another for loop with j starting from
rows - i and decreasing to 1:
o Print * followed by a space.
Step 6: Print a new line after the inner loop to move to the next row.
Step 8: End
Conclusion:
Problem Statement-03(b):
Write separate programs to print the following patterns taking number of rows
as user input
(b)
1
123
12345
1234567
11 | P a g e
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k, rows, cols = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
printf(" ");
}
return 0;
}
Output Snippet:
12 | P a g e
Algorithm:
Step 1: Start
Step 4: Read the input value and store it in the rows variable.
Step 5: Use a loop (i from 1 to rows) to print each row of the pattern:
Step 5.1: Use another loop (j from 1 to rows - i) to print spaces before the
numbers.
Step 5.2: Use another loop (k from 1 to cols) to print numbers in
increasing order.
Step 5.3: Increase cols by 2 to expand the number sequence in the next
row.
Step 5.4: Print a newline to move to the next row.
Step 6: Stop.
Conclusion:
This program correctly prints a number pyramid pattern based on the number of
rows entered by the user. It first prints spaces to align the numbers in a pyramid
shape, then prints numbers in increasing order. The program runs successfully
and displays the expected output without errors.
13 | P a g e