943,793 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 950
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 27th, 2009
0

adding from 0 to a number that will be entered by the user .

Expand Post »
hi,

well this is my first post on this great forum, hope to be a good boy

i m writing a programm that adds numbers from 0 to any numer entered by the user .

for Example :

- if the user enter 4, the sum will be 10 = 0+1+2+3+4.
- if 10 --> the sum is : 1+2+3+4...+10 = 64 .

i make the code like that :

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int number;
  7. int sum;
  8. int i;
  9.  
  10. printf("please enter the end number : \n" );
  11. scanf("%i", &number);
  12.  
  13. for ( i = 0; i <= number; i++ ) {
  14.  
  15. sum += i;
  16.  
  17. }
  18. printf("the sum is : %i\n", sum);
  19. return 0;
  20.  
  21.  
  22. }

the result :

the sum is : 2293586 when i enter 4 i don t know why .

any ideas how to make this work

Thanks.
Last edited by mrayoub; Jun 27th, 2009 at 12:53 pm.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
mrayoub is offline Offline
5 posts
since Jun 2009
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

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

int main()
{
    int number;
    int sum;
    int i;
 
    printf("please enter the end number : \n" );
    scanf("%u", &number);
 
    for ( i = 0, sum = 0; i <= number; i++ ) {
        sum += i;
    }

    printf("the sum is : %i\n", sum);

    return 0;
}
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

thank you very much mathematician for this quick answer but i have small question why did you use %u in the place of %i i mean it works with %i too .

thanks again
Reputation Points: 21
Solved Threads: 0
Newbie Poster
mrayoub is offline Offline
5 posts
since Jun 2009
Jun 27th, 2009
-1

Re: adding from 0 to a number that will be entered by the user .

its just

sum=sum+i

hehehe
Reputation Points: 8
Solved Threads: 1
Newbie Poster
james14 is offline Offline
2 posts
since Jun 2009
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

for ( i = 0; i <= number; i++ ) {
sum =sum+ i;
}

try this code...
Reputation Points: 8
Solved Threads: 1
Newbie Poster
james14 is offline Offline
2 posts
since Jun 2009
Jun 27th, 2009
-1

Re: adding from 0 to a number that will be entered by the user .

Click to Expand / Collapse  Quote originally posted by mrayoub ...
thank you very much mathematician for this quick answer but i have small question why did you use %u in the place of %i i mean it works with %i too .

thanks again
Well firstly, unless it is compiler specific, I have never seen %i before. A decimal integer is usually %d.

Secondly, given what you are doing, you presumably want the user to enter a positive (unsigned) integer (%u).
Last edited by mathematician; Jun 27th, 2009 at 2:10 pm.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

thanks james14 it works too ( well i m a beginner i coudn t figure it out )

thanks


Well firstly, unless it is compiler specific, I have never seen %i before. A decimal integer is usually %d.

Secondly, given what you are doing, you presumably want the user to enter a positive (unsigned) integer (%u).
well we always do this, we work just with %i if its an integer and it works

but the idea to use %ufor the positive numbers is great
thank you mathematecian for solving this .
Reputation Points: 21
Solved Threads: 0
Newbie Poster
mrayoub is offline Offline
5 posts
since Jun 2009
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

Click to Expand / Collapse  Quote originally posted by james14 ...
for ( i = 0; i <= number; i++ ) {
sum =sum+ i;
}

try this code...
well if sum doesn t initialised it wouldnt work !

for(i = 0, sum = 0; i <= number; i++)

will be correct

thanks
Reputation Points: 21
Solved Threads: 0
Newbie Poster
mrayoub is offline Offline
5 posts
since Jun 2009
Jun 27th, 2009
0

Re: adding from 0 to a number that will be entered by the user .

mrayoub:

%i and %d are equivalent. both of them specify the format for a signed decimal integer, and either are acceptable to use.

%u, as you have seen, is for unsigned decimal integer

however, if you have declared a variable of type int then you should not print it as %u, an unsigned int. That is a bad practice to get into... because while it may in many cases appear to work correctly, your integer may very well have a valid negative value , and the %u format will print an incorrect value.

if you want the variable to be unsigned, then make it unsigned by declaring it so: unsigned number;

.
Last edited by jephthah; Jun 27th, 2009 at 8:57 pm.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jun 27th, 2009
1

Re: adding from 0 to a number that will be entered by the user .

thank you very much jephthah this was really helpful, well programming in C has to be very specific .

i got more informations now trough this post .Really nice to join you !
Reputation Points: 21
Solved Threads: 0
Newbie Poster
mrayoub is offline Offline
5 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help to work on structures..
Next Thread in C Forum Timeline: extract multidigits from a char* (substring)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC