CSE1202 Structured Programming Lab ULAB
CSE1202 Structured Programming Lab ULAB
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:
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 3: Ask the user to enter two numbers and store them in num1 and num2.
Step 5: Inside the loop, check if the current number i is even (i.e., i % 2 == 0).
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 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:
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 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.
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 3: Use a for loop where i starts from 1 and goes up 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 8: End
11 | P a g e
Conclusion:
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 3: Ask the user to enter the base and power of a number.
Step 5: If base is less than 0, print "Base can't be less than 1".
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:
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 7: End
15 | P a g e
Conclusion:
16 | P a g e
Problem Statement-08:
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
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