Showing posts with label Programs - UDF. Show all posts
Showing posts with label Programs - UDF. Show all posts

Friday 30 March 2012

Programs - UDF


Program - 1 : Create UDF  name prime which accept one argument of integer type and display that the number is prime or not.

#include<stdio.h>
#include<conio.h>

void prime(int);
void main()
{

    int num;
    clrscr();

    printf("\n\nEnter a number: ");
    scanf("%d",&num);
    prime(num);
    getch();

}

void prime(int num)
{

  int i,count=0;
  for(i=1;i<=num;i++)
    {
          if(num%i==0)
          {
              count++;
         
          }

    }

   if(count==1 || count== 2)
          printf("\n\n %d is a prime number",num);

   else
          printf("\n\n %d is not a prime number",num);
}


Program – 2 : Create UDF named lower() which converts the character in lower case

#include<stdio.h>
#include<conio.h>

void lower(char);
void main()

{
          int a;
          char str;
          clrscr();

          printf("\n Enter character you want convert in lowercase : ");
          str=getchar();
         printf(“\n Enterd character is : %d”,str);
          lower(str);
          getch();
}

void lower(char str)
{
          if(str>=65 && str<=90)
                   {
                   str=str+32;
                   }
          printf("Lower case :    %c",str);

}


Program – 3 : Create UDF named upper() which converts the character in upper case.

#include<stdio.h>
#include<conio.h>

void upper(char);

void main()

{
          int a;
          char str;
          clrscr();

          printf("\n Enter character you want to convert in uppercase :");
          str=getchar();
printf(“\n Enterd character is : %d”,str);

          upper(str);
          getch();
}
void upper(char str)
{
          if(str>=97 && str<=122)
                   {
                   str=str-32;
                   }
          printf("UPPER CASE :    %c",str);


}
Program – 4 : Write UDF power to calculate X ^ Y

#include<stdio.h>
#include<conio.h>


double power(int,int);

void main()
{
   int x,y;
   double ans;
   clrscr();
   printf("\n enter value of x : ");
   scanf("%d",&x);
   printf("\n Enter value of y : ");
   scanf("%d",&y);

   ans=power(x,y);
   printf("\n answer = %lf",ans);

   getch();
}

double power(int x,int y)
 {
   double ans=1.0;
   int i;
 
  if(y>=0)
   {
     while(y>=1)
      {
           ans=ans*x;
           y--;
      }
   }
  else
   {
     while(y<0)
      {

           ans=ans/x;
           y++;
      }
   }
 return(ans);
 }







Program – 5 :  Write a program to  find factorial of given number using recursion

#include<stdio.h>
#include<conio.h>

float fact(int);
void main()
 {
    int n;
    float f;
    clrscr();
    printf("\n Enter any number : ");
    scanf("%d",&n);
    f=fact(n);
    printf("\n Factorial of %d = %f",n,f);
    getch();
 }
float fact(int n)
 {
    printf("\n %d",n);
    if(n==1)
      {
          return(1);
      }
    else
      {
          return(n*fact(n-1));
      }
 }

Program – 6 : Write a program to Find out fibonacci series using recursion

#include<stdio.h>
#include<conio.h>

int fibo(int n);

void main()
 {
          int n;
          int i;
          clrscr();
          printf("Enter any number for fibinacci series : ");
          scanf("%d",&n);
          i=0;
          while(i<n)
          {
                   ++i;
                    printf("\t %d",fibo(i));
          }
    getch();
}

int fibo(int n)
 { int y;

   if((n==1) || (n==0))
     {
            return 0;
     }
   else
      {
             if(n==2)
              {
                 return 1;
              }
             else
              {
                   return(fibo(n-1) + fibo(n-2));
               }
      }
}


Program – 7 : Write a program to find x^y power using recursion.

#include<stdio.h>
#include<conio.h>


double power(int,int);
void main()
{
   int x,y;
   double ans;
   clrscr();
   printf("\n enter value of x : ");
   scanf("%d",&x);
   printf("\n Enter value of y : ");
   scanf("%d",&y);
   if(y<0)
    {
      ans=1/(power(x,y));
    }
   else
    {
      ans=power(x,y);
    }
          printf("\n answer = %lf",ans);

   getch();
}

double power(int x,int y)
 {
   if(y==0)
    {
     return(1);

    }
  else if(y<0)
   {
     return(x*(power(x,y+1)));
   }
  else
   {
     return(x*(power(x,y-1)));
   }
 }

Program – 8 : Write a program for  Array as function argument

#include<stdio.h>
#include<conio.h>

void output(int no[],int n);
void main()
{
   int no[50],i,n;
   int no2[]={1,2,3};
   clrscr();
  
   printf("\n Enter how many number : ");
   scanf("%d",&n);
   for(i=0;i<n;i++)
    {
          printf("\n Enter value of number %d : ",i+1);
          scanf("%d",&no[i]);
    }
   output(no,n);
   output(no2,3);
   getch();
}

