Hello can someone help i'm having some problems whit my cocculator if i divide 1/0 or 2/0 etc. It isn't write an error its shows 0. Maybe someone can give advise what and in which place i have to write .
#include <stdio.h>
#include <stdlib.h>
int addition(int a,int b)
{return a+b;};
int substraction(int a, int b)
{return a-b;};
int division(int a, int b)
{return a/b;};
int multiplication(int a, int b)
{return a*b;};
int main() {
int a,b,rez;
char c;
for(;;) {
printf("Welcome to coculator.\n");
printf("Please choose an option by entering the number, press q to quit\n");
printf("1-Addtion\n");
printf("2-Substraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("--------------------------------------------------------------------------\n");
char num;
scanf("%d", &num);
printf(" Enter the first number:\n");
scanf("%d", &a);
printf("Enter the second number:\n");
scanf("%d", &b);
if (num == 1){
rez=addition(a,b);
printf("%d\n",rez);
}
else if (num == 2){
rez=substraction(a,b);
printf("%d\n",rez);
}
else if (num == 3){
rez=division(a,b);
printf("%d\n",rez);
}
else if (num == 4){
rez=multiplication(a,b);
printf("%d\n",rez);
}
else {
printf("You chose the wrong number! Try another one.");
}
scanf("%c", &c);
if (c == 'q')
break;
You simply cannot divide by 0 - so there is no point in carrying out the
divison function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
elseif (num == 3)
{
if (b ==0)
{
printf ("Not possible to divide by zero");
}
else //only carry out the division if denominator is not zero
{
rez=division(a,b);
printf("%d\n",rez);
}
}