0% found this document useful (0 votes)
8 views

Function, Scope and Recursion

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Function, Scope and Recursion

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Practice all programs in your local machine or through online C compilers.

C Code Examples: https://www.programiz.com/c-programming/examples


Online C Compiler: https://www.programiz.com/c-programming/online-compiler/

FUNCTION
Q.1 Add two nos using function
#include <stdio.h>
void add(float a1, float b1)
{
float res;
res=a1+b1;
printf("Result = %f",res);
}
int main()
{
float a,b,c;
printf("\n Enter two nos");
scanf("%f %f",&a,&b);
add(a,b);
return 0;
}
Q.2 Add two nos using function and return result back to main and print
#include <stdio.h>
void add()
{
Int c;
c=a+b;
printf("Result = %f",c);
}
int main()
{
float a,b,c;
printf("\n Enter two nos");
scanf("%f %f",&a,&b);

add(a,b);
return 0;
}
Q.3 Add two nos using function and return result back to main and print (function
protype)
#include <stdio.h>
int add(int a,int b); // or int add(int,int); function declaration or function prototype
int main()
{
int a,b,res;
printf("enter 2 nos");
scanf("%d %d", &a,&b);
res=add(a,b); // function calling
printf("\n result is : %d",res);
return 0;
}
int add(int a,int b) // function defination
{
printf("\n Welcome to function add");
int total;
total=a+b;
return total;
}
Q.4 Odd or even using function and return result back (option 1)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
res=evenorodd(a); // function calling
if(res==0)printf ("\n even");
else printf ("\n odd");
return 0;
}
int evenorodd(int a) // function defination
{
printf("\n Welcome to function add");
if (a%2==0) return 0;
else return 1;
}
Q.4 Odd or Even using function and return result back (option 2)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
res=evenorodd(a); // function calling
if(res==0)printf ("\n even");
else printf ("\n odd");
return 0;
}
int evenorodd(int a) // function defination
{
printf("\n Welcome to function add");
return(a%2);
}
Q.4 Odd or Even using function and return result back (option 3)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
if(evenorodd(a)==0) // function calling
printf ("\n even");
else printf ("\n odd");
return 0;
}

int evenorodd(int a) // function defination


{
printf("\n Welcome to function add");
return(a%2);
}
Q.4 Odd or Even using function and return result back (option 4)
#include <stdio.h>
int evenorodd(int a); // or int add(int,int); function declaration or function prototype
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
if(evenorodd(a)) // function calling
printf ("\n odd");
else printf ("\n even");
return 0;
}
int evenorodd(int a) // function defination
{
printf("\n Welcome to function add");
return (a%2);
}
Q.5 Find factorial using function and return result back to the main
#include <stdio.h>
int f(int a) // function defination
{
int fact=1, i;
for(i=1;i<=a;i++)
fact=fact*i;
return (fact);
}
int main()
{
int a,res;
printf("enter 1 no");
scanf("%d", &a);
res=f(a);
printf("\n result = %d",res);
return 0;
}
Q.6 Checking a number is prime using function (option 1)
#include <stdio.h>
void p(int a) // function defination
{
int flag=1, i;
for(i=2;i<a;i++)
{
if(a%i==0)
{
flag=0;
printf("\n Not prime");
break;
}
}
if(flag==1) printf("\n Prime");
}

int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
p(a);
return 0;
}
Q.6 Checking a number is prime using function and return the result bac to the main
(option 2)
#include <stdio.h>
int p(int a) // function defination
{
int flag=1, i;
for(i=2;i<a;i++)
{
if(a%i==0)
{
flag=0;
break;
}
}
return flag;
}

int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
if(p(a)) printf("\n Prime");
else printf("\n non prime");
return 0;
}

Q.7 Print factors of a number using function


#include <stdio.h>
void factors(int a) // function defination
{
int flag=1, i;
for(i=1;i<=a;i++)
{
if(a%i==0)
{
printf("\n Factor is %d",i);
}
}
}

int main()
{
int a;
printf("enter 1 no");
scanf("%d", &a);
factors(a);
return 0;
}
Q.8 Calculate power using function
#include <stdio.h>
void powercalc(int, int);
int main()
{
int base, power;
printf(" Enter base and power");
scanf("%d %d",&base,&power);
powercalc(base, power);
return 0;
}

void powercalc(int base, int power)


{
int result=1,i;
for(i=1;i<=power;i++)
result=result*base;
printf("%d ^ %d = %d", base, power, result);
}
Q 9. function- Palindrome (option 1)
#include <stdio.h>
void palin(int num);

int main()
{
int num;
printf("\n Enter the number :");
scanf("%d",&num);
palin(num);
return 0;
}

void palin(int num)


{
int rev=0,m=num;
while(num>0)
{
rev=rev*10+num%10;
num=num/10;
}
printf("\n Reverse is : %d",rev);
if(m==rev)
printf("\n Its a palindrome");
else
printf("\n It is not a palindrome");
}

Q 9. function- Palindrome (option 2) function returns a value


