5

用c语言完成累加操作_Ilyina的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_15750657/5583281
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

用c语言完成累加操作

精选 原创

Ilyina 2022-08-16 17:23:34 博主文章分类:c语言程序 ©著作权

文章标签 利用C语言完成累加操作 文章分类 C/C++ 编程语言 阅读数229

代码:代码文本在文章最下方

变量num:用来存放用户输入的数值                                                            变量sum:用来存放数据的和                                                                      变量status:用做循环的判断条件

用c语言完成累加操作_利用C语言完成累加操作

该程序利用了scanf函数的特性。

scanf函数在得到一个整数时,会把这个值赋给变量num;同时会返回1,再把这个1赋给变量status,这个程序就完成了。scanf函数在得到不是整数的其他值时,会返回0,这时循环结束。

代码文本:

#include<stdio.h>

int main()

long num;

long sum = 0L;

int status;

printf("请输入数据;");

status = scanf("%ld", &num);

while (status == 1)

 sum = sum + num;

 printf("请输入下一个数据,如果想结束,请输入no:");

 status = scanf("%ld", &num);

printf("%ld\n", sum);

return 0;

  • 1
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK