FUNCTION & RECURSION

 Chapter 3

Function








Q1.WAP to print your name 5 times using function.

#include<stdio.h>
#include<conio.h>
void name();
int main()
{
  name();
 return 0;
}
void name()
{
  for(int i=0;i<5;i++)
  {
    printf("Prajwol Shrestha!!\n");
  }
}


Q2.WAP to calculate the sum of first n natural no using function.

#include<stdio.h>
#include<conio.h>
void sum(int n);

int main()
{int n;
printf("enter the number:");
scanf("%d",&n);
sum(5);

 return 0;
}
void sum( int a)
{int s=0;
  for(int i=1;i<=a;i++)
  {
   s=s+i;
  }
  printf("sum of all natural number upto %d is %d",a,s);
}


Q3.WAP in c to check armstrong number using function.( display output in user defined function).

#include<stdio.h>
#include<conio.h>
void armstrong(int n);

int main()
{
  int n;
printf("enter a number:");
scanf("%d",&n);
armstrong(n);


 return 0;
}
void armstrong( int a)
{int r,d=0,c;
c=a;

 while(a!=0)
{
 r=a%10;
 d=(r*r*r)+d;
 a=a/10;
 
}
if(d==c)
{
  printf("given no is armstrong");
}
else
{
  printf("not armstrong");
}
 
 

}


Q4.WAP in c to check a perfect number using a function.(display output in main function).

#include<stdio.h>
#include<conio.h>
char perfect(int n);

int main()
{
  int n;
  char c;

printf("enter a number:");
scanf("%d",&n);
c=perfect(n);
if (c=='T')
{
  printf("Given number is perfect");
}
else
{
  printf("not a perfect number");
}

 return 0;
}
char perfect( int a)
{int i,sum=0;

for(i=1;i<a;i++)
{
  if(a%i==0)
  {
    sum=sum+i;
  }
}

 
if(sum==a)
{
  return ('T');
}
else
{
 return ('F');
}
}


Q5.WAP in c to check whether a given no is palindrome or not.

#include<stdio.h>
#include<conio.h>
char palindrome(int n);

int main()
{int n;
char c;
printf("enter a number:");
scanf("%d",&n);
c=palindrome(n);
if(c=='T')
{
  printf("palindrome");
}
else
{
  printf("not palindrome");
}
 
 return 0;
}
char palindrome( int a)
{int r,d=0,f;
f=a;
 while(a!=0)
 {
  r=a%10;
  d=r+(d*10);
  a=a/10;
 }
if(d==f)
{
  return('T');
}
else
{
  return('F');
}
 
}


Q6.WAP in c the decimal no into binary number.

#include<stdio.h>
#include<conio.h>
void bin2dec(int n);

int main()
{int n;
printf("enter a number:");
scanf("%d",&n);
bin2dec(n);

 
 return 0;
}
void bin2dec( int a)
{int b[10],r;
int i=0,j=0;
 while(a!=0)
 {
  r=a%2;
  b[i]=r;
  a=a/2;
  i++;
 }
 printf("binary value is \n");
 for(j=i-1;j>=0;j--)
 {
  printf("%d ",b[j]);
 }


}



Q7.WAP in c to convert binary value into decimal number.

#include<stdio.h>
#include<conio.h>
void dec2bin(int n);

int main()
{int n;
printf("enter a binary number:");
scanf("%d",&n);
dec2bin(n);
 return 0;
}
void dec2bin( int a)
{int r,d=0,base=1;
 while(a!=0)
{
  r=a%10;
  d=d+r*base;
  a=a/10;
  base=base*2;
}
printf("decimal number is %d",d);
}


Q7.WAP to find the smallest no. in an array using function.

#include<stdio.h>
#include<conio.h>
void smallarr(int[],int);
int main()
{
  int n,a[100];
  printf("enter the size of array:");
  scanf("%d",&n);
  printf("enter the elements of array:");
  for(int i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
 
  smallarr(a,n);
return 0;

}
void smallarr( int a[],int b)
{ int i,j,temp=0;
   for(i=0;i<b;i++)
   {
    for(j=i+1;j<b;j++)
    {
      if(a[i]<a[j])
      temp=a[i];
      a[i]=a[j];
      a[j]=temp;
    }

   }
   printf("smallest elements of array is %d",a[b-1]);
}


Q8.WAP in c to read an array using function.

#include<stdio.h>
#include<conio.h>
void readarr(int[],int);
int main()
{
  int n,a[100];
  printf("enter the size of array:");
  scanf("%d",&n);
  printf("enter the elements of array:");
  for(int i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("inputed array is\n");
  readarr(a,n);
return 0;

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


Q10.WAP to find the square of a given number.

#include<stdio.h>
#include<conio.h>
int square(int);
int main()
{ int s,n;
   printf("enter the number:");
  scanf("%d",&n);
  s=square(n);
  printf("Square of given number is %d",s);
return 0;
}
int square( int a)
{
  return a*a;
}

# RECURSION 

Q1.WAP to print your name using recursion.

#include<stdio.h>
#include<conio.h>
void name(int n);

int main()
{int n;
  printf("enter a number for recursion:");
  scanf("%d",&n);
  name(n);

 return 0;
}
void name(int count)
{if(count==0)
  {
    return ;
  }
  printf("Prajwol Shrestha\n");
  name(count-1);
}


Q2.WAP to in c to find sum of all natural number between 1 to n using recursion.

#include<stdio.h>
#include<conio.h>
int sum(int);
int main()
{ int n;
   printf("enter the number:");
  scanf("%d",&n);
  printf("sum of all natural no up to %d is %d",n,sum(n));
 
return 0;
}
int sum( int a)
{ if(a==1)
 {return 1;
  }
  int SNM1=sum(a-1);
  int sum=SNM1+a;
  return(sum);
}


Q3.WAP in c to find the factorial of a given number.

#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{ int n;
   printf("enter the number:");
  scanf("%d",&n);
  printf("factorial of %d is %d",n,fact(n));
 
return 0;
}
int fact( int a)
{ if(a==1)
 {return 1;
  }
  int FNM1=fact(a-1);
  int factn=FNM1*a;
  return(factn);
}


Q4.WAP in c to find the L.C.M of given two numbers using recursion.


#include<stdio.h>
#include<conio.h>
int LCM(int,int);
int main()
{ int x,y;
printf("enter two numbers:");
scanf("%d%d",&x,&y);
  printf("lcm of given two number is %d",LCM(x,y));
return 0;
}
int LCM( int a,int b)
{
 static int m=0;
 m=m+a;
 if(m%a==0 && m%b==0)
 {
  return m;
 }
 return LCM(a,b);
}























Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.