| | |
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:
Solved Threads: 0
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 :
the result :
the sum is : 2293586 when i enter 4
i don t know why .
any ideas how to make this work
Thanks.
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 :
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { int number; int sum; int i; printf("please enter the end number : \n" ); scanf("%i", &number); for ( i = 0; i <= number; i++ ) { sum += i; } printf("the sum is : %i\n", sum); return 0; }
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.
•
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
#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;
}•
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
•
•
•
•
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
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.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
thanks james14 it works too ( well i m a beginner i coudn t figure it out
)
thanks
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 .
)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).
but the idea to use %ufor the positive numbers is great

thank you mathematecian for solving this .
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
if you want the variable to be unsigned, then make it unsigned by declaring it so:
.
%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.
![]() |
Similar Threads
- Displaying Prime Number (C#)
- How to sum cubes and squares of the number input by the user (C++)
- counting number of letters in the strings entered by the user (C++)
- help in function to return the number of items greater than certain number (C++)
- Please help! Sudoku in C (C)
- Trying To Find The Square Root Of Each Number Of A Array (C++)
- Loop program help (C++)
- Applet Help (Java)
- Mortgage Program help (Java)
Other Threads in the C Forum
- Previous Thread: help to work on structures..
- Next Thread: extract multidigits from a char* (substring)
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






