Home » C Program for Addition Subtraction Multiplication and Division using Function

# C Program for Addition Subtraction Multiplication and Division using Function

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);
}

{
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("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
scanf("%d", &choice);

switch (choice) {
case 1:
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,
2.Subtraction
3.Multiplication
4.Division
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,