# Problem with my output

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-04-2008
Dogmasur
Problem with my output
Okay. So I have started a new program that is meant to query the user to input a number greater than 2. The program then processes that numbers divisors and prints them out on the screen. Eventually, I want the program to also print the sum of those integers to show if the
number is a perfect number or not.

The problem I am having right now is with my integers. My program successfully ( and correctly ) prints out my divisors as long as the inputted integer is 30 or less. Anything higher prints 1 and 2. What am I not seeing or what did I do wrong? I'm not worried about the second half of my program yet ( as you can see, I am not there yet in my code). I want to make sure the functionality works before playing with the rest of my input.

Thanks.

Code:

``` /*write a program which accepts as input an integer (which must be 2 or greater)*/ /*and prints out its proper divisors in ascending order, and to print the sum of its */ /*proper divisors.  The program should also report whether the input number is perfect or not.*/   #include <stdio.h>   int main ( void ) {     int n, d = 1;         printf("Enter a number greater than two:\t");     scanf("&#37;d", &n);     getchar ();         printf("The proper divisor(s) of %d is:\n", n);         while ( d < n ){           if ( n % d )               d++;           else               printf("%d\t", d);               d++;     }         getchar();     return 0;                   }```
• 08-04-2008
Richie T
You're missing some braces so the second d++ is always executed.

Code:

```while ( d < n ) {     if ( n % d )         d++;     else     {         printf("%d\t", d);         d++;     } }```
It is for this reason that I always use braces with things like if statements and loops, even if there is only one line to be executed.
• 08-04-2008
noops
Or you could write:

Code:

```          if ( n % d == 0 )       printf("%d\t", d); d++;```
• 08-04-2008
Dogmasur
Yep, that did it. Thanks. There is a good lesson in paying close attention to syntax.

So, if I wanted to take all the divisors and add them together, would I be best to create a function to store them into an array and then to return the sum of that array when it is complete or could I code something like that within main and not bother with a function? Either way, is there a way I can store them as they are discovered, so the program does not have to go through and get them later on?
• 08-04-2008
Dogmasur
Quote:

Originally Posted by noops
Or you could write:

Code:

```          if ( n % d == 0 )       printf("%d\t", d); d++;```

So the the if statement checks the truth of n%d being 0 and ignores anything else and then goes back to the loop for the next number? I haven't used '=='.
• 08-04-2008
noops
Quote:

Originally Posted by Dogmasur
So the the if statement checks the truth of n&#37;d being 0 and ignores anything else and then goes back to the loop for the next number? I haven't used '=='.

Yup. And for the sum you could use another variable to keep a running total. Whenever you find that n % d is 0 just add d to that running total variable. You will need brackets with the if statement if you add this.

edit: Also, '==' means 'equal to', and '=' means you are assigning a variable. be careful not to mix them up.
• 08-04-2008
Dogmasur
Code:

``` while ( d < n ) {     if ( n % d  == 0 )         printf("%d\t", d)         d++; }```
Where should the enclosing braces go for adding my variable to keep track of the sum? I am using s as the variable, initialized it to 0, and my expression will read s = d + s.
• 08-04-2008
noops
You use the braces for 'if' just like you do for 'while'. So if(){stuff} will execute 'stuff' when the if is true.
Code:

```if(condition) {     stuff;     morestuff; }```
edit: also, you could look into using a 'for' loop for this.
• 08-04-2008
Dogmasur
I had my 'd++' inside the if braces and it was affecting my output. Now it's fixed. Thanks.
• 08-04-2008
Dogmasur
Here is the final code for my program, which seems to work fine.

Code:

```/*write a program which accepts as input an integer (which must be 2 or greater)  and prints out its proper divisors in ascending order, and the sum of its pro-  per divisors.  The program should also report whether the input number is per-  fect or not.*/   #include <stdio.h>   int main ( void ) {     int n, d = 1, s = 0;         printf("Enter a number greater than two:\t");     scanf("%d", &n);     getchar ();         printf("The proper divisor(s) of %d is:\n", n);         while ( d < n ){           if ( n % d == 0 ){                 s = d + s;                 printf("%d\t", d);                 }                    d++;     }         printf("\nThe sum of the divisors is %d.\n", s);         if ( n == s )         printf("%d is a perfect number.\n", n);     else         printf("%d is not a perfect number.\n", n);         getchar();     return 0;                   }```
• 08-05-2008
QuantumPete
You need to fix your indentation on that if statement inside the while loop. Also the other if/else statement is still missing brackets. Your code was a prime example of why you should always put brackets with every if/else/while/for/do statements.
Code:

```while ( d < n ){     if ( n &#37; d == 0 ){         s += d;         printf("%d\t", d);     }        d++; }```
That's what your indentation should look like. Indentation is there to give you an idea of the logical grouping of statements and of scope.

QuantumPete
Edit: Also 'n' is not initialised!
• 08-05-2008
Dogmasur
I see what you mean about the proper indentation. I didn't think I needed to initialize variable 'n' because it user-defined and is not used for any operations until the user has defined its value.
• 08-06-2008
QuantumPete
Quote:

Originally Posted by Dogmasur
I see what you mean about the proper indentation. I didn't think I needed to initialize variable 'n' because it user-defined and is not used for any operations until the user has defined its value.

At the moment, yes. But imagine some time in the future, when your code is 10,000 lines long. You need to add a statement that uses n. You'd have to trace through all the code to find out when/where n is initialised (and no, "I'll know when it is initialised" is not a valid argument. You'd be surprised how much you forget about code in a matter of weeks).
Better to init the variable at creation. Then you can be assured that it has a valid value anytime that it is in scope.
The thing with variable initialisation, brackets and braces, putting constants on the lhs for comparisons etc, is that while you may not see any benefit in it now, when you come back to alter or simply read the program later, there'll be fewer surprises and thereby fewer bugs.
Just because you can get away with something in C doesn't mean it's good practice or even safe.

QuantumPete
• 08-06-2008
Dogmasur
Good points. I understand what you're saying. Thanks.
• 08-06-2008
ssharish2005
Code:

```printf("Enter a number greater than two:\t");     scanf("&#37;d", &n);     getchar ();```
Perhaps, you should write a proper function to clear off the input buffer, but not just using the getchar function to clear it. Since there might be more than one char until you reach EOF char, which needs to be cleared. Use the following function to clear the input buffer.

Code:

```void Clear_Buffer( void ) {     int ch;     while( ( ch = getchar() ) != '\n' && ch != EOF ); }```
And, try avoiding using scanf function.

ssharish
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last