-2
#include <stdio.h>

int main() {
    int x,y,add,subtraction,multiplication,division;
   printf("Write x:");
    scanf("%d",&x);
   printf("Write y:");
    scanf("%d",&y);
    add = x+y;
    printf("Add=%d\n",add);
    subtraction = x-y;
    printf("subtraction = %d\n",subtraction);
    multiplication = x*y;
    printf("multiplication = %d\n",multiplication);
    division = x/y;
    if  (y == 0)
        printf("Operation not permitted");
    else
        printf("division = %d\n",division);
    return 0;
}

So When i'm printing y=0 nothing happens No "Operation not permitted" or answer of division What should i do?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Vladyslav
  • 35
  • 5
  • Show complete code. Is `y` an `int`, or is it a `float`? – William Pursell Nov 11 '21 at 17:39
  • You have no input of `y`. – Hrushikesh Dharmadhikari Nov 11 '21 at 17:39
  • 5
    If `y` is really `0` you check too late. You have already done division by zero, then, and chances are good that the program was terminated abnormally. – Gerhardh Nov 11 '21 at 17:40
  • Put the division line ` iloraz = x/y;` in the `else` block. The division is only valid if divider is not zero – Arnaud Valmary Nov 11 '21 at 17:44
  • 1
    the if statement should be placed before you actually do the division itself you're preventing an error that has already happened – Imeguras Nov 11 '21 at 17:45
  • After `division = x/y;` it is too late to check for the value of `y`. If `y` is `0` then the harm has already been done. The `x/y` operation produces a division by zero error, the program exits and the `if (y == 0)` statement does not have any chance to execute. – axiac Nov 11 '21 at 17:56

2 Answers2

1

Make the division happen in the else. Like this:

#include <stdio.h>

int main() 
{
    int x,y,sum,roz,iloczyn,iloraz;
   printf("Wprowadz x:");
    scanf("%d",&x);
   printf("Wprowadz y:");
    scanf("%d",&y);
    sum = x+y;
    printf("Suma=%d\n",sum);
    roz = x-y;
    printf("roznica = %d\n",roz);
    iloczyn = x*y;
    printf("iloczyn = %d\n",iloczyn);
    
    if  (y == 0)
    {
        printf("Operation not permitted");
        exit(0);
    }
    else
    {
        iloraz = x/y;
        printf("iloraz = %d\n",iloraz);
        return 0;
    }
}
1

Just place this statement

division = x/y;

inside the if statement like

if  (y == 0)
{
    printf("Operation not permitted");
}
else
{
    division = x/y;
    printf("division = %d\n",division);
}

return 0;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335