In this article function program in c we give the simple, conditional, looping based program are covered using user defined function and show the proper output in details.
Function program in C:
// Function with arguments but return a value
// Write a C Program to calculate addition of two number using function.
#include<stdio.h>
#include<conio.h>
int total(int a, int b); // function declaration
void main()
{
int a,b,sum=0;
clrscr();
printf(“\n Enter the two numbers: “);
scanf(“%d%d”,&a,&b);
sum=total(a,b); // function call
printf(“\n\n Addition of Two numbers: %d”,sum);
getch();
}
int total(int a, int b) // function defination
{
return(a+b);
}
Output:
Enter the two numbers: 8
9
Addition of Two numbers: 17
// To find area of rectangle using user defined function
#include<stdio.h>
#include<conio.h>
int area_rect(int ,int ); // function declaration
void main()
{
int l,b,area=0;
clrscr();
printf(“\n\n Enter the length and breadth: “);
scanf(“%d%d”,&l,&b);
area=area_rect(l,b); // function call
printf(“\n\n Area of Rectangle: %d”,area);
getch();
}
int area_rect(int l,int b) // function defination
{
return(l*b);
}
Output:
Enter the length and breadth: 6
4
Area of Rectangle: 24
/* Write a C Program to interchange two values
by using user defined function */
#include<stdio.h>
#include<conio.h>
void swap(int,int); // function declaration
void main()
{
int a,b;
clrscr();
printf(“\n Enter the two numbers: “);
scanf(“%d%d”,&a,&b);
swap(a,b); // function call
getch();
}
void swap(int a, int b) // function defination
{
int temp;
printf(“\n\n Before Swap \n”);
printf(“\t%d\t%d”,a,b);
temp=a;
a=b;
b=temp;
printf(“\n\n After Swap \n”);
printf(“\t%d\t%d”,a,b);
}
Output:
Enter the two numbers: 50
100
Before Swap
50 100
After Swap
100 50
Function program in C:
/* Write a C Program to find given number is odd/even
by using user defined function */
#include<stdio.h>
#include<conio.h>
void oddeven(int); // function declaration
void main()
{
int n;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&n);
oddeven(n); // function call
getch();
}
void oddeven(int n) // function defination
{
if(n%2==0)
printf(“\n %d is even number.”, n);
else
printf(“\n %d is odd number.”, n);
}
Output:
Enter the number: 100
100 is even number.
/* Write a C Program to find smallest number among three
Different numbers using function */
#include<stdio.h>
#include<conio.h>
void smallest3(int,int,int); // function declaration
void main()
{
int x,y,z;
clrscr();
printf(“\n Enter the three numbers: “);
scanf(“%d%d%d”,&x,&y,&z);
smallest3(x,y,z); // function call
getch();
}
void smallest3(int x, int y, int z) // function defination
{
if(x<y && x<z)
{
printf(“\n First number is Smallest number: %d”, x);
}
else
if(y<z)
{
printf(“\n Second number is Smallest number: %d”, y);
}
else
{
printf(“\n Thired number is Smallest number: %d”, z);
}
}
Output:
Enter the three numbers: 10
20
30
First number is smallest number: 10
/* Write a C Program to show sum of digits
using user defined function */
#include<stdio.h>
#include<conio.h>
int sumdig(int n); // function declaraion
void main()
{
int n,sumd=0;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&n);
sumd=sumdig(n); // function call
printf(“\n\n Sum of digit=%d”, sumd);
getch();
}
int sumdig(int n) // function defination
{
int dig=0,sum=0;
while(n>0)
{
dig=n%10;
sum=sum+dig;
n=n/10;
}
return sum;
}
Output:
Enter the number: 123
Sum of digit = 6
Function program in C:
/* write a C Program to show reverse number
by using user defined function */
#include<stdio.h>
#include<conio.h>
int reverse(int); // function declaration
void main()
{
int n,r=0;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&n);
r=reverse(n); // function call
printf(“\n\n Reverse number: %d”,r);
getch();
}
int reverse(int n) // function definition
{
int rev=0, dig=0;
while(n>0) // 123 12 1 0
{
dig=n%10; // 3 2 1
rev=(rev*10)+dig; // 3 32 321
n=n/10; // 12 1 0
}
return rev;
}
Output:
Enter the number: 123
Reverse number: 321
/* write a C Program to show Palindrome number
by using user defined function
ex. 121 = 121 , 535 = 535 etc.
*/
#include<stdio.h>
#include<conio.h>
int reverse(int); // function declaration
void main()
{
int n,r=0,t=0;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&n);
t=n;
r=reverse(n); // function call
if(r==t)
printf(“\n\n %d is a Palindrome number.”, r);
else
printf(“\n\n %d is not a Palindrome number.”, t);
getch();
}
int reverse(int n) // function definition
{
int rev=0, dig=0;
while(n>0) // 123 12 1 0
{
dig=n%10; // 3 2 1
rev=(rev*10)+dig; // 3 32 321
n=n/10; // 12 1 0
}
return rev;
}
Output:
Enter the number: 121
121 is a Palindrome number.
Function program in C:
by using user defined function */
#include<stdio.h>
#include<conio.h>
int prime(int); // function declaration
void main()
{
int n;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&n);
prime(n); // function call
getch();
}
int prime(int n) // function defination
{
int d=2;
while(n>d) // 5>2 5>3 5>4 5>5
{
if(n%d==0) // 1 2 1
{
printf(“\n\n %d is not prime number.”,n);
break;
}
else
{
d++; // 3 4 5
}
}
if(n==d)
printf(“\n\n %d is a Prime number.”,n);
return 0;
}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System