Functions & Recursion

73. WRITE A C PROGRAM TO EXPLAIN FUNCTION PROTOTYPE:

/*program to explain function prototype*/
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
float square ( float x ) // function definition
{
float p ;
p = x * x ;
return ( p ) ;
}
output
Enter some number for finding square
2
Square of the given number 2.000000 is 4.000000


74. WRITE A C PROGRAM FUNCTION WITH NO ARGUMENTS & NO RETURN VALUE

/*C program Function with No Arguments & No Return Value*/
#include<stdio.h>
void area(); // Prototype Declaration
void main()
{
area();
}
void area()
{
float area_circle;
float rad;
printf("\nEnter the radius : ");
scanf("%f",&rad);
area_circle = 3.14 * rad * rad ;
printf("Area of Circle = %f",area_circle);
}

output
Enter the radius : 3
Area of Circle = 28.260000


75. WRITE A C PROGRAM FUNCTION WITH ARGUMENTS AND NO RETURN VALUE

/* C program Function with arguments and no Return Value */
#include<stdio.h>
#include<conio.h>
//----------------------------------------
void area(float rad); // Prototype Declaration
//----------------------------------------
void main()
{
float rad;
printf("nEnter the radius : ");
scanf("%f",&rad);
area(rad);
getch();
}
//----------------------------------------
void area(float rad)
{
float ar;
ar = 3.14 * rad * rad ;
printf("Area of Circle = %f",ar);
}
output
Enter the radius : 3
Area of Circle = 28.260000


76. write a C program Function with No arguments passed but a return value

/*C program Function with No arguments passed but a return value*/
#include<stdio.h>
#include<conio.h>

int send()
{
int no1;
printf("Enter a no : ");
scanf("%d",&no1);
return(no1);
}

void main()
{
int z;
clrscr();
z = send();
printf("\nYou entered : %d.", z);
getch();
}
output
enter a number : 5
you entered : 5.


77. WRITE A C PROGRAM FUNCTION WITH ARGUMENTS AND RETURN VALUE

/*C program Function with arguments and return value*/
#include<stdio.h>

int Multiplication(int, int); 

int main()
{
int a, b, Multi;

printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);

//Calling the function with dynamic values
Multi = Multiplication(a, b);

printf("\n Multiplication of %d and %d is = %d \n", a, b, Multi); 
return 0; 
}

int Multiplication(int a, int b)
{
int Multi; 

Multi = a * b;

return Multi;
}
output
please enter two integer values
30
60
multiplication of 30 and 60 is = 1800


78. WRITE A C PROGRAM LINEAR SEARCH C PROGRAM

/*C program Linear search c program*/
#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");
scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter the number to search\n");
scanf("%d", &search);

for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}
output
enter the number of elements in array
5
enter 5 numbers
5
6
4
2
9
enter the number to search
4
4 is present at location 3.


79. WRITE A C PROGRAM OF MATRIX ADDITION USING FUNCTION

/*program of addition of two given 3X3 matrix through function*/
#include<stdio.h>
#include<conio.h>
void show_matrix(int mat[3][3]);
void add_matrix(int matA[3][3], int matB[3][3], intmatSum[3][3]);
int main()
{
int x[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int y[3][3] = { {1,4,7}, {2,5,8}, {4,1,2} };
int z[3][3];
add_matrix(x,y,z);
printf("\nFirst matrix is :\n");
show_matrix(x);
printf("\nSecond matrix is :\n");
show_matrix(y);
printf("\nNew addition matrix is :\n");
show_matrix(z);
getch();
return 0;
}

void add_matrix(int matA[3][3], int matB[3][3], intmatSum[3][3])
{
int r,c;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
matSum[r][c]=matA[r][c]+matB[r][c];
}
}

void show_matrix(int mat[3][3])
{
int r,c;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
printf(" %d",mat[r][c]);
printf("\n");
}
}
output
First matrix is :
1 2 3
4 5 6
7 8 9


Second matrix is :
1 4 7
2 5 8
4 1 2


New addition matrix is :
2  6  10
6 10  14
11 9  11


80. WRITE A C PROGRAM PASSING ARGUMENT TO FUNCTION : CALL BY VALUE

/*C program Passing Argument to Function : call by value*/
#include<stdio.h>
void interchange(int number1,int number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}
int main() {
int num1=50,num2=70;
interchange(num1,num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
output
Number 1 : 50
Number 2 : 70


81. WRITE A C PROGRAM PASSING ARGUMENT TO FUNCTION : CALL BY REFERENCE(ADRESS)

/* C program Passing Argument to Function : call by reference(adress)*/
#include<stdio.h>
void interchange(int *num1,int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main() {
int num1=50,num2=70;
interchange(&num1,&num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
output
Number 1 : 70
Number 2 : 50


82. WRITE A C PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING RECURSION

/* C Program to find factorial of a given number using recursion*/
#include <stdio.h>

int factorial(int);

int main()
{
int num;
int result;

printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
output
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.


83. WRITE A C PROGRAM TO PRINT FIBONACCI SERIES TILL NTH TERM USING RECURSION

/* C Program to print fibonacci series using recursion*/
#include <stdio.h>
#include <conio.h>

int fibonacci(int term);
int main(){
int terms, counter;
printf("Enter number of terms in Fibonacci series: ");
scanf("%d", &terms);
/*
* Nth term = (N-1)th therm + (N-2)th term;
*/
printf("Fibonacci series till %d terms\n", terms); 
for(counter = 0; counter < terms; counter++){
printf("%d ", fibonacci(counter));
}
getch();
return 0;
}
/*
* Function to calculate Nth Fibonacci number
* fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
*/
int fibonacci(int term){
/* Exit condition of recursion*/
if(term < 2)
return term;
return fibonacci(term - 1) + fibonacci(term - 2);
}
output
Enter number of terms in Fibonacci series: 9
Fibonacci series till 9 terms
0 1 1 2 3 5 8 13 21


84. WRITE A C PROGRAM TO PRINT GCD OF TWO NUMBERS USING RECURSION

/*C program to print GCD of Two Numbers using Recursion*/
#include<stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2));
return 0;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2);
else 
return n1;
}
output
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.

Comments