-1
    int main ()
    {
    int num, i=num, isPrime;
    printf("Enter an integer: ");
    scanf("%d", &num);

    while (i>=2)
    {
        if (num%i!=0)
            i--;

        if (num%i==0) //check if it is a factor
            {
                isPrime = 1;
                for (int j=2; j<=i; j++)
                {
                    if (i%j==0)
                    {
                    isPrime = 0;
                    break;
                    }
                }
                if (isPrime==1)
                {
                    printf("%d ", i);
                    num = num/i;
                }
            }
    }
    return 0;
}

May I know why is my code not working? I was trying to write a C code which print all prime factors of a given number from the biggest factor to the smallest and when I run it just show nothing after I input a number.

ashers
  • 7
  • 3
  • Try opening the code in your debugger, step through line by line, and examine the values of the variables. – Nicholas Hunter May 18 '21 at 12:23
  • 2
    `"May I know why is my code not working?"` -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 18 '21 at 12:23
  • 1
    ashersm If input is less than 2, what output is expected? None? – chux - Reinstate Monica May 18 '21 at 12:43

2 Answers2

4
  • #include of required header stdio.h to use printf() and scanf() is missing.
  • num is assigned to i before a value is read to num. This is assigning an indeterminate value to i and using the value invokes undefined behavior.
  • Looping j until it becomes i is wrong because i%i will always become zero unless i is zero.
  • You should decrement i also when num%i==0 and i is not prime. Otherwise, the update of i will stop there and the loop may go infinitely.
  • The two if statements if (num%i!=0) and if (num%i==0) may see different values of i. This happens when num%i!=0 at the beginning of the iteration because i is updated when the condition is true. You should use else instead of the second if statement.

Fixed code:

#include <stdio.h>

int main (void)
{
    int num, i, isPrime;
    printf("Please enter an integer: ");
    if (scanf("%d", &num) != 1)
    {
        fputs("read error\n", stderr);
        return 1;
    }

    i=num;
    while (i>=2)
    {
        if (num%i!=0) //check if it is a factor
        {
            i--;
        }
        else
        {
            isPrime = 1;
            for (int j=2; j<i; j++)
            {
                if (i%j==0)
                {
                    isPrime = 0;
                    break;
                }
            }
            if (isPrime==1)
            {
                printf("%d ", i);
                num = num/i;
            }
            else
            {
                i--;
            }
        }
    }
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2

At least one bug here:

int num, i=num, isPrime;

It seems you are trying to make i equal to num. But here `num isn't even initialized yet. You have to do the assignment after reading user input:

int num, i, isPrime;
printf("Please enter an integer: ");
scanf("%d", &num);
i=num;

Also the check for primes is wrong:

            for (int j=2; j<=i; j++)
            {
                if (i%j==0)
                {
                    isPrime = 0;
                    break;
                }
            }

Since you use j<=i then j will eventually be equal to i and i%j==0 will be true so you don't find any primes. Try j<i instead.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63