c语言输出1-10,用for循环

Write a C program that outputs to standard out the numbers from 1 to
10 with comma separation using a 'for' loop. This list of numbers should be on a
single line (put a newline character at the end of the line). Place your answer in the
file called 'count.c' in the Q3 directory. When run from the command line you should
see:
$ ./count
1,2,3,4,5,6,7,8,9,10
$

以下是题目的 C 语言程序代码:
#include <stdio.h>int main() { int i; for (i = 1; i <= 10; i++) { printf("%d", i); if (i < 10) { printf(",");
}
} printf("\n"); return 0;
}c
#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d", i);
if (i < 10) {
printf(",");
}
}
printf("\n");
return 0;
}
在这个程序中,我们使用了一个 for 循环来迭代从 1 到 10 的数字。我们使用了 printf 函数来输出每个数字,并在需要的时候添加逗号。最后,我们在输出末尾添加了一个换行符,以便所有的数字都可以在同一行上打印出来。
将以上代码保存到 count.c 文件中,使用 C 编译器编译并执行该程序,即可得到题目所要求的输出。
$ gcc count.c -o count
$ ./count
1,2,3,4,5,6,7,8,9,10
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-18
希望有帮助
#include<stdio.h>
int main(){
for(int i = 1; i <= 10; i++){
if(i != 10){
printf("%d," i);

}
else{
printf("%d" i);
}
}
printf("\n" );
return 0;

}本回答被提问者和网友采纳
第2个回答  2014-07-18
#include<stdio.h>
int main()
{
printf("1");
for(int i = 2; i <= 10; i++)
{
printf(",%d", i);
}
printf("\n");
return 0;
}
第3个回答  2014-07-18

#include<stdio.h>
main()
{
int i;
for(i=1;i<=10;i++)
printf("%d,",i);
printf("\n");
}

 

第4个回答  2014-07-18
for(n=1;n<11;n++)
{
printf(%d,n);
}
大概就是这样,字符可能不很准确。望采纳。
相似回答