Learn about C Program for Addition Subtraction Multiplication and Division using Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program for Addition Subtraction Multiplication and Division using Function
Program:
#include<stdio.h>
float input()
{
float n;
scanf("%f",&n);
return n;
}
void display(float n1, float n2, char ch, float result)
{
printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result);
}
void add(int n1, float n2)
{
float result;
result = n1 + n2;
display(n1, n2, '+', result);
}
void subtract(float n1, float n2)
{
float result;
result = n1 - n2;
display(n1, n2, '-', result);
}
void multiply(float n1, float n2)
{
float result;
result = n1 * n2;
display(n1, n2, '*', result);
}
void divide(float n1, float n2)
{
float result;
result = n1 / n2;
display(n1, n2, '/', result);
}
int main()
{
char ch;
int choice;
float n1, n2;
do
{
printf("Enter first number: ");
n1 = input();
printf("Enter second number: ");
n2 = input();
printf("\nWhich operation you want to perform,\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(n1, n2);
break;
case 2:
subtract(n1, n2);
break;
case 3:
multiply(n1, n2);
break;
case 4:
divide(n1, n2);
break;
default:
printf("Invalid choice");
}
printf("\nDo you want to continue (y/n): ");
scanf("%c",&ch);
scanf("%c",&ch);
printf("--------------------------------\n");
} while(ch=='y');
return 0;
}
Output:
Enter first number: 5
Enter second number: 3
Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 1
5.00 + 3.00 = 8.00
Do you want to continue (y/n): y
Enter first number: 4
Enter second number: 2
Which operation you want to perform,
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice: 2
4.00 – 2.00 = 2.00
Do you want to continue (y/n): n
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program for Addition Subtraction Multiplication and Division using the userdefined Function
C Program for Addition Subtraction Multiplication and Division using the Function