c - Sum of the all the numbers between a and b -
i need create program gives sum of numbers between constants of a , b given user. b needs greater a.
#include <stdio.h> void main() { int index, begno, endno, sum = 0; printf("program sum of numbers in given range\n"); printf("enter beg. no.: "); scanf("%d", &begno); printf("enter end. no.: "); scanf("%d", &endno); index = begno; for(; index <= endno; index ++) sum = sum + index; printf("the sum of numbers between %d , %d is: %d", begno, endno, sum); }
the code given looks ok, if want sum without including last number, case should change loop this
for(; index < endno; index ++)
Comments
Post a Comment