void output(int no[],int n)
{
    int i;
    int s=0;
    float avg;
    printf("\n output ");

    for(i=0;i<n;i++)
    {
          printf("\n%d",no[i]);
          s=s+no[i];
    }

    avg=(float)s/n;
    printf("\n Sum= %d",s);
    printf("\n Average = %f",avg);

}

Program -  9 : W.A.P. to calculate total,sum and average  of odd and  even numbers using    UDF.

#include<stdio.h>
#include<conio.h>
void oddeven(int *,int);
void main()
{
          int x,a[100],*p,n;

          clrscr();
          printf("\n\nEnter number you want to sum or avg :");
          scanf("%d",&n);

          for(x=0;x<n;x++)
          {
           printf("\n(%d)Enter number : ",x+1);
           scanf("%d",&a[x]);
          }
          p=&a[0];
          oddeven(p,n);
          getch();

}

void oddeven(int *p,int n)
{
          int o,e,osum,esum,x;
          float oavg,eavg;
          oavg=eavg=0;
          osum=esum=o=e=0;

          printf("\n___________________________\n");
          printf("\n\nlist even number \n");

          for(x=0;x<n;x++)
          {
          if(*(p+x)%2==0)
                   {
                   printf("\n%d",*(p+x));
                   ++e;
                   esum=esum+*(p+x);
                   }
          }
          eavg=(float)esum/e;
          printf("\ntotal of even number =%d",esum);
          printf("\naverage of total even number =%.2f",eavg);

          printf("\n___________________________\n");
          printf("\n\nlist odd number \n");
          for(x=0;x<n;x++)
          {
          if(*(p+x)%2!=0)
                   {
                   printf("\n%d",*(p+x));
                   ++o;
                   osum=osum+*(p+x);
                   }
          }
          oavg=(float)osum/o;
          printf("\ntotal of odd number =%d",osum);
          printf("\naverage of total odd number =%.2f",oavg);

}

Program – 10 :  W.A.P. to create UDF which pass argument and find maximum and minimum number.

#include<stdio.h>
#include<conio.h>

void minmax(int *,int);
void main()

{
          int i,p[50],*a,n;
          clrscr();
          printf("\n Enter How many No :  ");
          scanf("%d",&n);

          for(i=0;i<n;i++)
          {
                   printf("\n(%d) Enter No : ",i+1);
                   scanf("%d",&p[i]);
          }

          a=&p[0];
          minmax(a,n);

          getch();
}

void minmax(int *a,int p)

{
     int i,min,t,max;
     max=*a;

     for(i=0;i<p;i++)
     {
          if(*a>max)
                   max=*a;
          a++;
     }

     a=a-p;
     min=*a;

     for(i=0;i<p;i++)
     {
          if(*a<min)
                   min=*a;
          a++;
     }

     printf("\nMax No : %d",max);
     printf("\nMin No : %d",min);
}

Program – 11 :  W.A.P. arrange numbers in descending order using UDF

#include<stdio.h>
#include<conio.h>
void descend(int a[],int);
void output(int a[],int);
void main()
{
          int a[50],i,n;
          clrscr();

          printf("\nEnter any Number you want to descend :\n\n");
          scanf("%d",&n);

          for(i=0;i<n;i++)
          {
           printf("\n(%d) Enter No :",i+1);
           scanf("%d",&a[i]);
          }

          printf("\n Before sorting ");
          output(a,n);

          descend(a,n);

          printf("\n After sorting ");
          output(a,n);

          getch();

}

void descend(int a[],int n)
{
          int i,j,t,max;

          printf("\n\nDescending order :\n\n");
           for(i=0;i<n;i++)
          {
           for(j=i+1;j<n;j++)
            {
                if(a[i]<a[j])
                   {
                    max=a[i];
                    a[i]=a[j];
                    a[j]=max;
                   }

            }
            printf("\n%d\n",a[i]);
          }

}

void output(int a[],int n)
 {
    int i;
    for(i=0;i<n;i++)
      {
         printf("\n %d",a[i]);
      }
  }

 Program – 12 : Write a program to create and print matrix using UDF

// Two dimensional array as function argument
#include<stdio.h>
#include<conio.h>

void input(int mat[][3],int r,int c);
void output(int mat[][3],int r,int c);
void main()
          {
                   int i,j;
                   int mat1[3][3],mat2[3][3],mat3[3][3];
                   clrscr();

                   printf("\n Input Matrix -1  \n" );
                   input(mat1,3,3);

                   printf("\n Input Matrix -2  \n" );
                   input(mat2,3,3);
                    //add_mat(mart1,mat2,mat3,3,3);
                    //output(mat3,3,3);

                   printf("\n output  Matrix-1 \n" );
                   output(mat1,3,3);

                   printf("\n output  Matrix-2 \n" );
                   output(mat2,3,3);

             getch();
          }

void input(int mat[][3],int r,int c)
 {
          int i,j;
          for(i=0;i<r;i++)
                   {
                             for(j=0;j<c;j++)
                             {
                             printf("\n enter element [%d][%d] : ",i+1,j+1);
                             scanf("%d",&mat[i][j]);
                             }
                   }
  }

