CSE1202 Structured Programming Lab ULAB
CSE1202 Structured Programming Lab ULAB
Lab Report 05
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 function in c program to find the perfect numbers within a given
number of ranges.
Pass the range as a parameter:
Input: Lower Range: 1 Upper Range: 50
Output: 6 28
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int lower, upper;
printf("Enter a Lower Range: ");
scanf("%d", &lower);
printf("Enter a Upper Range: ");
scanf("%d", &upper);
findPerfectNumbers(lower, upper);
return 0;
}
2|Page
Output Snippet:
Algorithm:
Step 1: Start
Step 4: For each number, initialize sum = 0 to store the sum of its divisors.
Step 5: Use another loop to check all numbers less than the current number to
find its divisors.
Step 6: If a number is a divisor (i.e., it divides the current number exactly), add
it to sum.
Step 7: After checking all divisors, if sum is equal to the original number, print
it as a perfect number.
Step 8: In the main() function, take user input for the lower and upper range.
3|Page
Conclusion:
This program successfully finds and prints all perfect numbers within a given
range. A perfect number is a number whose sum of divisors (excluding itself)
equals the number itself. The program takes user input for the lower and upper
range and checks each number to determine if it is perfect. If the input values
are valid, the program runs correctly and displays the correct output. If no
perfect numbers are found in the given range, it simply does not print any
number. Overall, the program functions properly without errors.
Problem Statement-02:
Write a function in C to find the prime numbers within a range of numbers.
Pass the range as a parameter:
Input: Lower Range: 1 Upper Range: 50
Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int is_prime = 1;
for(i = 2; i * i <= num; i++)
{
if(num % i == 0)
{
is_prime = 0;
break;
}
}
if(num == 2)
{
is_prime = 1;
4|Page
}
if(is_prime == 1)
{
printf("%d ", num);
}
}
}
int main()
{
int lower, upper;
printf("Enter a Lower Range: ");
scanf("%d", &lower);
printf("Enter a Upper Range: ");
scanf("%d", &upper);
findPrimeNumbers(lower, upper);
return 0;
}
Output Snippet:
5|Page
Algorithm:
Step 1: Start
Step 4: If the number is 0 or 1, skip it, as these are not prime numbers.
Step 6: Use another loop to check if the number is divisible by any number
from 2 to its square root. If it is divisible, set is_prime = 0 and break the loop.
Step 9: In the main() function, take user input for the lower and upper range.
Step 10: Call the findPrimeNumbers() function with the given range.
Conclusion:
This program successfully finds and prints all prime numbers within a given
range. It asks the user to enter a lower and upper limit and then checks each
number in that range to determine if it is prime. The program runs correctly and
provides accurate results. If the user inputs valid numbers, the program
efficiently identifies the prime numbers. However, if the lower and upper values
are not entered correctly, the output may not be meaningful. Overall, the
program works as expected without any
Problem Statement-03:
Write a function to obtain the first 10 numbers of a Fibonacci sequence.
Pass a value as a parameter.
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
In a Fibonacci sequence the sum of two successive terms gives the third term.
6|Page
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int term;
printf("Enter number of term: ");
scanf("%d", &term);
fibonacci(term);
return 0;
}
Output Snippet:
7|Page
Algorithm:
Step 1: Start
n1 = 0 (first term)
n2 = 1 (second term)
Step 5: Use a while loop to generate the remaining terms of the Fibonacci series
until the given number of terms is reached.
Step 7: End
Conclusion:
This program correctly generates the Fibonacci series up to the number of terms
entered by the user. It starts with 0 and 1 and continues by adding the previous
two numbers to get the next one. The program runs properly and displays the
expected output. However, it does not handle cases where the user enters a
8|Page
number less than 2, which may cause incorrect results. Overall, the code is
efficient and performs well for generating the Fibonacci series.
Problem Statement-04:
Write 4 different functions for calculating the area of a triangle, rectangle,
square and circle.
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
float triangle()
{
float base, height;
printf("Enter the base & height of a triangle: ");
scanf("%f %f", &base, &height);
return (0.5 * base * height);
}
float rectangle()
{
float length, height;
printf("Enter the length & height of a rectangle: ");
scanf("%f %f", &length, &height);
return (length * height);
}
float square()
{
float length;
printf("Enter the length of a square: ");
scanf("%f", &length);
return (length * length);
}
float circle()
{
float radius, pi = 3.1416;
printf("Enter the radius of a circle: ");
scanf("%f", &radius);
return (pi * radius * radius);
}
int main()
9|Page
{
int opt;
printf("1. Triangle \n2. Rectangle \n3. Square \n4. Circle\n");
printf("Enter a number for calculating the area(1-4): ");
scanf("%d", &opt);
if(opt == 1)
{
float r1 = triangle();
printf("Area of the triangle: %.2f\n", r1);
}
else if(opt == 2)
{
float r2 = rectangle();
printf("Area of the rectangle: %.2f\n", r2);
}
else if(opt == 3)
{
float r3 = square();
printf("Area of the square: %.2f\n", r3);
}
else if(opt == 4)
{
float r4 = circle();
printf("Area of the circle: %.2f\n", r4);
}
else
{
printf("Invalid input.");
}
return 0;
}
10 | P a g e
Output Snippet:
Algorithm:
Step 1: Start
11 | P a g e
If choice is 3 (Square):
• Ask the user to enter the length of the square.
• Calculate the area using the formula: (length × length).
• Display the result.
If choice is 4 (Circle):
• Ask the user to enter the radius of the circle.
• Calculate the area using the formula: (π × radius × radius), where π =
3.1416.
• Display the result.
Step 6: Stop.
Conclusion:
Problem Statement-05:
Give declaration for a function called DigitCount( ), which takes a integer and
returns the number of digit found in the given integer.
12 | P a g e
Code Snippet:
#include <stdio.h>
#include <stdlib.h>
int DigitCount(int);
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
int digit = DigitCount(num);
printf("The number of digit: %d", digit);
return 0;
}
Output Snippet:
13 | P a g e
Algorithm:
Step 1: Start
Initialize cnt to 0.
Conclusion:
14 | P a g e