美文网首页C语言
day10:求助!!!

day10:求助!!!

作者: 今生何求惟你 | 来源:发表于2018-06-20 14:06 被阅读3次

    题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

    可以正确输出的代码:

    #include <stdio.h>

    main()

    {

    int n1,n2,temp,pro,d;

    printf("Please enter two number:");

    scanf("%d%d",&n1,&n2);

    pro = n1 * n2;

    if(n1 <= n2)

    {

    temp = n1;

    n1 = n2;

    n2 = temp;

    }

    while(n2 != 0)

    {

    d = n1 % n2;

    n1 = n2;

    n2 = d;

    }

    printf("The divisor is %d,and the muitiple is %d.\n",n1,pro / n1);

    return 0;

    出错的代码:

    #include <stdio.h>

    main()

    {

    int n1,n2,temp,pro,d;

    printf("Please enter two number:");

    scanf("%d%d",&n1,&n2);

    pro = n1 * n2;

    if(n1 <= n2)

    {

    temp = n1;

    n1 = n2;

    n2 = temp;

    }

    while(d != 0)

    {

    d = n1 % n2;

    n1 = n2;

    n2 = d;

    }

    printf("The divisor is %d,and the muitiple is %d.\n",n1,pro / n1);

    return 0;

    思考:为啥第二种代码会出错呢?

    相关文章

      网友评论

      本文标题:day10:求助!!!

      本文链接:https://www.haomeiwen.com/subject/xfqxyftx.html