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 :

#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.

Recommended Answers

All 11 Replies

#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;
}

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 ;)

its just

sum=sum+i

hehehe

commented: no it's not. -2
for ( i = 0; i <= number; i++ ) {
        sum =sum+ i;
}

try this code...

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).

commented: (1) %i is not compiler specific. (2) don't presume anything. -2

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 .

well if sum doesn t initialised it wouldnt work !

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

will be correct ;)

thanks :)

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; .

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 !

commented: yes, C is very structured. nowhere near as loose as some other languages. +11

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

just as an afterthought, i should clarify that the above statement is only strictly true for "printf()"

for "scanf()", the %d and %i are subtly different ... %d requires a decimal (base 10) integer input, while %i assumes decimal by default, but will also read number as octal if it is prefixed by '0', or hexadecimal if prefixed by '0x' or '0X'

the meaning of

sum+=i;

is same as the

sum=sum+i;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.