A
amanayin
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
return 0;
}
char call(float math)
{
while(d != 'q'){
printf("Enter operand or q to exit: ");
scanf("%s",&d);
if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d != 'q')
{ printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}
float add(float a, float b)
{
c = a + b;
return c;
}
float sub(float a,float b)
{
c = a - b;
return c;
}
float multi(float a, float b)
{
c = a * b;
return c;
}
float div(float a, float b)
{
c = a / b;
return c;
}
float per(float a, float b)
{
c = a * b / 100;
return c;
}
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
return 0;
}
char call(float math)
{
while(d != 'q'){
printf("Enter operand or q to exit: ");
scanf("%s",&d);
if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d != 'q')
{ printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}
float add(float a, float b)
{
c = a + b;
return c;
}
float sub(float a,float b)
{
c = a - b;
return c;
}
float multi(float a, float b)
{
c = a * b;
return c;
}
float div(float a, float b)
{
c = a / b;
return c;
}
float per(float a, float b)
{
c = a * b / 100;
return c;
}