有人会特别向我解释这段代码吗?

时间:2018-12-22 14:56:05

标签: c loops

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
    int i,flag = 1,n,newn;
    clrscr();
    printf("Enter number...");
    scanf("%d",&n);
    for(newn = 2;newn <= n;newn++)
    {
        flag=1;
        for(i=2;i<=newn/2 ;i++) //what would be the initial value of variable "newn" will be.

        {
            if(newn % i == 0)
            {
                flag = 0;
                break;
            }
        }
        if(flag == 1)
            printf("%d ",newn);
    }
    getch();
}

第一次进行迭代时,内部for循环的初始值是什么?为什么在此程序中需要外部for循环?

1 个答案:

答案 0 :(得分:0)

此代码打印2到您输入的数字之间的所有素数。我重新编写了代码,以使其更加清楚

int main()
{
    int flag=1, max_number = 100;
    // Flag determines if we have a prime number of not
    // flag = 1, prime number found
    // flag = 0, no prime number found
    printf("Please Enter a number bigger then 2 and press enter : \n");
    scanf("%d",&max_number);

    printf("List of prime numbers from 2 to %d : \n", max_number);

    // Counts from 2 till the halb of the number you entered, since 0 and 1 
    // are not interesting for us
    for(int current_number = 2; current_number <= max_number; current_number++)
    {

        flag=1;
        // Counts from 2 till the half of the current_number and check if its a prime 
        // number 

        for(int i = 2; i <= current_number/2 ; i++)
        {
            //printf("Outer loop : %d Inner Loop %d\n", current_number, i);

            // Checks if we have a prime number
            if(current_number % i ==0)
            {
              // No we have not a prime number, quit this one and check next number
              flag=0;
              break;
            }
        }

        // Prime number found
        if(flag==1)
            printf("Prime number found: %d \n", current_number);
    }

    return 0;
}

max_number = 15的输出为:

 Prime number found: 11                                                                                                                                                             
    Outer loop : 12 Inner Loop 2                                                                                                                                                       
    Outer loop : 13 Inner Loop 2                                                                                                                                                       
    Outer loop : 13 Inner Loop 3                                                                                                                                                       
    Outer loop : 13 Inner Loop 4                                                                                                                                                       
    Outer loop : 13 Inner Loop 5                                                                                                                                                       
    Outer loop : 13 Inner Loop 6                                                                                                                                                       
    Prime number found: 13                                                                                                                                                             
    Outer loop : 14 Inner Loop 2                                                                                                                                                       
    Outer loop : 15 Inner Loop 2                                                                                                                                                       
    Outer loop : 15 Inner Loop 3