繁体   English   中英

如何使用C语言在printf输出中使用除法运算符来显示余数

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

/ *在页面的3/4处,我有直接在此段下面列出的代码。 我需要它来打印其余部分,但似乎无法正确处理。 我知道使用Modulus运算符是此功能的关键,但是我对如何正确使用它一无所知。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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