0% found this document useful (0 votes)
12 views14 pages

CSE1202 Structured Programming Lab ULAB

This document contains a lab report for the Structured Programming Lab course, detailing five programming problems in C. Each problem includes a problem statement, code snippets, algorithms, and conclusions regarding the functionality of the programs. The problems cover finding perfect numbers, prime numbers, generating Fibonacci sequences, calculating areas of geometric shapes, and counting digits in an integer.

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)
12 views14 pages

CSE1202 Structured Programming Lab ULAB

This document contains a lab report for the Structured Programming Lab course, detailing five programming problems in C. Each problem includes a problem statement, code snippets, algorithms, and conclusions regarding the functionality of the programs. The problems cover finding perfect numbers, prime numbers, generating Fibonacci sequences, calculating areas of geometric shapes, and counting digits in an integer.

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

Course Code: CSE1202

Course Title: Structured Programming Lab


Section: 03
Semester: Spring 2025

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:

Submission Date: 25/03/2025

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>

void findPerfectNumbers(int lower, int upper)


{
int i, j;
printf("Perfect Numbers are: ");
for(i = lower; i <= upper; i++)
{
int sum = 0;
for(j = 1; j < i; j++)
{
if(i % j == 0)
{
sum += j;
}
}
if(sum == i)
{
printf("%d ", i);
}
}
}

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 2: Define a function findPerfectNumbers(lower, upper) to find and print


perfect numbers within a given range.
Step 3: Inside the function, initialize a loop from lower to upper to check each
number.

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.

Step 9: Call findPerfectNumbers(lower, upper) to find and print perfect


numbers in the given range.
Step 10: End

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>

void findPrimeNumbers(int lower, int upper)


{
int i, num;
for(num = lower; num <= upper; num++)
{
if(num == 0 || num == 1)
{
continue;
}

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 2: Define a function findPrimeNumbers(lower, upper) to find prime


numbers within a given range.
Step 3: Inside the function, use a loop to check each number from lower to
upper.

Step 4: If the number is 0 or 1, skip it, as these are not prime numbers.

Step 5: Assume the number is prime (is_prime = 1).

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 7: If the number is 2, set is_prime = 1, as 2 is a prime number.


Step 8: If is_prime = 1, print the number as it is a prime number.

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.

Step 11: End.

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>

void fibonacci(int term)


{
int n1 = 0, n2 = 1, next;
next = n1 + n2;
printf("Fibonacci Series: %d %d ", n1, n2);
while (term - 2)
{
printf("%d ", next);
n1 = n2;
n2 = next;
next = n1 + n2;
term--;
}
}

int main()
{
int term;
printf("Enter number of term: ");
scanf("%d", &term);
fibonacci(term);

return 0;
}

Output Snippet:

7|Page
Algorithm:

Step 1: Start

Step 2: Define a function fibonacci(term) to generate the Fibonacci series.

Step 3: Inside the function, initialize three variables:

 n1 = 0 (first term)

 n2 = 1 (second term)

 next = n1 + n2 (next term in the series)

Step 4: Print the first two terms (n1 and n2).

Step 5: Use a while loop to generate the remaining terms of the Fibonacci series
until the given number of terms is reached.

 Print the next term.

 Update n1 = n2 and n2 = next.

 Calculate the new next = n1 + n2.

 Decrease term by 1 in each iteration.


Step 6: In the main() function:

 Ask the user to enter the number of terms.

 Store the input value in the variable term.

 Call the fibonacci(term) function to generate and print the Fibonacci


series.

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

Step 2: Display a menu with four options:


1. Triangle
2. Rectangle
3. Square
4. Circle

Step 3: Ask the user to enter a number (1-4) to choose a shape.


Step 4: Read the user’s choice and proceed accordingly:
If choice is 1 (Triangle):
• Ask the user to enter the base and height of the triangle.
• Calculate the area using the formula: (0.5 × base × height).
• Display the result.
If choice is 2 (Rectangle):
• Ask the user to enter the length and height of the rectangle.
• Calculate the area using the formula: (length × height).
• Display the result.

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 5: If the user enters an invalid choice, display an error message.

Step 6: Stop.

Conclusion:

The program successfully calculates the area of different shapes (triangle,


rectangle, square, and circle) based on user input. It asks the user to choose a
shape and enter the required dimensions, then calculates and displays the area
correctly. The program runs properly and gives accurate results for valid inputs.
If the user enters an invalid option, it displays an error message. Overall, the
program works as expected and performs the required calculations correctly.

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;
}

int DigitCount(int num)


{
int cnt = 0;
while(num != 0)
{
num = num / 10;
cnt++;
}
return cnt;
}

Output Snippet:

13 | P a g e
Algorithm:

Step 1: Start

Step 2: Declare an integer variable num to store user input.

Step 3: Ask the user to enter a number.


Step 4: Read the number from the user and store it in num.
Step 5: Call the function DigitCount(num) to count the number of digits in
num.

Step 6: Inside the DigitCount function:

Initialize cnt to 0.

Use a loop to divide num by 10 repeatedly until it becomes 0.


Increase cnt by 1 in each iteration.
Step 7: Return the value of cnt (total number of digits) to the main function.

Step 8: Print the total number of digits.


Step 9: End

Conclusion:

This program successfully counts the number of digits in a given number. It


takes a number as input from the user and uses a function to divide the number
by 10 repeatedly until it reaches 0. Each division increases the count of digits,
which is then displayed as output. The program runs properly and provides the
correct number of digits for any positive or negative integer.

14 | P a g e

You might also like