Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Fisher编号的结果检查不正确 节目详情:费希尔号码_C_Function_Loops - Fatal编程技术网

C Fisher编号的结果检查不正确 节目详情:费希尔号码

C Fisher编号的结果检查不正确 节目详情:费希尔号码,c,function,loops,C,Function,Loops,Fisher数是一个整数,其因子(包括自身)的乘积等于该数的立方体。例如,12是一个Fisher数,因为123=2x3x4x6x12 示例: Input: 12 Output: true (12<sup>3</sup> = 2 x 3 x 4 x 6 x 12) Input: 8 Output: false (8<sup>3</sup> != 2 x 4 x 8) #include <stdio.h> #include &l

Fisher数是一个整数,其因子(包括自身)的乘积等于该数的立方体。例如,
12
是一个Fisher数,因为123=2x3x4x6x12

示例:

Input: 12  
Output: true (12<sup>3</sup> = 2 x 3 x 4 x 6 x 12)

Input: 8  
Output: false (8<sup>3</sup> != 2 x 4 x 8)
#include <stdio.h>
#include <conio.h>
int ch, product = 1, cube, num, i, j, min, max;

// Check Function

void check() {
    int i;
    printf("Enter the Number to check whether It is Fisher or Not\n");
    scanf("%d", &num);
    cube = num * num * num;
    for(i = 2; i <= num; i++) {
        if(num % i == 0) {
            product = product * i;
        }
    }
    if(cube == product) {
        printf("It is a Fisher Number!\n");
    }
    else {
        printf("It is not a Fisher Number\n");
    }
}

// RANGE FUNCTION

void range() {
    printf("Enter the Minimum and Maximum value of Range\n");
    scanf("%d%d", &min, &max);
    for(i = min; i <= max; i++) {
        cube = i * i * i;
        for(j = 1; j <= i; j++) {
            if(i % j == 0) {
                product = product * i;
            }
        }
        if(cube == product) {
            printf("%d\n", i);
        }
    }
}

void main() {
    clrscr();
    printf("Enter Your Choice \n");
    printf("1 - Check Fisher Number \n");
    printf("2 - Display Fisher Numbers in a Range \n");
    scanf("%d", &ch);
    switch(ch) {
        case 1: check();
                break;
        case 2: range();
                break;
        default: printf("Enter Valid Choice \n");
    }
    getch();
}
Enter Your Choice 
1 - Check Fisher Number 
2 - Display Fisher Numbers in a Range 
2
Enter the Minimum and Maximum value of Range
1 40
1

我没有得到正确的范围输出。例如,如果范围设置为1到15,它只显示
1
,这是错误的

你应该在这里乘以
j

if(i%j == 0) {
    product = product * i;
}
如果使用比
i
j
更有意义的变量名,这一点会更加明显

您还需要在内部循环之前重置
product
变量(为什么它是一个全局变量?您不知道它们是邪恶的吗?)

cube=i*i*i;
产品=1;

对于(j=1;jWhat error?请确保您包含了所有相关信息。我没有获得正确的范围输出。例如,如果范围设置为1到15,则只显示错误的1!请将您的“例如”添加到问题中,并使用适当的格式。此外,“哪一个是错误的”还不够-也显示预期结果。您应该阅读。另外,作为未来的一般指导原则:当您有两个函数都需要执行相同的非平凡任务时(例如,检查整数是否为Fisher数),编写第三个函数,名为
IsFisherNumber
,它们都可以调用,而不是实现两次算法。您的代码将更易于理解和维护。
cube = i * i * i;
product = 1;
for(j=1;j<=i;j++) { ...