My Subject Blog

C Program for Calculator using Function

/*
Write a calculator program(add,subtract,multiply,divide). 
Prepare user defined function for each functionality.  
*/

#include <stdio.h>
int add(int, int);
int subtract(int, int);
int multiply(int, int);
int divide(int, int);
int main(void) {
int a, b, ch;
do
{
printf("\n Calculator Program ");
printf("\n=======================");
printf("\n 1 - Add ");
printf("\n 2 - Subtract ");
printf("\n 3 - Multiply ");
printf("\n 4 - Divide ");
printf("\n 5 - Exit ");
printf("\n=======================");
printf("Enter Your Choice :");
scanf("%d",&ch);
printf("\n Enter First Value :");
scanf("%d",&a);
printf("\n Enter Second Value :");
scanf("%d",&b);
switch(ch)
{
case 1:
printf("Add = %d",add(a,b));
break;
case 2:
printf("Subtract = %d",subtract(a,b));
break;
case 3:
printf("Multiply = %d",multiply(a,b));
break;
case 4:
printf("Divide = %d",divide(a,b));
break;
case 5:
printf("Thanks for using our program");
break;
}
}while(ch!=5);
return 0;
}

int add(int p, int q)
{
return (p+q);
}
int subtract(int p, int q)
{
return (p-q);
}
int multiply(int p, int q)
{
return (p*q);
}
int divide(int p, int q)
{
return (p/q);
}

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

Followers