Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
int fib(int );
main()
{
int n;
printf("Enter a positive value for n=");
scanf("%d",&n);
printf("The %dth term of Fibonacci series is %d\n",n,fib(n));
}
int fib(int x)
{
if(x==0 || x==1)
return x;
else
return (fib(x-1)+fib(x-2));
}
Using recursion write C functions Least Common Multiple of two integers.
#include<stdio.h>
int lcm(int a,int b)
{
static int temp=1;
if((temp%a==0)&&(temp%b==0))
{
return temp;
}
temp++;
lcm(a,b);
return temp;
}
void main()
{
int x,y;
printf("enter two integer values");
scanf("%d%d",&x,&y);
printf("\n LCM of %d and %d is : %d",x,y,lcm(x,y));
}
Using recursion write C functions Greatest Common Divisor of two
integers.
#include<stdio.h>
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
void main()
{
int x,y;
printf("enter two integer values");
scanf("%d%d",&x,&y);
printf("\n HCF of %d and %d is : %d",x,y,gcd(x,y));
}
A function that takes a real number x and a positive integer n
as arguments and returns x n .
#include<stdio.h>
#include<math.h>
int power(int x,int n)
{
return pow(x,n);
}
void main()
{
int x,n;
printf("enter two integer values");
scanf("%d%d",&x,&n);
printf("\n power of %d and %d is : %d",x,n,power(x,n));
}
Write a c program 1 to n numbers find minimum and maximum
values
#include<stdio.h>
main()
{
int i=1,n,max=0,min;
while(i<=5)
{
scanf("%d",&n);
if(n>max)
max=n;
If(min==0)
min=n;
if(n<min)
min=n;
i++;
}
printf("max=%d min=%d",max,min);
}