-5

When I tried to do division of 6/3 the output comes like this 2 / -1431650288. What's wrong in code? My program in c is like this:

#include <stdio.h>

int main(){

    char Operator;
    int num1, num2;

    printf("Enter the operator in which you want to perform calculation(+, -, *, /)\n");
    scanf(" %c", &Operator);

    if (Operator == '/'){
        printf("Enter two numbers: ");
    scanf(" %d %d", &num1, &num2);
     if (num2==0){
            printf("\a Denominator must be greater than 0.\n");
        }
        else{
            printf(" %d / %d", num1/num2);
        }
    } 
    else{
    printf("Enter two integer numbers: ");
    scanf(" %d %d", &num1, &num2);

    if(Operator =='+'){
        printf(" %d + %d = %d", num1, num2, num1+num2);
    }
    else if(Operator == '-'){
      printf(" %d - %d = %d", num1, num2, num1-num2);
    }
    else if(Operator == '*'){
      printf(" %d * %d = %d", num1, num2, num1*num2);
    }

    else{
       printf("\t \a Invalid Operator.\n");
    }

    }
}
hellow
  • 12,430
  • 7
  • 56
  • 79
Vikesh Patil
  • 9
  • 2
  • 2
  • 5

1 Answers1

2

This line:

printf(" %d / %d", num1/num2);

The first '%d' is the result of num1/num2 and that's enough. The second %d and the '/' character should not be here. Change it to:

printf(" %d ", num1/num2);

Additionaly, for your purpose, the switch case structure is more suitable for code readability (and better optimization too I think)

namcao00
  • 272
  • 1
  • 6
  • 10