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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 5
Reputation: mrayoub is an unknown quantity at this point 
Solved Threads: 0
mrayoub mrayoub is offline Offline
Newbie Poster

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

 
0
  #1
Jun 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

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

 
0
  #2
Jun 27th, 2009
#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;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: mrayoub is an unknown quantity at this point 
Solved Threads: 0
mrayoub mrayoub is offline Offline
Newbie Poster

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

 
0
  #3
Jun 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 2
Reputation: james14 is an unknown quantity at this point 
Solved Threads: 1
james14 james14 is offline Offline
Newbie Poster

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

 
-1
  #4
Jun 27th, 2009
its just

sum=sum+i

hehehe
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 2
Reputation: james14 is an unknown quantity at this point 
Solved Threads: 1
james14 james14 is offline Offline
Newbie Poster

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

 
0
  #5
Jun 27th, 2009
for ( i = 0; i <= number; i++ ) {
sum =sum+ i;
}

try this code...
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

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

 
-1
  #6
Jun 27th, 2009
Originally Posted by mrayoub View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: mrayoub is an unknown quantity at this point 
Solved Threads: 0
mrayoub mrayoub is offline Offline
Newbie Poster

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

 
0
  #7
Jun 27th, 2009
thanks james14 it works too ( well i m a beginner i coudn t figure it out )

thanks


Originally Posted by mathematician View Post
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 .
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: mrayoub is an unknown quantity at this point 
Solved Threads: 0
mrayoub mrayoub is offline Offline
Newbie Poster

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

 
0
  #8
Jun 27th, 2009
Originally Posted by james14 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,615
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

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

 
0
  #9
Jun 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: mrayoub is an unknown quantity at this point 
Solved Threads: 0
mrayoub mrayoub is offline Offline
Newbie Poster

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

 
1
  #10
Jun 27th, 2009
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 !
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC