0% found this document useful (0 votes)
2 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 in C. It includes code snippets, algorithms, and conclusions for each problem, covering topics such as summing even numbers, calculating factorials, and checking for prime numbers. The report is structured with problem statements, code, algorithms, and conclusions for clarity.

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)
2 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 in C. It includes code snippets, algorithms, and conclusions for each problem, covering topics such as summing even numbers, calculating factorials, and checking for prime numbers. The report is structured with problem statements, code, algorithms, and conclusions for clarity.

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/ 19

Course Code: CSE1202

Course Title: Structured Programming Lab


Section: 03
Semester: Spring 2025

Lab Report 03

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: 26/02/2025

1|Page
Problem Statement-01:
Write a program to input two integer numbers and display the sum of even
numbers between these two input numbers.

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, num1, num2, even_sum = 0;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
for(i = num1; i < num2; i++)
{
if(i % 2 == 0)
{
even_sum += i;
}
}
printf("The sum of even numbers between them is: %d", even_sum);

return 0;
}

Output Snippet:

2|Page
Algorithm:

Step 1: Start

Step 2: Declare integer variables i, num1, num2, and even_sum

(initialize even_sum to 0).

Step 3: Ask the user to enter two numbers and store them in num1 and num2.

Step 4: Use a for loop to iterate from num1 to num2 - 1.

Step 5: Inside the loop, check if the current number i is even (i.e., i % 2 == 0).

Step 6: If the number is even, add it to even_sum.

Step 7: After the loop ends, print the sum of even numbers.

Step 8: Stop.

Conclusion:

This program calculates the sum of even numbers between two given numbers.
It takes two numbers as input, checks each number in between, and adds the
even ones to the total sum. The program runs correctly and gives the expected
output.

3|Page
Problem Statement-02:

Write a program to calculate and display the sum and average of first n odd
natural numbers.

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, odd_num, odd_sum = 0, cnt = 0;
float avg;
printf("Enter a number: ");
scanf("%d", &odd_num);
for(i = 1; i <= odd_num; i += 2)
{
if(i % 2 != 0)
{
odd_sum += i;
cnt++;
}
}
avg = odd_sum / cnt;
printf("The sum of first %d odd number is: %d \nThe average is:
%.2f", odd_num, odd_sum, avg);

return 0;
}

4|Page
Output Snippet:

Algorithm:

Step 1: Start

Step 2: Declare variables i, odd_num, odd_sum, cnt, and avg.

Step 3: Set odd_sum = 0 and cnt = 0.

Step 4: Ask the user to enter a number and store it in odd_num.

Step 5: Use a loop from 1 to odd_num, increasing i by 2 in each step.

Step 6: If i is an odd number, add it to odd_sum and increase cnt by 1.

Step 7: Calculate the average by dividing odd_sum by cnt.

Step 8: Display the sum of the first odd_num odd numbers and their average.

Step 9: End

Conclusion:

This program calculates the sum and average of the first few odd numbers
entered by the user. It correctly identifies odd numbers, adds them, and finds
their average. The program runs properly and gives the expected results.

5|Page
Problem Statement-03:

Write a program to display Fibonacci series of last term up to 300.

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, num1 = 0, num2 = 1, next = 0;
next = num1 + num2;
printf("The Fibonacci series is: %d %d ", num1, num2);
while(next <= 300)
{
printf("%d ", next);
num1 = num2;
num2 = next;
next = num1 + num2;
}

return 0;
}

Output Snippet:

6|Page
Algorithm:

Step 1: Start

Step 2: Declare integer variables i, num1, num2, and next.

Step 3: Initialize num1 = 0, num2 = 1, and next = num1 + num2.

Step 4: Print the first two Fibonacci numbers (num1 and num2).

Step 5: Use a while loop to generate Fibonacci numbers until next is less than
or equal to 300.

Step 6: Inside the loop:

 Print the current next value.


 Update num1 to num2.
 Update num2 to next.
 Calculate the next Fibonacci number as num1 + num2.

Step 7: End the program.

Conclusion:

This program generates the Fibonacci series starting from 0 and 1 and continues
until the next number exceeds 300. It correctly prints the sequence by adding
the previous two numbers to get the next one. The program runs properly and
gives the expected Fibonacci series without errors.

7|Page
Problem Statement-04:

Write a program to print the numbers from 1 to 10 and their squares using
while/do while /for Loop.

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
for(i = 1; i <= 10; i++)
{
printf("The square of %d is: %d\n", i, i * i);
}

return 0;
}

Output Snippet:

8|Page
Algorithm:

Step 1: Start

Step 2: Declare an integer variable i.

Step 3: Use a for loop where i starts from 1 and goes up to 10.

Step 4: Inside the loop, calculate the square of i by multiplying i * i.

Step 5: Print the value of i along with its square.

Step 6: Repeat Steps 4 and 5 for all values of i from 1 to 10.

Step 7: End

Conclusion:

This program calculates and prints the square of numbers from 1 to 10. It uses a
for loop to go through each number, multiplies it by itself, and displays the
result. The program runs correctly and gives the expected output without any
errors.

9|Page
Problem Statement-05:

