-1

I'd like to ask for help about this. I'm going to input two integers. All of the results are showing except for "modulus". Also the second and third "%d" is remaining as is. Here is the results when I run my program.

Enter First Number: 10
Enter Second Number: 20

10 + 20 = 30  
10 - 20 = -10  
10 * 20 = 200  
10 / 20 = 0  
10 % %d = %d

What could be the probable fix if this is my program:

#include <stdio.h>
#include <conio.h>
main()
{

  int num1, num2, sum=0, diff=0, prod=0, quot=0, mod=0;

  clrscr();

  printf("\nEnter First Number:");
  scanf("%d",&num1);
  printf("\nEnter Second Number:");
  scanf("%d",&num2);
  sum = num1 + num2;
  diff = num1 - num2;
  prod = num1 * num2;
  quot = num1 / num2;
  mod = num1 % num2;
  printf("%d + %d = %d", num1, num2, sum);
  printf("%d - %d = %d", num1, num2, diff);
  printf("%d * %d = %d", num1, num2, prod);
  printf("%d / %d = %d", num1, num2, quot);
  printf("%d % %d = %d", num1, num2, mod);
  getch();

}
3

2 Answers 2

2

The problem is here:

printf("%d % %d = %d", num1, num2, mod);
           ~~~~

When you use a % in the format string, printf will treat it as part of a conversion specification (like %d). If it's followed by something other than a valid specification, the results are undefined, so anything might happen.

If you need a literal % in the output, use %% in the format string:

printf("%d %% %d = %d", num1, num2, mod);
1

The % character is used to denote a format string in printf.

Use %% to print the % character. Now, printf is expecting a format string, but there's only a space.

printf("%d %% %d = %d", num1, num2, mod);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.