关于for循环嵌套的问题

syn724 2008-07-10 12:12:45
我是一名c语言初学者,在一本书上看见关于for循环嵌套的一篇代码,但是其中的语句有些问题不能编译,请大家帮我改进下,谢谢!
#include <stdio.h>

int main(void)
{
long sum = 1L;
int j = 1;
int count = 0;

/* Prompt for, and read the input count */
printf("\nEnter the number of integers you want to sum: ");
scanf(" %d", &count);

for(int i = 1 ; i <= count ; i++)
{
sum = 1L;
j=1;
printf("\n1");

/* Calculate sum of integers from 1 to i */
while(j < i)
{
sum += ++j;
printf("+%d", j);
}
printf(" = %ld\n", sum);
}
return 0;
}
书上面的程序输出如下:
Enter the number of integers you want to sum:5

1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
烂泥 2008-07-10
  • 打赏
  • 举报
回复
#include <stdio.h> 

int main(void)
{
long sum = 1L;
int j = 1;
int count = 0;
int i;

/* Prompt for, and read the input count */
printf("\nEnter the number of integers you want to sum: ");
scanf(" %d", &count);

for( i = 1 ; i <= count ; i++)
{
sum = 1L;
j=1;
printf("\n1");

/* Calculate sum of integers from 1 to i */
while(j < i)
{
sum += ++j;
printf("+%d", j);
}
printf(" = %ld\n", sum);
}
return 0;
}
这样!
烂泥 2008-07-10
  • 打赏
  • 举报
回复
#include <stdio.h> 

int main(void)
{
long sum = 1L;
int j = 1;
int count = 0;
int i;
/* Prompt for, and read the input count */
printf("\nEnter the number of integers you want to sum: ");
scanf(" %d", &count);

for( i = 1 ; i <= count ; i++)
{
sum = 1L;
j=1;
printf("\n1");

/* Calculate sum of integers from 1 to i */
while(j < i)
{
sum += ++j;
printf("+%d", j);
}
printf(" = %ld\n", sum);
}
return 0;
这样就行 了!先把i定义为整型!
artman 2008-07-10
  • 打赏
  • 举报
回复
ALL OK! with VC6
wahl1986 2008-07-10
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{int n;
int sum=0;
cout<<"Enter the number of integers you wants to sum:";
cin>>n;
cout<<endl;
for(int i=1;i<=n;i++)
{sum=sum+i;
for(int j=1;j<i;j++)
{
cout<<j;
cout<<"+";
}

cout<<i<<"="<<sum;
cout<<endl;
}
return 0;
}比书上的简单
syn724 2008-07-10
  • 打赏
  • 举报
回复
呵呵 ,谢谢你。问题已经解决。不过我奇怪的是,书上的for循环的控制表达式中,都是在表达式里面定义的类型。但是这样写法在VC里面会出现报错。看来还是我RP的问题。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