Write a program using while/do while /for Loop to calculate the factorial value
of any integer entered through the keyboard.
Input: 5
Output: 120

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, num;
unsigned long long int fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
if(num < 0)
{
num = -num;
}
for(i = 1; i <= num; i++)
{
fact *= i;
}
printf("The factorial of %d is: %llu", num, fact);

return 0;
}

10 | P a g e
Output Snippet:

Algorithm:

Step 1: Start

Step 2: Declare variables i, num, and fact. Initialize fact to 1.

Step 3: Ask the user to enter a number.

Step 4: Read the input number and store it in num.

Step 5: If num is negative, convert it to a positive number.

Step 6: Use a for loop to calculate the factorial:

 Multiply fact by each number from 1 to num.

Step 7: Display the factorial of num.

Step 8: End

11 | P a g e
Conclusion:

This program calculates the factorial of a given number. It first converts


negative numbers to positive and then uses a loop to find the factorial. The
program runs correctly and gives the expected output for positive numbers.

Problem Statement-06:

Write a program to emulate the pow library function where the parameters will
be user input using a while loop.

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i = 0, base, pow, result = 1;
printf("Enter base and power of a number: ");
scanf("%d %d", &base, &pow);
if(base < 0)
{
printf("Base can't be less than 1");
}
else
{
while (i < pow)
{
result *= base;
i++;
}
printf("The result of %d^%d is: %d", base, pow, result);
}

return 0;
}

12 | P a g e
Output Snippet:

Algorithm:

Step 1: Start

Step 2: Declare variables i = 0, base, pow, and result = 1.

Step 3: Ask the user to enter the base and power of a number.

Step 4: Read the values of base and pow.

Step 5: If base is less than 0, print "Base can't be less than 1".

Step 6: Otherwise, repeat the following steps while i < pow:

 Multiply result by base.


 Increase i by 1.

Step 7: Print the final result as base^pow.

Step 8: End.

13 | P a g e
Conclusion:

This program calculates the power of a number using a loop. It takes a base and
an exponent (power) as input from the user and multiplies the base repeatedly to
find the result. If the base is negative, the program displays an error message.
The program runs correctly and gives the expected output.

Problem Statement-07:

Write a program to Find out if a number is perfect or not.


Sample output:
28: Perfect Number
24: Not Perfect Number

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for(i = 1; i < num; i++)
{
if(num % i == 0)
{
sum += i;
}
}
if(sum == num)
{
printf("%d is a Perfect number.", num);
}
else
{
printf("%d is Not Perfect number.", num);
}

return 0;
}

14 | P a g e
Output Snippet:

Algorithm:

Step 1: Start

Step 2: Declare variables i, num, and sum (initialize sum to 0).

Step 3: Ask the user to enter a number.

Step 4: Read the input number and store it in num.

Step 5: Use a loop from 1 to num - 1.

 If num is divisible by i (i.e., num % i == 0), add i to sum.

Step 6: After the loop ends, check if sum is equal to num:

 If yes, print that num is a Perfect number.


 If no, print that num is Not a Perfect number.

Step 7: End

15 | P a g e
Conclusion:

This program checks whether a given number is a Perfect Number or not. A


Perfect Number is a number whose sum of all divisors (excluding itself) equals
the number itself. The program correctly takes user input, finds its divisors,
calculates the sum, and displays whether it is perfect or not. If the user enters a
valid number, the program runs properly without any errors and gives the
correct result.

16 | P a g e
Problem Statement-08:

Write a code to find out if a number is a prime number or not.


Input: 29
Output: Prime

Code Snippet:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, num, is_prime = 0;
printf("Enter a positive number: ");
scanf("%d", &num);

if(num % 2 != 0)
{
for(i = 2; i * i < num; i++)
{
if(num % i != 0)
{
is_prime = 1;
break;
}
}
}
if(num == 2)
{
is_prime = 1;
}
else if(num == 0 || num == 1)
{
is_prime = 0;
}

if(is_prime == 1)
{
printf("%d is a prime number.", num);
}
else
{
printf("%d is not a prime number.", num);
}

17 | P a g e
return 0;
}

Output Snippet:

Algorithm:

Step 1: Start

Step 2: Declare variables i, num, and is_prime (initialize is_prime to 0).

Step 3: Ask the user to enter a positive number.

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

Step 5: Check if the number is odd (num % 2 != 0):

 If true, use a loop to check divisibility from i = 2 to sqrt(num).


 If num is not divisible by any number in this range, set is_prime = 1 and
break the loop.

Step 6: If num is 2, set is_prime = 1 (since 2 is a prime number).

Step 7: If num is 0 or 1, set is_prime = 0 (since they are not prime).

Step 8: Check the value of is_prime:

18 | P a g e
 If is_prime == 1, print that num is a prime number.
 Otherwise, print that num is not a prime number.

Step 9: End.

Conclusion:

This program correctly checks whether a given number is prime or not. It first
verifies if the number is even, then uses a loop to check divisibility for odd
numbers up to its square root. Special cases like 0, 1, and 2 are handled
separately. The output correctly determines and displays whether the number is
prime. If the input is valid, the program runs properly without errors. However,
there is a small mistake in the loop condition (i * i < num should be i * i <=
num), which may cause incorrect results for some numbers. Fixing this would
make the program fully accurate.

19 | P a g e

You might also like