Sum of Integers in given range coderinme

Sum of Integers in given range coderinme

Sum of Integers in given range coderinme
Finding the sum of all numbers in a given range manually would be very hectic, frustrating as well as a time taking process. To make it simple Let us now write a program in C that reads two integers named start and end in the main program. It passes these two integers to a function sub program that uses for loop to find the sum of all the integers from start to end and returns the sum to the main program. The main program should print the sum.

What we are supposed to do here is very simple. Assuming that the start number is 5 and the end number is 8, then the required output is 5 + 6 + 7 + 8 = 26

#include<stdio.h>
int summ(int a,int b)
{
int sum=0;
for(; a <= b ; a++)
{
sum = sum + a;
}
return sum;
}
int main()
{
int start,end,sum;
printf("\nEnter the start number: ");
scanf("%d",&start);
printf("Enter the end number: ");
scanf("%d",&end);
sum = summ(start,end);
printf("\nThe sum of integers from %d to %d is: %d",start,end,sum);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Sum of Integers in given range coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.