I want to build a program so that it will sum integers from any "start" to "finish" spaced by a "step". Negative step values are allowed. For invalid combinations, i.e. “step” = 0, start less than finish with a negative step, or start bigger than finish with a positive step; I want the program to terminate and print an error message.
Code:
/* Sum of Spaced Integers */
/* Ian Richardson */
/* 02/14/2003 */
#include <stdio.h>
int main (void)
{
/* intialization phase */
int count, start, step, end, sum = 0;
printf("What is your starting number?\n");
scanf("%d", &start);
printf("What is your ending number?\n");
scanf("%d", &end);
printf("What interval step would you like to use?\n");
scanf("%d", &step);
if ( step > 0 )
else if ( step < 0 )
else if ( start < end && step > 0 )
else if ( start > end && step < 0 ) {
for ( count = start; count <= end; count += step ) {
sum += count;
}
printf("The start number is: %d\n", start );
printf("The end number is: %d\n", end );
printf("The sum of the spaced integers is: %d\n", sum );
}
else
printf("Error: Invalid step entered.\n");
return 0;
}