0% found this document useful (0 votes)
2 views8 pages

C Programs Using Functions

The document contains various C programs demonstrating the use of functions and recursion, including calculating the area of a rectangle, finding minimum and maximum of three numbers, checking for prime numbers, reversing a number, generating Fibonacci series, and summing a series of factorials. Each program includes code snippets, function declarations, and sample outputs for user inputs. The document serves as a practical guide for understanding basic programming concepts in C.

Uploaded by

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

C Programs Using Functions

The document contains various C programs demonstrating the use of functions and recursion, including calculating the area of a rectangle, finding minimum and maximum of three numbers, checking for prime numbers, reversing a number, generating Fibonacci series, and summing a series of factorials. Each program includes code snippets, function declarations, and sample outputs for user inputs. The document serves as a practical guide for understanding basic programming concepts in C.

Uploaded by

Science Fiction
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SAMPLE PROGRAMS based on FUNCTIONS

// C Program to Find the Area of a Rectangle Using Functions


#include<stdio.h>

// function declaration
float findArea(float l, float b);

int main()
{
float length, width, result;

printf("Enter the length and width of the rectangle:\n ");


scanf("%f %f", &length, &width);

result = findArea(length, width); //function call


printf("Area of rectangle = %.2f\n",result);

return 0;
}

// function to find area of rectangle


// function definition
float findArea(float l, float b)
{
float area;
area = l * b;
return area; //return statement
}

Enter the length and width of the rectangle:


56.9 45.8
Area of rectangle = 2606.02

Find Minimum & Maximum in Three Numbers Using Functions


#include<stdio.h>

// function declarations
float minimum(float a, float b, float c);
float maximum(float a, float b, float c);

int main()
{
float n1, n2, n3;

printf("Enter three numbers: ");


scanf("%f %f %f", &n1, &n2, &n3);

printf("Minimum number = %.2f\n",minimum(n1,n2,n3));


printf("Maximum number = %.2f\n",maximum(n1,n2,n3));
return 0;
}
// function for finding minimum of three numbers
float minimum(float a, float b, float c)
{
if(a<=b && a<=c) return a;
else if(b<=a && b<=c) return b;
else return c;
}

// function for finding maximum of three numbers


float maximum(float a, float b, float c)
{
if(a>=b && a>=c) return a;
else if(b>=a && b>=c) return b;
else return c;
}

Enter three numbers: 45.8


56.9
567.0
Minimum number = 45.80
Maximum number = 567.00

/* Write a program in C to check whether a number is a prime number or not using


the function */
#include<stdio.h>
// Function Declaration
int Primechecker(int);
int main()
{
int n1,prime;
printf("\n\n Function : check whether a number is prime number or not :\n");
printf(" Input a positive number : ");
scanf("%d",&n1);
prime = Primechecker(n1); // Function Call
if(prime==1)
printf(" The number %d is a prime number.\n",n1);
else
printf(" The number %d is not a prime number.\n",n1);
return 0;
}
int Primechecker(int n1) //Function Definition
{
int i=2;
while(i<=n1/2)
{
if(n1%i==0)
return 0;
else
i++;
}
return 1;
}

Function : check whether a number is prime number or not


: Input a positive number : 59
The number 59 is a prime number.

/*C Program to Reverse a Number using Function*/


#include<stdio.h>
reverse(int n)
{
int sum=0;
while (n!=0)
{
sum = sum*10 + n%10;
n /= 10;
}
return sum;
}

void main()
{
int rev, num;
printf("Enter a Positive Number: ");
scanf("%d", &num);
rev = reverse(num);
printf("The Reverse of given number %d is: %d", num, rev);
}

Enter a Positive Number: 3456


The Reverse of given number 3456 is: 6543
/* Display Fibonacci series in C within a range using a function */

#include<stdio.h>
void fibonacciSeries(int range)
{
int a=0, b=1, c;
while (a<=range)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}

int main()
{
int range;
printf("Enter range: ");
scanf("%d", &range);
printf("The Fibonacci series is: \n");
fibonacciSeries(range);

Enter range: 56
The Fibonacci series is:
0 1 1 2 3 5 8 13 21 34 55

/* Write a program in C to find the sum of the series


1!/1+2!/2+3!/3+4!/4+5!/5 using the function. */
#include <stdio.h>
int fact(int);
void main()
{
int sum;
sum=fact(1)/1+fact(2)/2+fact(3)/3+fact(4)/4+fact(5)/5;
printf("\n\n Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5
:\n"); printf("The sum of the series is : %d\n\n",sum);
}

int fact(int n)
{
int num=0,f=1;
while(num<=n-1)
{
f =f+f*num;
num++;
}
return f;
}
Sample Output:

Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5


: ----------------------------------------------------------

The sum of the series is : 34


SAMPLE PROGRAMS based on RECURSION

C Program to print sum of Natural Numbers Using Recursion


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

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}

Output

Enter a positive integer:3


sum = 6
C Program to calculate the factorial
of a number using recursion.
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial
you want to calculate:");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}

Output
Enter the number whose factorial you want to calculate:5
factorial = 120

We can understand the above program of the recursive method call by the figure given below:

// C Program to calculate the sum of digits of a given number using

recursion #include <stdio.h>

/* Function declaration */
int sumofdigits(int num);

int main()
{
int num, sum;
printf("Enter any number to find sum of digits: ");
scanf("%d", &num);

sum = sumofdigits(num);

printf("Sum of digits of %d = %d", num, sum);

return 0;
}

/*Recursive function to find sum of digits of a number */


int sumofdigits(int num)
{
// Base condition
if(num == 0)
return 0;

return ((num % 10) + sumofdigits(num / 10));


}

Enter any number to find sum of digits: 234


Sum of digits of 234 = 9
/**********************************************************
Program to reverse the digits of a number using recursion
**********************************************************
/

#include<stdio.h> // include stdio.h library


void reverse_num(int num);

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

reverse_num(num);
}

void reverse_num(int num)


{
int rem;

// base condition
if (num == 0)
{
return;
}
else
{
rem = num % 10; // get the rightmost digit
printf("%d", rem);
reverse_num(num/10); // recursive call
}
}

Enter a number: 3456


6543

// Compute Fibonacci numbers using recursion


#include<stdio.h>
int fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Enter Total terms to generate:");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", fibonacci(i));
i++;
}
}

int fibonacci(int n)
{
if(n == 0 || n == 1)
return n;

else
return ( fibonacci(n-1) + fibonacci(n-2) ); }

Enter Total terms to generate: 10


Fibonacci series terms are:
0
1
1
2
3
5
8
13
21
34

You might also like