Caculator in C Language in Linux
Caculator in C Language in Linux
c
/* Program to calculate the product of two numbers. */ #include <stdio.h> float val1, val2, val3; char c; float float float float product(float x, float y); division(float x, float y); addition(float x, float y); subtraction(float x, float y);
Page 1 of 2
int main(void) { /* Get the first number */ printf("Please enter the first number: "); scanf("%f", &val1); /* Get the second number */ printf("Please enter the second number: "); scanf("%f", &val2); scanf("%c",&c); printf("Enter operator: "); scanf("%c",&c); if (c=='*'){ /* Calculate and display the product */ val3 = product(val1, val2); printf ("%f * %f = %f\n", val1, val2, val3); } else if (c=='/'){ /* Calculate and display the product */ val3 = division(val1, val2); printf ("%f / %f = %f\n", val1, val2, val3); } else if (c=='+'){ /* Calculate and display the product */ val3 = addition(val1, val2); printf ("%f + %f = %f\n", val1, val2, val3); } else if (c=='-'){ /* Calculate and display the product */ val3 = subtraction(val1, val2); printf ("%f - %f = %f\n", val1, val2, val3); } else printf("invalid operator \n"); return 0; } /* Function returns the product of the two values provided */ float product(float x, float y) { return (x * y); } float division(float x, float y) { return (x / y); } float subtraction(float x, float y) { return (x - y);
File: /home/umar/OS/lab.c
} float addition(float x, float y) { return (x + y); }
Page 2 of 2