简体   繁体   English

似乎找不到重新输入数字后为什么我的Prime号码计划无法正常运行的原因

[英]Can't seem to find why my Prime Number Program doesn't work properly after reprompt for number

Im having trouble with my prime number section part of my program. 我在程序的素数部分遇到了麻烦。 When I compile and run my program, the prime numbers print out fine for the first number, but when reprompted for another number. 当我编译并运行程序时,素数对第一个数字的输出很好,但是在提示您输入另一个数时却很好。 It usually prints out either no numbers or it cuts off the prime numbers from a certain point down. 它通常不打印任何数字,或者从某个点向下切掉质数。 Im new to coding and this forum so I'm sorry about any formatting issues in my post. 我是本次论坛的编码新手,对于帖子中的任何格式问题,我深表歉意。

#include <stdio.h>
#include <math.h>
int main(void)
{
    int number, n=1;
    long factorial=1;
    int a=1, b=0,c;
    int q=2, r=2, w=0;
    int prime, count;

    printf("Enter a Number:\n");
    scanf("\n%d", &number);
    while(number!=1000)
    {
        if(number<1||number>1000)
            printf("Input is Invalid\n");
        else
       {
            if(number==1000)
                printf("Goodbye\n");
            if(number<15)
            {
            factorial=number;
            n=number-1;
            while(n>=1)
            {
            factorial=factorial*n;
            n--;

            }
            printf("The Factorial of %d is: %ld\n", number,  factorial);
        }   
        c=a+b;
        printf("Fibonacci Sequence up to %d\n ", number);
        while(c<number&&a+b<number)
            {
                 c=a+b;
                 a=b;
                 b=c;
                 n++;
                 printf("%d\t", c);

                 count=n;
                 if(count%10==0)
                    printf("\n");

            }
            printf("\nTotal:%d\n", n);
            printf("\nPrime numbers up to %d:\n", number);

            while(q<=number)
            {
                prime=0;

                for(r=2;r<q;++r)
                {
                if(q%r==0)
                {
                prime=1;
                }
                }
                if(prime==0)
                {
                printf("%d\t", r);
                w++;
                }
                q++;
                }
                count=r;
                if(count%10==0)
                    printf("\n");
                    printf("\nTotal:%d\n", w);
                }
                printf("\nEnter a Number:\n");
                scanf("\n%d", &number);
                        a=1;
                b=0;                
            }
return(0);
}

You are never resetting your variable q, which is the control variable for your prime number loop. 您永远不会重置变量q,它是质数循环的控制变量。 It is best practice to create all of your variables which need to be reset inside your loop. 最佳做法是创建所有需要在循环内重置的变量。 Either reset q at the beginning/end of your loop, or create q at the beginning of your outer loop and set it to 2. 在循环的开始/结束处重置q,或在外部循环的开始处创建q并将其设置为2。

Like this: 像这样:

while(number != 1000) {
  int q = 2;
  //other assignments below which need to be reset
  ...
  //all other code below
  ... 
}

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

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