0% found this document useful (0 votes)
7 views

CSE1202 Structured Programming Lab ULAB

The document is a lab report for the Structured Programming Lab course, detailing various programming problems and their solutions. It includes code snippets, algorithms, and conclusions for each problem, focusing on generating series, patterns, and number sequences based on user input. The report is submitted to a lecturer at the University of Liberal Arts Bangladesh and includes a submission date.

Uploaded by

balihip991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CSE1202 Structured Programming Lab ULAB

The document is a lab report for the Structured Programming Lab course, detailing various programming problems and their solutions. It includes code snippets, algorithms, and conclusions for each problem, focusing on generating series, patterns, and number sequences based on user input. The report is submitted to a lecturer at the University of Liberal Arts Bangladesh and includes a submission date.

Uploaded by

balihip991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Course Code: CSE1202

Course Title: Structured Programming Lab


Section: 03
Semester: Spring 2025

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:

Submission Date: 19/03/2025

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 2: Declare integer variables t, i, and sum, and initialize sum to 0.

Step 3: Ask the user to enter a range for the series.

Step 4: Read the user input and store it in variable t.

Step 5: Print the message "The series is: ".

Step 6: Use a for loop to iterate from 1 to t:

 Step 6.1: Add the current value of i to sum.


 Step 6.2: Print the updated sum followed by " + ".

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:

Step 6: Start an inner loop from j = 1 to i:

 Print * (star) followed by a space.

Step 7: Start another loop from k = rows - i down to 1:

 Print - (dash) followed by a space.

Step 8: Print a new line to move to the next row.


Step 9: Repeat the process until all rows are printed.
Step 10: End the program.

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 2: Declare integer variables i, j, and rows.

Step 3: Ask the user to enter the number of rows.

Step 4: Read the input value and store it in rows.

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:

 Convert j to a lowercase letter using ASCII (j + 96).


 Print the character with a space.

Step 7: Move to a new line after printing each row.

Step 8: Repeat the process until all rows are printed.

Step 9: End.

Conclusion:

This program successfully prints a pattern of lowercase letters in a triangular


format based on the number of rows entered by the user. It uses nested loops to
generate and display characters using their ASCII values. The program runs
correctly and produces the expected output.

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 3: Ask the user to enter the number of rows.

Step 4: Read the input value and store it in the rows variable.

Step 5: Use a for loop to iterate from i = 1 to rows:

 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:

This program successfully prints a number pattern in a triangular format. It


starts from 1 and continues incrementing numbers in each row. The user enters
the number of rows, and the pattern is displayed accordingly. The program runs
correctly and provides the expected output. It correctly uses nested loops to
control the pattern structure and increments the number sequentially.

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 2: Declare integer variables i, j, 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: 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 7: Repeat Steps 5 and 6 until the pattern is completely printed.

Step 8: End

Conclusion:

The program successfully prints an inverted right-angled triangle pattern using


asterisks (*). It takes the number of rows as input from the user and correctly
decreases the number of asterisks in each row until it reaches one. The program
runs properly without errors and displays the expected output based on the
given input.

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(" ");
}

for(k = 1; k <= cols; k++)


{
printf("%d ", k);
}
cols += 2;
printf("\n");
}

return 0;
}

Output Snippet:

12 | P a g e
Algorithm:

Step 1: Start

Step 2: Declare variables i, j, k, rows, and cols, and initialize cols to 1.

Step 3: Ask the user to enter the number of rows.

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

You might also like