0% found this document useful (0 votes)
3 views11 pages

C Lab File 5

The document outlines an experiment focused on understanding function programming in C, including types of functions, function calls, and recursion. It provides examples of recursive functions such as calculating factorials and GCD, as well as reversing integers and summing natural numbers. The conclusion highlights the practical knowledge gained in modular programming, recursion, and problem-solving skills.

Uploaded by

Shivank Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

C Lab File 5

The document outlines an experiment focused on understanding function programming in C, including types of functions, function calls, and recursion. It provides examples of recursive functions such as calculating factorials and GCD, as well as reversing integers and summing natural numbers. The conclusion highlights the practical knowledge gained in modular programming, recursion, and problem-solving skills.

Uploaded by

Shivank Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment-5 Date-6/10/25

Aim- To understand function programming, its types and


function-call

Theory-In C programming, a function is a self-contained


block of code that performs a specific task. Functions help in
breaking a large program into smaller, manageable, and
reusable parts. Every function in C has a return type, a
name, and parameters (optional). There are two main types
of functions:
1. Library Functions – These are predefined functions
provided by C libraries, such as printf(), scanf(), sqrt(),
etc.
2. User-Defined Functions – These are functions created
by the programmer to perform specific tasks according
to the requirements.
Functions improve modularity, reduce code repetition, and
make programs easier to understand and debug. A function
is executed when it is called from another function, usually
from the main() function.
Function Call and Return
When a function is called, control of the program is
transferred to the function definition. After the execution of
the function body, control returns to the point where it was
called. If the function has a return type other than void, it will
return a value to the calling function.
Recursion
A special type of function call in C is recursion, where a
function calls itself directly or indirectly to solve a problem.
Recursion is based on the principle of solving a big problem
by breaking it into smaller subproblems of the same type.
Every recursive function has two parts:
1. Base Case – The condition under which the recursion
stops. Without a base case, recursion will continue
infinitely, causing a stack overflow.
2. Recursive Case – The part of the function where it
calls itself with modified arguments, moving the problem
closer to the base case.
For example, the factorial of a number n can be expressed
recursively as:
• Factorial(n) = n × Factorial(n–1), with base case
Factorial(0) = 1.
Similarly, recursion can be used to compute the GCD of two
numbers, the sum of natural numbers, and other problems
that can be reduced step by step. Functions and recursion
are fundamental concepts in C programming. Functions
allow structured program design, while recursion provides a
powerful way to solve problems that have repetitive or self-
similar patterns.
5.1 Factorial using recursion
Flowchart-

START

INPUT N

IF N!=0 NO

YES

PRINT FACT(N)

FACT(N-1)*N
PRINT 1

PRINT OUTPUT

END

Code-
#include<stdio.h>
int fact(int n);
int main(){
int a,n;
printf("Enter the number");
scanf("%d", &n);
a = fact(n);
printf("the factorial is: %d", a);

}
int fact(int n){
int factn1, factn;
if (n == 0){
return 1;
}
factn1 = fact(n-1);
factn = fact(n-1)*n;
return factn;
}
Output-

5.2 GCD of two integers


Flowchart-

Code-
#include <stdio.h>
int main(){
int x,y,i,j,num;
printf("Enter the no.: ");
scanf("%d%d", &x,&y);
num = (x<y)?x:y;
for(i=1;i<=num;i++){
if (x%i==0 && y%i==0){
j=i;
}
}
printf("%d",j);
}
Output-

5.3 Read an integer and reverse it


Flowchart-

START

INPUT THE INTERGER

SWAP THE VARIABLES


AND REVSERSE THE
VALUE

PRINT THE REVERSE OF THE INTEGER

END

Code-
#include <stdio.h>
int main(){
char a,b,c,d,e,f;
printf("Enter the number");
scanf("%c%c%c", &a,&b,&c);
printf("Your number is: %c%c%c", a,b,c);
f=a;
e=b;
d=c;
printf("The reverse is: %c%c%c", d,e,f);
}

Output-

5.4 Sum of first n natural numbers-


Flowchart-

START

INPUT N

IF N!=1
NO

YES

PRINT SUM(N)

SUM(N-1) + N
PRINT 1

PRINT OUTPUT

END

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

Output-
Conclusion-
• Learned how to perform function calls and pass
values between functions.
• Gained practical knowledge of recursion and its
working with base conditions.
• Implemented recursion to solve problems like factorial,
GCD, number reversal, and sum of natural
numbers.
• Observed how large problems can be broken down into
smaller sub-problems using recursion.
• Improved understanding of modular programming
and code reusability.
• Enhanced logical thinking and problem-solving skills
using recursive approaches.
• Understood the concept of functions and their different
types in C.

SHIVANK MEHTA
AR- B1
[Link].-61
131251025267

You might also like