-2

This code does not generates an output.. please tell what is missing.. I'm new to programming.

int main() 
{
    int num;
    scanf("%d", &num);
    for (int i = 1; i++; i <= num)
    {
        int f = 0;
        for (int t = 1; i++; i<i)
        {
            if (i%t == 0)
                f++;
        }
        if (f == 2)
            printf("%d ", &i);
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 7
    For one, your second and third expressions of your for-loops are reversed. The condition should be second; the increment step third. The second loop makes no sense whatsoever, btw, even if you fix that problem. – WhozCraig Oct 03 '19 at 06:40
  • The remainder when you divide any integer by one is zero. – Jonathan Leffler Oct 03 '19 at 06:48
  • First loop I used to pick a number from 1 to num, second to find which of them was prime. @WhozCraig Can you please tell why it's not printing anything at all – pragya kulshrestha Oct 03 '19 at 07:00
  • First step: enable compiler warnings & errors. Second step: fix anything flagged by the compiler. Third step: fix the logic of the inner loop. After following those steps I get for the input of `10` the output of `4 9` (not sure what this is supposed to mean, though) – Stefan Becker Oct 03 '19 at 07:06
  • It looks as if your method is to use the definition that a prime number is only divisible by 1 and itself. Apart from the coding errors, that is probably the least efficient method there is. Better would be to keep an array of primes found so far, and if any of them divides into the number, it's not prime so you can break the loop. There is no need to test any other divisors, or go beyond the square root of the number. – Weather Vane Oct 03 '19 at 08:08
  • 1
    Watch your program while it runs, using a debugger. https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://stackoverflow.com/questions/8041614/how-to-debug-in-codeblocks/58050981#58050981 – Yunnosch Oct 03 '19 at 08:22
  • You need to reduce this to a [mcve] before posting here. As a new user, also take the [tour] and read [ask]. – Ulrich Eckhardt Oct 03 '19 at 11:41
  • Ok sure @ulrich , thanks everyone.. it helped. – pragya kulshrestha Oct 03 '19 at 14:01

1 Answers1

0

you for (int i = 1; i++; i <= num) is wrong should be for(int i = 1; i <= num; i++)(intialize var , condition, increment). Another problem found at printf("%d", &f) doing this you are printing address of memory.

You should be more carefull, writing you code and be a beginner dont rush it.

J.Fernandes
  • 87
  • 2
  • 10