-1

I'm trying to make a calculator program for my class (not fully completed as you can tell) and I'm running into a problem with the Multiplication and Division section. They both print out 0 instead of their intended value, but Addition and Subtraction work fine. Any help?

int choice = 8;
double numberone;
double numbertwo;
    
while(choice > 7 || choice < 1){
    printf("(1) Addition \n(2) Subtraction \n(3) Multiplication \n(4) Division \n(5) Modulus (integers only) \n(6) Test if prime (integers only) \n(7) Exit \nPlease choose an operation: \n");
    scanf(" %i", &choice);
    
    if (choice != 5 || choice!= 6){
        printf("Enter the first number: ");
        scanf(" %d", &numberone);
        
        printf("Enter the second number: ");
        scanf(" %d", &numbertwo);
        
        if (choice == 1){
            double sum = numberone + numbertwo;
            printf("Sum: %d", sum);
        }
        else if (choice == 2){
            double dif = numberone - numbertwo;
            printf("Difference: %d", dif);
        }
        else if (choice == 3){
            double pro = numberone * numbertwo;
            printf("Product: %d", pro);
        }
        else if (choice == 4){
            double quo = numberone / numbertwo;
            printf("Quotient: %d", quo);
        }
    }
}
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

1

You use %d in your printf which converts your results to an Integer. Use %lf instead.

// ...
        else if (choice == 3){
            double pro = numberone * numbertwo;
            printf("Product: %lf", pro);
        }
        else if (choice == 4){
            double quo = numberone / numbertwo;
            printf("Quotient: %lf", quo);
// ...

EDIT: The same applies to your scanf. Use %lf instead of %i:

// ...
       printf("Enter the first number: ");
       scanf(" %lf", &numberone);
        
       printf("Enter the second number: ");
       scanf(" %lf", &numbertwo);
// ...
Sebi2020
  • 1,966
  • 1
  • 23
  • 40