如果结果是浮点数,如何停止划分?

发布于 2025-01-31 21:23:54 字数 625 浏览 1 评论 0原文

我已经对此代码进行了编程,但是它部分向我展示了结果50/3向我展示16,而我想介绍以停止这种操作(如果浮动)。该怎么办?

#include <stdio.h>
int main(){
    int number, divisor, x, y;
    printf("Enter the Natural Number to find Divisors\n");
    scanf("%d", &number);
    printf("Enter the Natural Number to divide for\n");
    scanf("%d", &x);
        
    printf("Divisors of the number %d are \n", number);
    y=number/x;
    for (divisor = 1; divisor<=number;divisor++){
    if((number%divisor)==0){
    printf("%d\n", divisor);
    
    }
    else{
    continue; }
    
}
printf("The result of the division is %d", y);
return 0;
}

i have programmed this code but it show me partially the result as example 50/3 show me 16 while i want to introduce to stop this kind of operations if float. How to do?

#include <stdio.h>
int main(){
    int number, divisor, x, y;
    printf("Enter the Natural Number to find Divisors\n");
    scanf("%d", &number);
    printf("Enter the Natural Number to divide for\n");
    scanf("%d", &x);
        
    printf("Divisors of the number %d are \n", number);
    y=number/x;
    for (divisor = 1; divisor<=number;divisor++){
    if((number%divisor)==0){
    printf("%d\n", divisor);
    
    }
    else{
    continue; }
    
}
printf("The result of the division is %d", y);
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这就是我必须阅读您的代码多远:

for (i=0;i<x;i++)
{
    j=x/i;

您认为由0的部门做什么?

但是让我们继续前进:

if("J=%d")

如果字符串常数为null,则该测试是永远不会的。

总体而言,您似乎正在尝试测试X是素数。在这种情况下,您需要在2上启动循环并测试其余部分:

if (x % i == 0)

This is how far I have to read your code:

for (i=0;i<x;i++)
{
    j=x/i;

What do you think a division by 0 should do?

But lets keep going:

if("J=%d")

This tests if the string constant is NULL, which can never be.

Overall it looks like you are trying to test if x is a prime. In that case you need to start the loop at 2 and test the remainder:

if (x % i == 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文