রবিবার, ১২ ডিসেম্বর, ২০১০

Function C Programing


1.Write a function to calculate the factorial value of any integer entered through the keyboard.
#include<stdio.h>
#include<conio.h>
long fact(int n)
{
            int i;
            long f=1;
            for (i=1;i<=n;i++)
                f*=i;
            return (f);
}
void main()
{
            int n,r;
            clrscr();
            aa:
              printf("Please enter a value for n & r(r<=n)");
              scanf("%d %d",&n,&r);
             if(r>n)
             {
               printf("r is greter than n. ");
               goto aa;
             }
            printf("\n %d!  = %ld",n,fact(n));
            printf("\n %dP%d = %ld",n,r,fact(n)/fact(n-r));
            printf("\n %dC%d = %ld ",n,r,fact(n)/(fact(r)*fact(n-r)));
            getch();
}
2.Write a function power (a,b) to calculate the value of a raised to b.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double powr(int x,int y);
void main()
{
            int n;
            double x,result;
            clrscr();
            printf("Programme for calculation of 'x' to the power 'n' : ");
            printf("\nEnter a value for x : ");
            scanf("%lf",&x);
            printf("\nEnter a intiger value for n : ");
            scanf("%d",&n);
            result = powr(x, n);
            printf("\n%.2lf\ to the power %d is : %.3lf",x,n,result);
            getch();
}
double powr(int x,int y)
{
            double c;
            c= pow(a,b);
            return c;
}
3.Write a function to calculate  LCM  of  two numbers
#include<stdio.h>
#include<conio.h>
int lcm(int x,int y);
int gcd(int p,int q);
void main()
{
            int a,b;
            clrscr();
            scanf("%d%d",&a,&b);
            printf("The lcm is %d",lcm(a,b));
            getch();
}
int lcm(int x,int y)
{
            int m,v;
            m=gcd(x,y);
            v=(x*y)/m;
            return v;
}
int gcd(int p,int q)
{
            int c;
            do{
            c=p%q;
            p=q;
            q=c;
            }
            while(c!=0);
            return p;
}
4.Write a function to calculate GCD of two numbers.
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b);
void main()
{
            int a,b,c;
            clrscr();
            printf("Please enter two number for 'GCD' :");
            scanf("%d%d",&a,&b);
            c= gcd(a,b);
            printf("GCD = %d.",c);
            getch();
}
int gcd(int a,int b)
{
            int c;
            while(a!=0)
            {
              c=b%a;
              b=a;
              a=c;
            }
            return b;
}
5.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 lyear(int year);
void main()
{
            int year,c;
            clrscr();
            printf("Please enter a year to be tested : ");
            scanf("%d", &year);
            c=lyear(year);
            getch();
}
int lyear(int year)
{
            if((year%4==0 && year%100 !=0)|| year%400==0)
                printf("\n%d is a leap year.",year);
            else
                printf("\n%d is not a leap year.",year);
            return 0;
}
6.A prime integer is entered through the keyboard. Write a function to obtain the prime factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3 whereas prime factor of 35 are 5 and 7
#include<stdio.h>
#include<conio.h>
int primf(int n);
void main()
{
            int n,c;
            clrscr();
            printf("Prime factor : Enter a number :");
            scanf("%d",&n);
            c=primf(n);
            getch();
}
int primf(int n)
{
            int i;
            for(i=2;n!=1;i++)
            {
               if((n%i)==0)
                 {
                   n=n/i;
                   printf("%3d",i);
                   i=1;
                 }
            }

            return 0;
}
7.Write a function which receives a float and an int from main (), finds the product of these two and returns the product which is printed through main().
#include<stdio.h>
#include<conio.h>
float prd(float x,int y);
void main()
{
            float a,c;
            int b;
            clrscr();
            printf("Please enter a float & a intger number : ");
            scanf("%f%d",&a,&b);
            c=prd(a,b);
            printf("Product = %.3f",c);
            getch();
}
float prd(float x,int y)
{
            float d;
            d =x*y;
            return d;
}
8. Write a program 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 v);
float av1(int p,int q,int r,int s,int v);
double dv1(int p1,int q1,int r1,int s1,int v1);
float av;

void main()
{
            int a,b,c,d,e,sum1;
            long float dv;
            clrscr();
            printf("Enter five intger number :\n");
            scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
            sum1=sum(a,b,c,d,e);
            av=av1(a,b,c,d,e);
            dv=dv1(a,b,c,d,e);
            printf("\n\nThe avarage is %.3f",av);
            printf("\n\nThe sum is %d",sum1);
            printf("\n\nThe dev is %.3lf",dv);
            getch();
}

float av1(int p,int q,int r,int s,int v)
{
            return (p+q+r+s+v)/5;
}
int sum(int p,int q,int r,int s,int v)
{
            return (p+q+r+s+v);
}
double dv1(int p1,int q1,int r1,int s1,int v1)
{
            double t,g;
            t=pow((av-p1),2)+pow((av-q1),2)+pow((av-r1),2)+pow((av-s1),2)+pow((av-v1),2);
            g=pow((t/5),.5);
            return g;
}
9. A 5 digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5 digit number.

(i)           Using recursion
(ii)         Without using recursion

Using recursion
#include<stdio.h>
#include<conio.h>
long int Sum(long int digits );
void main()
{
            long int num,n,c;
            clrscr();
            printf("\nEnter 5 digits number : ");
            scanf("%ld",&n);
            c = Sum(n);
            printf("%ld",c);
            getch();

}
long int Sum(long int digits )
{
            if(digits < 10)
               return digits;
            else
               return digits%10 + Sum(digits/10);
}

Without using recursion
#include<stdio.h>
#include<conio.h>
void main()
{
           
            long int n;
            clrscr();
            printf("\nEnter 5 digits number : ");
            scanf("%ld",&n);
            long int(long int n);
            getch();
}
long int(long int n)
{
            int rdgt;
            long int num,sum = 0;
num = n;
            do
            {
                        rdgt = n%10;
                        n = n/10;
                        sum = (sum + rdgt);
                        }while(n !=0);
            printf("\nSum of Digits of the number %ld  is %ld ",num,sum);
            return 0;         
}

11. 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(int m);
void main()
{
            int i,n;
            clrscr();
            printf("First 25 Fibonacci sequence are :\n");
            for(i=1;i<=25;i++)
              {
                 printf("%ld\n",fib(i));
              }
            getch();
}
long int fib(int m)
{
            if(m<=2)
               return 1;
            else
               return fib(m-1)+fib(m-2);
}

9. A 5 digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5 digit number.

(i)           Using recursion
(ii)         Without using recursion

Using recursion
#include<stdio.h>
#include<conio.h>
long int Sum(long int digits );
void main()
{
            long int num,n,c;
            clrscr();
            printf("\nEnter 5 digits number : ");
            scanf("%ld",&n);
            c = Sum(n);
            printf("%ld",c);
            getch();

}
long int Sum(long int digits )
{
            if(digits < 10)
               return digits;
            else
               return digits%10 + Sum(digits/10);
}

Without using recursion
#include<stdio.h>
#include<conio.h>
void main()
{
           
            long int n;
            clrscr();
            printf("\nEnter 5 digits number : ");
            scanf("%ld",&n);
            long int(long int n);
            getch();
}
long int(long int n)
{
            int rdgt;
            long int num,sum = 0;
num = n;
            do
            {
                        rdgt = n%10;
                        n = n/10;
                        sum = (sum + rdgt);
                        }while(n !=0);
            printf("\nSum of Digits of the number %ld  is %ld ",num,sum);
            return 0;         
}



কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন