Chapter 4- Programming in C----> Function & Recursion
Function
void sum(); { sum(); } |
void sum() { } |
Types of function in C programming
1. Function without argument and no return value
2. Function with argument and no return value
3. Function without argument and with return value
4. Function with argument and with return value
Function without argument and no return value
#include <stdio.h>
int main()
{
sum();
return 0;
}
void sum()
{
int a,b,s;
printf("enter two numbers");
scanf("%d%d",&a,&b);
s=a+b;
printf("sum=%d",s);
}
Function with argument and no return value
#include <stdio.h>
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d%d",&a,&b);
sum(a,b);
return 0;
}
void sum(int a,int b)
{
int s;
s=a+b;
printf("sum=%d",s);
}
Function without argument and with return value
#include <stdio.h>
int sum ();
int main()
{
int x;
x=sum();
printf("sum=%d",x);
return 0;
}
int sum()
{
int a,b,s;
printf("enter two numbers");
scanf("%d%d",&a,&b);
s=a+b;
return s;
}
Function with argument and with return value
#include <stdio.h>
int sum (int, int);
int main()
{
int a,b,x;
printf("enter two numbers");
scanf("%d%d",&a,&b);
x=sum(a,b);
printf("sum=%d",x);
return 0;
}
int sum(int a,int b)
{
int s;
s=a+b;
return s;
}
Recursion is a process where a function calls itself directly or indirectly to solve a problem.
Every recursive function has:
1. Base case → the stopping condition (prevents infinite calls).
2. Recursive case → where the function calls itself with a smaller problem.
Advantages of Recursion
1. Simplicity of Code
2. Solves Complex Problems Easily
1. 1. Write a program to calculate the simple interest using function.
#include <stdio.h>
void simple_interest();
int main()
{
simple_interest();
return 0;
}
void simple_interest()
{
float p, r, t, si;
printf("Enter principal, rate and time: ");
scanf("%f%f%f", &p, &r, &t);
si = (p * r * t) / 100;
printf("Simple Interest = %f", si);
}
2. 2. Write a program to display the greatest number among three numbers using function.
#include <stdio.h>
void greatest();
int main()
{
greatest();
return 0;
}
void greatest() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is greatest", a);
else if (b >= a && b >= c)
printf("%d is greatest", b);
else
printf("%d is greatest", c);
}
3. 3. Write a program to display whether the given number is exactly divisible by 5 or not function.
#include <stdio.h>
void divisible_by_5();
int main() {
divisible_by_5();
return 0;
}
void divisible_by_5()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 5 == 0)
printf("%d is divisible by 5", n);
else
printf("%d is not divisible by 5", n);
}
4. 4. Write a program to read N numbers from the user and display the sum of even and odd numbers separately using function.
#include <stdio.h>
void sum_even_odd();
int main()
{
sum_even_odd();
return 0;
}
void sum_even_odd()
{
int n, i, num, even_sum = 0, odd_sum = 0;
printf("Enter how many numbers: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num % 2 == 0)
even_sum += num;
else
odd_sum += num;
}
printf("Sum of even numbers = %d\n", even_sum);
printf("Sum of odd numbers = %d\n", odd_sum);
}
5. 5. Write a program to calculate the area of the circle by using function.
#include <stdio.h>
void area_circle();
int main()
{
area_circle();
return 0;
}
void area_circle()
{
float r, area, pi=3.1416;
printf("Enter radius of circle: ");
scanf("%f", &r);
area = pi * r * r;
printf("Area of Circle = %f", area);
}
6. 6. Write a program to display the Fibonacci series up to 10th terms using function.
#include <stdio.h>
void fibonacci();
int main()
{
fibonacci();
return 0;
}
void fibonacci()
{
int a = 0, b = 1, c, i;
printf("Fibonacci series up to 10 terms:\n");
printf("%d %d ", a, b);
for (i = 3; i <= 10; i++)
{
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
7. 7. Write a program to display the sum of "n" integer numbers using function.
#include <stdio.h>
void sum_n();
int main()
{
sum_n();
return 0;
}
void sum_n()
{
int n, i, num, sum = 0;
printf("Enter how many numbers: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &num);
sum += num;
}
printf("Sum = %d", sum);
}
8. 8. Write a program to display the factorial of the given number using recursion.
#include <stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, factorial(n));
return 0;
}
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
9. 9. Write a program to display the Fibonacci series using recursion.
#include <stdio.h>
int fibonacci(int n);
int main()
{
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
return 0;
}
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
10. 9. Write a program to find sum of first n natural number using recursion.
#include <stdio.h>
int sum(int n);
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum of first %d natural numbers = %d", n, sum(n));
return 0;
}
int sum(int n)
{
if (n == 0)
return 0;
else
return n + sum(n - 1);
}