c++ 逆序输出数字

来源:百度知道 编辑:UC知道 时间:2024/06/09 21:28:20
#include<iostream>
#include<cmath>
using namespace std;
int main()
{

char c;
int i=0;
cout<<"输入长度不大于5位的正整数:"<<endl;
while((c=getchar())!='\n')
{
i+=1;
putchar(c);
cout<<",";
}

cout<<endl;
cout<<"是"<<i<<"位整数"<<endl;

return 0;

}
还想实现的功能:逆序输出数字,限制输出5个正整数。
请高手帮忙,先谢谢了,在线等!
改下:不是限制输出5个正整数,是输入一个不多于5位的正整数。
除了这种方法:对该5位数进行取余,分别求出5位上的各个数,再先输出原来的个位、再十位···

定义一个数组,记录输入的每一位,然后再逆序输出这个数组

#include<iostream>
#include<stdio.h>
#include<cmath>

using namespace std;
int main()
{

char c;
char d[5];
int i = 0;
cout << "输入长度不大于5位的正整数:" << endl;

while ((c = getchar()) != '\n')
{
d[i] = c;
i += 1;
putchar(c);
cout << ",";
}

cout << endl;

cout << "是" << i << "位整数" << endl;

cout << "逆序输出数字:"<<endl;
while(i>0)
{
cout << d[i-1] << ",";
i--;
}
return 0;

}

对该5位数进行取余,分别求出5位上的各个数,再先输出原来的个位、再十位····就可以了嘛~
奥,你不要用char c 来存各位数,用char c[5]来,然后逆序输出就可以了

#include "stdio.h"
#include "conio.h"
main()
{<