#include <stdio.h>
int palin(int); // fun declaration / protype
int main()
{
int num,res;
printf("Enter a no");
scanf("%d",&num);
res=palin(num); // fun calling
if(res==1) printf("\n No is Palindrome");
else printf("\n No is NOT a Palindrome");
return 0;
}

int palin(int num) // fun defination


{
int n1=num;
int rev=0,rem;
while(num>0)
{
rem=num%10; // combine 1 and 2
rev=rev*10+rem; //or rev=rev*10+num%10
num=num/10; // num/=10
}
if(rev==n1) return 1; // return true that no is a palindrome
else return 0; // return true that no is NOT a palindrome

}
Q. 9 User defined function calling another function- Palindrome (option 2)

#include <stdio.h>
void palin(int num);
void input ();
int main()
{
input();
return 0;
}
void input ()
{
int num;
printf("\n Enter the number :");
scanf("%d",&num);
palin(num);
}
void palin(int num)
{
int rev=0,m=num;
while(num>0)
{
rev=rev*10+num%10;
num=num/10;
}
printf("\n Reverse is : %d",rev);
if(m==rev)
printf("\n Its a palindrom");
else
printf("\n It is not a palindrom");
}

Q.10 Converting a number in binary to octal


#include <stdio.h>
int dectooct(int); // fun declaration / protype
int main()
{
int num,res;
printf("Enter a no");
scanf("%d",&num);
res=dectooct(num); // fun calling

return 0;
}

int dectooct(int num) // fun defination


{
int A[32];
int rem,i=0;
while(num>0)
{
rem=num%8;
A[i]=rem;
i++;
num=num/8;
}
i--;
while(i>=0)
{
printf(" %d ",A[i]);
i--;
}

Q.11 Factors of a no

#include <stdio.h>
void factors(int); // fun declaration / protype
int main()
{
int num;
printf("Enter a no");
scanf("%d",&num);
factors(num); // fun calling

return 0;
}

void factors(int num) // fun defination


{
int i;
for(i=1;i<=num;i++)
{
if(num%i==0) printf("\n %d",i);
}
}
SCOPE OF A VARIABLE
Recursion
#include <stdio.h>
void main()
{
int i=1;
printf("Hello World");
i++;
if(i<=5)
main();

}
Hello world is printed only infinite times since the variable i is declared new after every function
call

Q. Recursion Example
#include <stdio.h>
void count_to_ten ( int count )
{
//we only keep counting if we have a value less than ten
if ( count < 10 )
{

count_to_ten( count + 1 );
printf(" \n %d",count);
}
}
int main()
{
count_to_ten ( 0 );
}

Print 9…0
Another example
#include <stdio.h>
void count_to_ten ( int count )
{
//we only keep counting if we have a value less than ten
if ( count < 10 )
{
printf(" \n %d",count);

count_to_ten( count + 1 );
}
}
int main()
{
count_to_ten ( 0 );
}

Print 0…9
Q Static variable
#include <stdio.h>
void main()
{
static int i=1;
printf("Hello World");
i++;
if(i<=5)
main();

}
Hello world is printed only 5 times since the variable is static (it retains its value)
Q.1 Global variable
#include <stdio.h>
int f=10;
void fun();
int main()
{
printf(" %d",f++);
fun();
return 0;
}

void fun()
{
printf(" %d",f);

}
OUTPUT: 10 11
Q.2 Global variable vs local variable

#include <stdio.h>
int f=10;
void fun();
int main()
{
int f=5;
printf(" %d",f++);
fun();
return 0;
}

void fun()
{
printf(" %d",f);
}
OUTPUT: 5 10

RECURSION (DIVIDE and CONQUER)


Question from ppt 17
Extra questions
https://www.includehelp.com/c-programs/recursion-examples.aspx
PRCATICE QUESTION
NORMAL PROGRAM
#include <stdio.h>
int main()
{
int b,p,i,res=1;
printf("\n Enter the base, power");
scanf("%d %d",&b,&p); // b=2, p=5 => 2*2*2*2*2
for(i=1;i<=p;i++)
{
res=res*b;
}
printf("\n %d ^ %d = %d",b,p,res);
return 0;
}
USING USER DEFINED FUNCTION
#include <stdio.h>
int power1(int b,int p)
{
int res=1,i;
for(i=1;i<=p;i++)
{
res=res*b;
}
return res;
}
int main()
{
int b,p,i,res=1;
printf("\n Enter the base, power");
scanf("%d %d",&b,&p); // b=2, p=5 => 2*2*2*2*2
res=power1(b,p);
printf("\n %d ^ %d = %d",b,p,res);
return 0;
}
USING RECURSION
#include <stdio.h>
int power1(int b,int p)
{
if(p==0)
return 1;
else
return b*power1(b,p-1);
}
int main()
{
int b,p,i,res=1;
printf("\n Enter the base, power");
scanf("%d %d",&b,&p); // b=2, p=5 => 2*2*2*2*2
res=power1(b,p);
printf("\n %d ^ %d = %d",b,p,res);
return 0;
}

You might also like