[go: up one dir, main page]

0% found this document useful (0 votes)
6 views6 pages

Spl lab 3

The lab report details the implementation of user-defined and recursive functions in C, focusing on calculating factorials, checking for prime numbers, and summing natural numbers. It includes source code for each program along with descriptions of the algorithms used. The author reflects on their learning experience and the practical applications of recursion and function implementation in programming.

Uploaded by

hasnat4222
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)
6 views6 pages

Spl lab 3

The lab report details the implementation of user-defined and recursive functions in C, focusing on calculating factorials, checking for prime numbers, and summing natural numbers. It includes source code for each program along with descriptions of the algorithms used. The author reflects on their learning experience and the practical applications of recursion and function implementation in programming.

Uploaded by

hasnat4222
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/ 6

Southern University Bangladesh

Department of Computer Science and Engineering

TITLE OF THE LAB REPORT

Implementation of user-defined function and recursive function.

Course Code: CS 0611-102


Course Title: Structured Programming Language Sessional
Fall 2024

1. Name : Hasnat Hossain


2. Student ID : 666-63-65
3. Batch : 63
4. Email ID : hasnat4222@gmail.com
5. Date of Assignment : 06/12/2024
6. Date of Submission : 12/12/2024
7. Contact No : 01615091911
8. Signature of Student :
9. Course Teacher : Tahsin Azad Tias
10. Signature of Teacher :
I. Objective:
• Calculate the factorial of a number using recursion.
• Check whether the number is prime or not.
• Find the Sum of Natural Numbers (upto n).

Program 1: Calculate the Factorial of a Number Using Recursion.

A. Description:
The problem is to calculate the factorial of a given number using recursion in C.
Factorial of a number (n) is the product of all positive integers less than or equal to
n. The recursive approach involves the function calling itself with a smaller value
until it reaches the base case (n = 1).

Steps:

1. Input the number:

• Take an integer input from the user using the scanf function.

2. Define the recursive function:

• Create a function factorial() that calls itself recursively with (n-1) until n

equals 1.

3. Base Case:

• If n == 1, return 1.

4. Print the Result:

• Call the factorial() function with the given input and print the result.

B. Source Code:
#include <stdio.h>
int factorial(int n) {
if (n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {

666-63-65 2
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 1)
printf("Factorial is not defined for negative numbers or zero.\n");
else
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

C. Program Input/Output:

Program 2 : Check Whether the Number is Prime or Not.

A. Description:
The goal is to determine if a number is prime. A prime number is greater than 1 and
divisible only by 1 and itself.
Steps:

1. Input the number:


o Take an integer input from the user.
2. Check divisibility:
o Iterate from 2 to √n using a loop.
o If num % i == 0 for any i, the number is not prime.
3. Print the Result:
o If no divisors are found, the number is prime; otherwise, it is not.

666-63-65 3
B. Source Code:

#include <stdio.h>
#include <math.h>
int isPrime(int num)
{
if (num <= 1)
return 0;
for (int i = 2; i <= sqrt(num); i++)
{
if (num % i == 0)
return 0;
}
return 1;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}

C. Program input/output:

666-63-65 4
Program 3: Find the Sum of Natural Numbers (Upto n).

A. Description:

The task is to find the sum of natural numbers up to n using recursion. The sum of
natural numbers is given by the formula S = 1 + 2 + 3 + ... + n.
Steps:

1. Input the number:


o Take an integer input from the user.
2. Define the recursive function:
o Create a function sumOfNatural() that calls itself recursively with (n-1)
until n equals 0.
3. Base Case:
o If n == 0, return 0.
4. Print the Result:
o Call the sumOfNatural() function and print the result.

B. Source Code:

#include <stdio.h>
int sumOfNatural(int n)
{
if (n == 0)
return 0;
return n + sumOfNatural(n - 1);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n < 1)
printf("Sum is not defined for negative numbers or zero.\n");
else
printf("Sum of natural numbers up to %d is %d\n", n, sumOfNatural(n));
return 0;
}

666-63-65 5
C. Program Input/Output:

II. Discussion:
From this lab, I learned how to implement user-defined functions and recursion in C
programming. I understood how recursion can simplify complex problems like
factorial calculation and summing natural numbers. Moreover, I practiced writing
prime number checks using loops and conditional statements. This knowledge can
be applied in real-life scenarios where mathematical computations or iterative tasks
are required, such as in developing algorithms or solving engineering problems.

666-63-65 6

You might also like