简体   繁体   中英

How do I show the remainder using the modulus operator from division in printf output in the language of C

/* 3/4 of the way down the page I have the code that is listed directly below this paragraph. I need it to print out the remainder, but can not seem to get it right. I know using the Modulus operator is key to this function, but I am lost on how to correctly use it.

result = num1 / num2;                   /* Division */
printf("When 63 is divided by 6 you get %i\n\n", result);

*/

#include <stdio.h>

int main(void)
{

    int     num1    = 63;
    int     num2    = 6;
    int     result;

    float   nickels = 0.05;
    float   pennies = 0.01;
    float   nickPen;                        /* I really wasn't sure rather to add nickPen as a   float or to even add it at all */


    printf("This program will do a few computations with two numbers.\n\n");

    printf("The two numbers used by this program are 63 and 6.\n\n");

    result = num1 + num2;                   /* Addition */
    printf("The sum of 63 + 6 is %i\n", result);

    result = num1 - num2;                   /* Subtraction */
    printf("The difference of 63 - 6 is %i\n", result);

    result = num1 * num2;                   /* Multiplication */
    printf("The product of 63 * 6 is %i\n", result);

    result = num1 / num2;                   /* Division */
    printf("When 63 is divided by 6 you get %i\n\n", result);

    nickPen = nickels*100 + pennies*25;     /* Multiplication and addition of money */
    printf("If you have 100 nickels + 25 pennies you will have $%.2f\n\n", nickPen);

    printf("Thank you for using this program");



        getchar();
        return 0;

} /* End Main*/
printf("When 63 is divided by 6 the remainder is %i\n\n", 63 % 6);

C中的模为x % y ,其中模定义为x - ((int)x/y)*y

result = num1 % num2;    /* Modulus */
printf("When 63 is divided by 6 you get a remainder of %i\n\n", result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM