0% found this document useful (0 votes)
8 views

Recursion

The document contains multiple C programs that demonstrate the use of recursion to solve various mathematical problems. These include calculating simple interest, the area of a triangle, the sum of natural numbers, the factorial of a number, and the area of a square. Each program prompts the user for input and displays the computed result based on recursive function calls.

Uploaded by

dedarkshadowyt66
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)
8 views

Recursion

The document contains multiple C programs that demonstrate the use of recursion to solve various mathematical problems. These include calculating simple interest, the area of a triangle, the sum of natural numbers, the factorial of a number, and the area of a square. Each program prompts the user for input and displays the computed result based on recursive function calls.

Uploaded by

dedarkshadowyt66
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
You are on page 1/ 5

SI unit using recursion

#include<stdio.h>
int si(int,int,int);
int main()
{
int p,t,r,ans;
printf("Enter principle,time & rate; ");
scanf("%d%d%d",&p,&t,&r);
ans=si(p,t,r);
printf("%d",ans);
return 0;
}
int si(int x,int y,int z)
{
if(y<=0)
{
return 0;
}
else
{
return (x*z/100)+si(x,y,z-1);
}
}
Area of triangle using recursion
#include<stdio.h>
int area(int,int);
int main()
{
int b,h,a;
printf("Enter dimensions: ");
scanf("%d%d",&b,&h);
a=area(b,h);
printf("%d",a);
return 0;
}
int area(int b,int h)
{
if(h<=0)
{
return 0;
}
else
{
return (0.5*b)+area(b,h-1);
}
}
Natural sum using recursion
#include<stdio.h>
int sum(int n);
int main()
{
int n, result;
printf("Enter a positive integer: ");
scanf("%d", &n);
if(n < 1) {
printf("Please enter a positive integer.\n");
return 1;
}
result = sum(n);
printf("Sum of natural numbers up to %d is: %d\n", n, result);
return 0;
}
int sum(int n)
{
if(n == 1) {
return 1;
} else {
return n + sum(n - 1);
}
}
Factorial of a number using recursion
#include<stdio.h>
int fact(int n);
int main()
{
int n, result;
printf("Enter a positive integer: ");
scanf("%d", &n);
if(n < 0) {
printf("Factorial is undefined.\n");
return 1;
}
result = fact(n);
printf("Factorial of %d is: %d\n", n, result);
return 0;
}
int fact(int n)
{
if(n == 0 || n == 1) {
return 1;
} else {
return n * fact(n - 1);
}
}
Area of square using recursion
#include<stdio.h>
int area(int,int);
int main()
{
int b,h,a;
printf("Enter dimensions: ");
scanf("%d%d",&b,&h);
a=area(b,h);
printf("%d",a);
return 0;
}
int area(int b,int h)
{
if(h<=0)
{
return 0;
}
else
{
return (0.5*b)+(b,h-1);
}
}

You might also like