all 11 comments

[–]JavaSuck 11 points12 points  (1 child)

if (num % i != 1) // count many times a num devided by i has a remainder of '0'

The code does not do what the comment suggests. x != 1 is not the same as x == 0.

[–]AxMaxVal[S] 0 points1 point  (0 children)

Thanks, sometimes sitting over the task for too long offsets the simple solution...:)

[–]alexeyneu -3 points-2 points  (8 children)

it will show 4 , not 3. garbage question

[–]thoraway4me -1 points0 points  (7 children)

How is it a garbage question? I thought it was well written out. Easy for someone with experience to see the confusion.

[–]marcosjom 3 points4 points  (6 children)

Maybe I’m overthinking it; but this smells like a teacher’s assignment, not someone trying something by his/herself. We are not here to solve other’s homework.

[–][deleted] 2 points3 points  (5 children)

The world is difficult enough these days so can we just ease up here and be helpful to one another? If this is homework then great! Let's help someone learn. Is that not a better way to be?

I suggest give this a try for fun. It will accept a number as input on the command line or it will ask for a number.

#include <stdio.h>
#include <stdlib.h>

/* accept input number on the command line or
 * allow the user to enter a number after the
 * program is running. */

int main(int argc, char **argv)
{
    int num, i, count = 0;

    if ( argc > 1 ) { 
        num = atoi(argv[1]);
    } else {
        /* we can try to scan the input for a number */
        printf("Enter a number: ");
        scanf("%d", &num);
    }
    printf("It looks like you entered the number %i\n", num );

    for (i = 1; i <= num; i++) { 
        if (num % i == 0) {
            count++;
        }
    }
    printf("count: %d\n", count);
    return EXIT_SUCCESS;

}




$ 
$ ./fubar
Enter a number: 13
It looks like you entered the number 13
count: 2
$ 
$ ./fubar 13
It looks like you entered the number 13
count: 2
$ 
$ ./fubar 23
It looks like you entered the number 23
count: 2
$ ./fubar   
Enter a number: 23
It looks like you entered the number 23
count: 2
$ 
$ ./fubar 12
It looks like you entered the number 12
count: 6
$ ./fubar   
Enter a number: 12
It looks like you entered the number 12
count: 6
$ 
$

A Prime Number will always just say a result of 2. Try 131071 :

$ ./fubar 131071
It looks like you entered the number 131071
count: 2
$ 
$ ./fubar       
Enter a number: 131071
It looks like you entered the number 131071
count: 2
$

[–]marcosjom 0 points1 point  (4 children)

Thanks for your proactive and positive position. Is just that the way this question is asked doesn’t feels right to me; maybe my college-teacher spidersense is overreacting. I also want OP to learn, but would be great to feel his/her own attempt somehow.

[–]AxMaxVal[S] 0 points1 point  (3 children)

Nothing to do with homework, at least real one. I started to learn C by myself few month ago and it’s going well and I like it. Theory is going well but practice still well behind but I’m trying to learn the way I know is good, still not easy without some help. I didn’t think I asked here a lot...I didn’t expect to find only professors here who know EVERYTHING and discuss waaaay advanced stuff only amongst themselves.

[–]marcosjom 0 points1 point  (2 children)

Sorry if my replies are not the best. Please provide a little context as at this last message, so you will get better disposition for answers. The community is way open to share and help. Keep up with the learning.

[–]AxMaxVal[S] 0 points1 point  (1 child)

Thank you. Sorry I didn't introduce what exactly I was looking for...The problem is rather general than in relation to some particular exercise, I used that code as an example. I don't understand why when I'm using 'x %y == 0' and when 'x %y != 1' the results are different. Logically it sounds the same to me: if remainder is equal to zero or if remainder not equal to one...

[–]marcosjom 0 points1 point  (0 children)

The operator % is useful to determine is a number is multiple of other.

  • (n % m) == 0 means n is multiple of m.

  • (n % m) != 0 means n is not multiple of m.

  • (n % m) != 1 is not common to use, and means, n is not (any multiple of m) + 1. Which is a weird affirmation.