c++分离一个三位数或者是两位数,分离后的数字相加

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:19:39
我想用C++写一段程序
int A,a,b,c;
A=a+b+c;
如果A是一个三位数,那么分离百位、十位、个位,之后输出百位+十位+个位;
如果A是一个两位数,那么分离十位、个位,之后输出十位+个位。
就这么简单啦,大家来帮帮忙,谢谢。

int A,S=0;
cin >> A;
while(A!=0)
{
S+=A%10;
A/=10;
}
cout << S;

#include <iostream>
using namespace std; // 这两句可以用#include <iostream.h>替代

void main()
{
int i, num, d[3] = {0};
cout << "请输入一个三位数:";
cin >> num;
i = 0;

while (num)
{
d[i++] = num % 10;
num /= 10;
}

num = d[0] + d[1] + d[2];

cout << num << endl;
}

# include <iostream>
using namespace std;
void main()
{
int shu;
int b,s,g;
cout<<"input the number"<<endl;
cin>>shu;
b=shu/100;
s=(shu-b*100)/10;
g=shu-b*100-s*10;
int sum=b+s+g;
cout<<b<<" "<<s<<" "<<g<<" "<<sum<<endl;
}两位数的也是一样

我用dev C++帮你编的,可能有些繁琐,因为只有这样dev c++才能编译运行