C language L1-008 Find the sum of integer segments

L1-008 Find the segment sum of integers
Given two integers A and B, output all the integers from A to B and the sum of these numbers.

Input format:
The input gives 2 integers A and B in one line, where −100≤A≤B≤100, separated by spaces.

Output format:
First, output all integers from A to B sequentially, with every 5 numbers occupying one line, each number occupying 5 characters in width, aligned to the right. Finally, the sum X of all numbers is output in the format of Sum = X in one line.

Example:
-3 8
Example:
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30
Insert image description here

#include<stdio.h>
int main()
{
    
    
	int a,b,count=0,sum=0;
	scanf("%d %d",&a,&b);
	for(int x=a;x<=b;x++)
	{
    
    
		sum+=x;
		++count;
		printf("%5d",x);
		if(count%5==0&&x!=b)
			printf("\n");
	}
	printf("\nSum = %d\n",sum);
	return 0;
}

Guess you like

Origin blog.csdn.net/ainuliba/article/details/132999160