void output(int mat[][3],int r,int c)
 {
   int i,j;
                   for(i=0;i<r;i++)
                   {
                             for(j=0;j<c;j++)
                             {
                                      printf("%4d",mat[i][j]);
                             }
                             printf("\n");
                   }
 }


Program – 13 :  Structure as function argument addition of two matrix
#include<stdio.h>
#include<conio.h>

void input(int mat[10][10],int r,int c);
void output(int mat[10][10],int r,int c);
void add_mat(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r,int c);

void main()
 {
    int mat1[10][10],mat2[10][10],r,c;

    clrscr();
    printf("\n Enter row : ");
    scanf("%d",&r);

    printf("\n Enter column : ");
    scanf("%d",&c);

    printf("\n Input matrix-1 \n");
    input(mat1,r,c);

    printf("\n Input matrix-2 \n");
    input(mat2,r,c);

    printf("\n output matrix-1 \n");
    output(mat1,r,c);

    printf("\n output matrix-2 \n");
    output(mat2,r,c);


    add_mat(mat1,mat2,mat3,r,c);
    printf("\n Addition of two matrix \n");
    output(mat3,r,c);


    getch();
 }

 void input(int mat[10][10],int r,int c)
  {
     int i,j;

     for(i=0;i<r;i++)
      {
 for(j=0;j<c;j++)
   {
      printf("\n enter element : ");
      scanf("%d",&mat[i][j]);
   }
     }
 }

 void output(int mat[10][10],int r,int c)
  {
     int i,j;

     for(i=0;i<r;i++)
      {
 for(j=0;j<c;j++)
   {
      printf("%4d",mat[i][j]);

   }
   printf("\n");

     }
 }
void add_mat(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r,int c)
{
     int i,j;
     for(i=0;i<r;i++)
      {
 for(j=0;j<c;j++)
   {
      mat3[i][j]=mat1[i][j]+mat2[i][j];
   }
     }
 }

      
 Program – 14 :  Structure as function argument

#include<stdio.h>
#include<conio.h>
void display(struct emp e);

struct emp
{
          int no;
          char name[15];
          float salary;
};
void main()
{
          struct emp e1={1,"xyz",5000};
          struct emp e2={2,"abc",6000};
          struct emp e;
          int i;
          clrscr();
          printf("\n enter no     : ");
          scanf("%d",&e.no);
          fflush(stdin);
          printf("\n Enter name   : ");
          gets(e.name);
          printf("\n Enter salary : ");
          scanf("%f",&e.salary);

          display(e1);
          display(e2);
          display(e);
          getch();
}
void display(struct emp e)
 {

          printf("\n No     : %d ",e.no);
          printf("\n Name       : %s ",e.name);
          printf("\n Salary : %f ",e.salary);
          printf("\n *************************************");

 }

Program – 15 :  Array of structure as function argument

#include<stdio.h>
#include<conio.h>


struct emp
{
          int no;
          char name[15];
          long salary;
};

void input(struct emp e[],int n);
void output(struct emp e[],int n);

void main()
{
          struct emp e[10];
          int i;
          clrscr();
          input(e,2);
          output(e,2);
          getch();
}
void input(struct emp e[],int n)
{
  int i;
  for(i=0;i<2;i++)
           {     printf("\n Record === %d",i+1);
                   printf("\n enter no     : ");
                   scanf("%d",&e[i].no);
                   fflush(stdin);
                   printf("\n Enter name   : ");
                   gets(e[i].name);
                   printf("\n Enter salary : ");
                   scanf("%ld",&e[i].salary);
           }
 }

void output(struct emp e[],int n)
 {
    int i;
    for(i=0;i<n;i++)
     {
          printf("\n No     : %d ",e[i].no);
          printf("\n Name       : %s ",e[i].name);
          printf("\n Salary : %ld ",e[i].salary);
          printf("\n -----------------------------");
    }
 }

Program – 16 : Write a program for passing pointer to structure

#include<stdio.h>
#include<conio.h>
void display(char *);
void main()
 {
   char *s1,*s2;
   clrscr();
   printf("\n Enter any string 1 : ");
   gets(s1);
   printf("\n Enter any string 2 : ");
   gets(s2);
  printf("\n String -1 ");

   display(s1);
   printf("\n String -2 ");
   display(s2);
   getch();
 }
 void display(char *s)
  {

    while(*s!='\0')
     {
          printf("\n %c",*s);
          s++;

  }
  }

Program – 17 : Write function len() to fing length of string

#include<stdio.h>
#include<conio.h>
int len(char *);
void main()
 {
   char *s;
   int l=0;
   clrscr();
   printf("\n Enter any String : ");
   gets(s);
   printf("\n String is : %s",s);
   l=len(s);
   printf("\n length is : %d",l);
   getch();
 }
 int len(char *s)
  {
    int l=0;
    while(*s!='\0')
     {
          l++;
          s++;

   }
   return(l);
  }


 Posted By : Ruchita Pandya