当前位置:网站首页>7-14 sum integer segments (C language)
7-14 sum integer segments (C language)
2022-07-03 14:14:00 【Big fish】
Given two integers A and B, Output from A To B All the integers of and the sum of these numbers .
Input format :
Enter... On one line 2 It's an integer A and B, among −100≤A≤B≤100, Separated by spaces .
Output format :
First, output sequentially from A To B All integers of , Every time 5 A line of numbers , Each number accounts for 5 Character width , Align right . Finally, in a line, press Sum = X Output the sum of all the numbers X.
sample input :
-3 8
sample output :
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30
PS:
It is a problem of counting and summing cycles . Test point 1 If not , There is a problem with the output format , Output one more line feed . Note that when the number of output numbers is 5 The integral times of , There may be more than one line feed output , Therefore, there is no need to wrap the last line .
Attach code :
#include <stdio.h>
int main(){
int a,b,i;
int sum = 0;
int couter = 0; // To count
scanf("%d %d",&a,&b);
for(i=a;i<=b;i++){ //i Used to record output numbers
sum += i;
couter++;
printf("%5d",i); // Output 5 Characters
if(couter%5 == 0 && i!=b){ // When the count reaches 5 When it's time , It's a new line , And the last line does not need to wrap
printf("\n");
}
}
printf("\n");
printf("Sum = %d",sum);
return 0;
}
边栏推荐
- Leetcode(4)——寻找两个正序数组的中位数
- Exercise 8-2 calculate the sum and difference of two numbers
- [clean up the extraordinary image of Disk C]
- MongoDB索引
- Exercise 10-3 recursive implementation of exponential functions
- JS download files through URL links
- JVM object lifecycle
- concat和concat_ws()区别及group_concat()和repeat()函数的使用
- JS input number and standard digit number are compared. The problem of adding 0 to 0
- QT learning 23 layout manager (II)
猜你喜欢
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
x86汇编语言-从实模式到保护模式 笔记
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
jvm-类加载
剑指 Offer 28. 对称的二叉树
Message subscription and publishing
牛客网:过河卒
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Exercise 10-6 recursively find Fabonacci sequence
随机推荐
Exercise 6-1 classify and count the number of characters
MongoDB索引
protobuf与grpc
虽然不一定最优秀,但一定是最努力的!
Current situation, analysis and prediction of information and innovation industry
信创产业现状、分析与预测
Redis:字符串類型數據的操作命令
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
玖逸云黑免费无加密版本源码
金属有机骨架材料ZIF-8包载姜黄素([email protected]纳米颗粒)|纳米金属有机框架搭载雷帕霉素|科研试剂
Exercise 10-6 recursively find Fabonacci sequence
Exercise 6-2 using functions to sum special A-string sequences
7-8 overspeed judgment
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
Exercise 7-6 count capital consonants
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
JVM class loading
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
JS get DPI, PX to cm, cm to PX