- Write a function to calculate the factorial value of any integer entered through the keyboard.
#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
clrscr();
long int n,res,x;
scanf("%ld",&n);
res=factorial(n);
printf("%ld",res);
getch();
}
long int factorial(int n)
{
int rem=1,i;
for(i=n;i>=1;i--)
rem=rem*i;
return rem;
} - Write a function power(a,b) to calculate the value of a raised to b .
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
void main()
{
clrscr();
int i,x,sum,r,n;
scanf("%d%d",&x,&n);
r=power(x,n);
printf("%d",r);
getch();
}
int power(int a,int b)
{
int i,res=1;
for(i=b;i>=1;i--)
res=a*res;
return res;
} - Write a function to calculate LCM of two numbers.
#include<stdio.h>
#include<conio.h>
int lcm(int a,int b);
void main()
{
clrscr();
int a,b,i,x,y,res;
scanf("%d%d",&x,&y);
res=lcm(x,y);
printf("LCM is:\n%d",res);
getch();
}
int lcm(int a,int b)
{
int i;
for(i=1; ;i++)
{
if(i%a==0&&i%b==0)
break;
}
return i;
} - Write a function to calculate GCD of two numbers.
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b);
void main()
{
clrscr();
int x,y,res;
scanf("%d%d",&x,&y);
res=gcd(x,y);
printf("%d",res);
getch();
}
int gcd(int a,int b)
{
int rem;
while(b>0)
{
rem=a%b;
a=b;
b=rem;
}
return a;
} - Any year is entered through the keyboard.Write a function to determine whether the year is a leap year or not.
#include<stdio.h>
#include<conio.h>
int year(int y);
void main()
{
clrscr();
int y,res;
printf("Enter the value of year?\n");
scanf("%d",&y);
res=year(y) ;
getch();
}
int year(int y)
{
if(y%400==0)
return printf("It is leap year");
else if(y%100==0)
return printf("It is not leap year");
else if(y%4==0)
return printf("It is leap year");
else
return printf("It is not leap year");
} - A prime integer is entered through the keyboard.Write a function to obtain the prime factors of this number.
#include<stdio.h>
#include<conio.h>
int prime (int x);
void main()
{
clrscr();
int num;
printf("Enter The Number:");
scanf("%d",&num);
int p,i;
p=num;
for(i=2;i<=(num/2);i++)
{ if(prime(i))
while(p%i==0)
{ printf("%d ",i);
p=p/i;
}
}
getch();
}
int prime (int x)
{ int i=2;
while(i<=(x/2))
{ if(x%i==0)
break;
++i;
}
if (i>(x/2))
return 1;
else
return 0;
} - Write a function which receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main().
#include<stdio.h>
#include<conio.h>
#include<math.h>
int sum(int p,int q,int r,int s,int t);
float average(float o);
float sd(float m,float n);
void main()
{
clrscr();
int a,b,c,d,e,x;
float y,z;
printf("Enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
printf("Enter the value of c:");
scanf("%d",&c);
printf("Enter the value of d:");
scanf("%d",&d);
printf("Enter the value of e:");
scanf("%d",&e);
x=sum(a,b,c,d,e);
printf("\nSUM=%d",x);
y=average(x);
printf("\nAVERAGE=%f",y);
z=sd(x,y);
printf("\nSTANDARD DEVIATION=%f",z);
getch();
}
int sum(int p,int q,int r,int s,int t)
{
return p+q+r+s+t;
}
float average(float o)
{
return o/5;
}
float sd(float m,float n)
{
return (sqrt(m-n));
} - A 5 digit positive integer is entered through the keyboard write a function to calculate sum of digits of the digit number.
Using recursion:
#include<stdio.h>
#include<conio.h>
int digitsum(int n);
void main()
{
clrscr();
int x;
printf("Enter The Number:");
scanf("%d",&x);
printf("The Sum Is:%d",digitsum(x));
getch();
}
int digitsum(int n)
{
int rem,sum;
if(n==0)
return 0;
rem=n%10;
n=n/10;
sum=rem+digitsum(n);
return sum;
}
Without using recursion:
#include<stdio.h>
#include<conio.h>
int digitsum(int n);
void main()
{
clrscr();
int x;
printf("Enter The Number:");
scanf("%d",&x);
printf("The Sum Is:%d",digitsum(x));
getch();
}
int digitsum(int n)
{
int rem,sum=0,i;
for (i=1;i<=5;i++)
{ rem=n%10;
n=n/10;
sum=sum+rem; }
return sum;
} - A positive integer is entered through the keyboard, write a program to obtain the prime factors recursively.
#include<stdio.h>
#include<conio.h>
primefct(int x);
void main()
{
clrscr();
int x;
printf("Enter an integer:");
scanf("%d",&x);
printf("the prime factors are:");
primefct(x);
getch();
}
primefct(int n)
{
int i;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
printf("%d ",i);
primefct(n/i);
break;
}
}
} - Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence.In a Fibonacci sequences the sum of two successive terms given the third term. Following are the first few term of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55.........
#include<stdio.h>
#include<conio.h>
long int fib(long int m);
void main()
{
clrscr();
long int i;
for(i=1;i<=25;i++)
printf("\n%ld\n",fib(i));
getch();
}
long int fib(long int m)
{
if(m<=2)
return 1;
else
return fib(m-1)+fib(m-2);
}
Search Here
Wednesday, March 30, 2011
Functional C programming
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment