试图从模数中得到余数但有一些麻烦 - 在 C 中 [英] Trying to get the remainder from modulus but having some troubles - in C

查看:47
本文介绍了试图从模数中得到余数但有一些麻烦 - 在 C 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用创建的模数函数进行更新.然而,当它编译时,输入两个数字后,它什么也不返回.

Updated with a created modulus function. However, while it does compile, after entering two numbers, it returns nothing.

#include<stdio.h>
int main(){
    double num1, num2, num3;
    printf("Enter two double numbers > ");
    scanf("%lf", &num1);
    scanf("%lf", &num2);
    num3 = num2;
    while (num3 > num1) {
        if (num3 > num1) {
        num3 = num3-num1;
        }
        else {
            printf("%.4lf float modulo %.4lf is %.4lf", num1, num2, num3);
        }
    }
    return 0; 
}

推荐答案

如您所知,除法实际上只是减法的一种快捷形式,余数是除法时剩下的数字部分.因此,请考虑从 num1 中反复减去 num2,直到得到一个小于 num2 的数字.那是你的模数.我会把代码留给你.

As you know, division is really just a shortcut form of subtraction, and a remainder is the part of the number that is left over when you divide. So consider repeatedly subtracting num2 from num1 until you have a number that is less than num2. That's your modulus. I'll leave the code up to you.

示例:7 % 3:

7 - 3 = 4
4 - 3 = 1

1 < 3, so 7 % 3 = 1

这篇关于试图从模数中得到余数但有一些麻烦 - 在 C 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