C++输出怎么换行?

#include <stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
while(x!=0)
{
y=x%10;
if(y!=0)
printf("%d",y);
x=x/10;
}
return 0;
}

我输入123
输出是321Press any key to continue
怎么能让后面的英文句子换到下行显示?

两种方法:

一:cout<<endl;

二:printf("\n");

例如:

//参考代码如下:
#include "iostream"
#include "stdio.h
#include <string.h>
using namespace std; 
int main()
{
printf("1\n");//方法一
cout<<2<<endl;//方法二
cout<<3;
    return 0; 
}
/*
运行结果:
1
2
3
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-08-23

换行4种方法

1.

cout<<endl;//同cout<<"\n"或cout<<'\n'

头文件:iostream  命名空间:std

2.

printf("\n");

头文件:cstdio

3.

putchar('\n');

头文件:cstdio

4.

puts("");//引号里没有内容是因为puts会自动换行

头文件:cstdio

说明一下  endl比'\n'慢  还是第四种最简单


修改后代码:

#include<stdio.h>
int main()
{
    int x,y;
    scanf("%d",&x);
    while(x)
    {
        y=x%10;
        if(y)
            printf("%d",y);
        x/=10;
    }
    return!puts("");
}

第2个回答  2015-09-21
#include<iostream.h>
int main()
{
    cout << "hello" << endl << "world"<<endl;
//使用字符 endl 换行
}

第3个回答  2011-10-28
//此为标准的C语言,并非C++

#include <stdio.h>
int main()
{
int x, y;
scanf("%d", &x);
while(x != 0)
{
y = x % 10;
if(y != 0)
printf("%d\n", y); //添加 \n 换行
x = x / 10;
}
return 0;
}
第4个回答  2011-10-28
在return 0前面加一个printf("%n");本回答被提问者采纳
相似回答