:
int main(void)
{
int n, number, counter, triangularNumber;
n, , , . - int. - .
for(counter = 1; counter <= 5; ++counter)
{
printf("What triangular number do you want? ");
scanf("%i", &number);
triangularNumber = 0;
Now for the first time, the memory in the address where the triangular number indicates indicates the value, the value is 0.
for(n = 1; n <= number; ++n)
{
triangularNumber += n;
If you had not set triangularNumber to 0 on line 12, the first time through this loop, triangularNumber would have an unknown value.
}
printf("Triangular number %i is %i\n\n", number, triangularNumber);
n = 0;
triangularNumber = 0;
not required, a link to these addresses is discarded anyway, and memory is free for other things to be used.
}
return 0;
}
source
share