简体   繁体   中英

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. 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.

Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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