简体   繁体   中英

I need to do a program for school “given a number establish the sum of its prime divisors” but I don't understand why my program doesn't work

I don't understand why it doesn't work. When I run the program it tells me that the sum is equal to 0.

int main()  {
    system("CLS"); system("COLOR 0a");
    int i,num,j,cont=0,somma=0;
    printf("enter a number");       //the user enter a number
    scanf("%d",&num);
    for(i=2;i<=num;i++)      //the for and the if detects all divisors of the number entered except 1
        if(num%i==0){ 
            for(j=2;j<i;i++) //this for and if detect the divisors of the divisor of the entered number
                if(i%j==0)
                    cont++; 
            {
                if(cont==0) //if cont == 0 the divisor of the number is prime and is added to the sum
                    somma=somma+i;
            }
            printf("the sum of the prime divisor of %d is %d",num,somma);
        }

}

Curly brackets after for and if are only required when you use more than one statement, but as a beginner you should learn to always use them unless you have a single line statement like if (i == 0) j=1; . And even if it is not required care for indentation. It will make the structure of the program more evident at first sight. And it matters...

Then you should learn to be very cautious with short variable names: for(j=2;j<i;i++) increments i when it should increment j .

Finaly you should always wonder at what time a variable should be initialized. You use cont inside a double loop, and initialize it to 0 only at the beginning of the program. It should be reset to 0 on every iteration of the outer loop.

And last but not least: learn to use a debugger , or use trace print statements throughout your code. If you had, the problems would have been self evident - in fact I did it...

Fix all that and the program should give the expected results. Or ask a new question if it is still broken. But when it will work, I strongly advice you to post it in code review. You will get good advices there both on the C language part and on the algorithmic one.


Here is a minimaly fixed version of your code with the (commented out) traces that helped to identify the problems:

int main()  {
    int i,num,j,cont,somma=0;
    printf("enter a number");
    scanf("%d",&num);
    //printf("Processing %d\n", num);
    for(i=2;i<=num;i++) {
        //printf("i:%d", i);
        if(num%i==0){
            cont = 0;          // must be reset to 0 for every i
            for(j=2;j<i;j++) {
                //printf("j:%d\n", j);
                if(i%j==0) cont++;
            }
            if(cont==0) somma=somma+i;
        }
    }
    printf("the sum of the prime divisor of %d is %d",num,somma);
    return 0;
}

Please do not blindly use it but try to understand the differences and do not forget the Code Review step...

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