Good evening, I'm basically doing a program that does something like this:
You enter two integers -4 and 6 which represent intervals
Then the loop does this:
(-4) + (-3) + (-2) + (-1) + 0 + 1 + 2 + 3 + 4 + 5 + 6
After that it prints out the sum of everything added. Furthermore if a number which is
added consists of 2 or more digits, the program adds the digits of the number instead the
whole number. Like this:
8 and 10
8+9+1+0

Here's what I got:

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

/* The Beginning of the procedure*/

int main()
{
int integer1; /* First number */
int integer2,temp; /* Second number */
int temp2,temp3,temp4;
int sum,x; /* Variable in which sum will be stored */

printf("Hello, this program calculates between intervals");
printf("\n","");
printf("Enter the first integer:\n");
scanf("%d",&integer1);
printf("Enter the second integer:\n");
scanf("%d",&integer2);
if (integer2<integer1) {
temp = integer1;
integer1 = integer2;
integer2 = temp;
}
printf("The beginning of the interval is: %d\n",integer1);
printf("The end of the interval is: %d\n",integer2);
sum = 0;
temp4 = 0;
for (x = integer1;integer1 < integer2;integer1++);
{
sum = integer1 + (integer1 +1);
}
printf("The sum is: %d\n",sum);
/*while (integer1>9)
{
integer1 = integer1 / 10;
temp2 = integer1 % 10;
printf("The first numbers digits are: %d\n",temp2);
}
while (integer2>9)
{
integer2 = integer2 / 10;
temp3 = integer2 % 10;
printf("The second numbers digits are: %d\n",temp3);
} */

system("PAUSE");

}``````

I'm puzzled as to what to do with the for loop as it most likely does the integer++ part,
but I can't get the sum because loop adds only the last number.

Another problem I'm having is with While loops ( don't mind their placement atm.).
I can't get the last digit.For example I write 169, it return 1 and 6.

Help appreciated.

2
Contributors
2
Replies
4
Views
10 Years
Discussion Span
Last Post by Phanta

Try:

``````for (x = integer1;integer1 < integer2;integer1++);
{
sum += integer1;
}``````

You should add numbers to sum, so take a sheet of papper and look what you are doing.

Ok, everything's working now, I'll mark the thread as solved now. Here's the changes:

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

int main()
{
int integer1;
int integer2,temp;
int sum,x;

printf("Hello, this program calculates between intervals");
printf("\n","");
printf("Enter the first integer:\n");
scanf("%d",&integer1);
printf("Enter the second integer:\n");
scanf("%d",&integer2);
if (integer2<integer1) {
temp = integer1;
integer1 = integer2;
integer2 = temp;
}
printf("The beginning of the interval is: %d\n",integer1);
printf("The end of the interval is: %d\n",integer2);
sum = 0;
[B]for (x = integer1; x <= integer2; x++)
{
int help = abs(x);
while (help>0)
{
sum += help % 10;
help = help / 10;
}
}
printf("The sum is: %d\n",sum);[/B]

system("PAUSE");

}``````