作业帮 > 综合 > 作业

C语言用while写出1-2+3-4+5-6...+N的合

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/28 07:29:44
C语言用while写出1-2+3-4+5-6...+N的合
用while写出来 不要do while和 for的
清楚点
C语言用while写出1-2+3-4+5-6...+N的合
#include <stdio.h>

int main(void) /* 1-2+3-4+5-6...+N */
{
\x05int this = 1; /* 运算起始 */
\x05int n;
\x05scanf("%d",&n); /* 输入N的值 */
\x05int sum = 0;
\x05while (this <= n) {
\x05\x05if (this%2 == 0) /* 是合数 */
\x05\x05\x05sum -= this;
\x05\x05else /* 是基数 */
\x05\x05\x05sum += this;
\x05\x05this ++; /* this递增 */
\x05}
\x05printf("%d\n",sum);
\x05
\x05return 0;
}

给分吧!
再问: - - 我现在没学什么递增 合数 和基数
好想老师就弄了++A这样的 能做出来么
再答: =。=我原本怕太复杂了你看不懂。。#include <stdio.h>
int main(void) /* 1-2+3-4+5-6...+N */{\x09int this = 0, sum = 0, n; /* 运算起始 */\x09scanf("%d", &n); /* 输入N的值 */\x09while (++this <= n) sum = (this%2 == 0) ? sum - this : sum + this;\x09printf("%d\n", sum);\x09\x09return 0;}
再问: 谢谢 大神
再答: 采纳吧A_A