C++ 信奥刷题
1.将一个整数中的所有数字相加(数位和)编写一个程序,读入一个1到99范围内的整数,将此整数中的所有数字相加。比如23,输出结果为5。
2.编写一个程序,读入一个100到999范围内的整数,将此整数中的所有数字相加。比如999,输出结果为27。提示: 23%10=3 23/10=2
#include <iostream>
using namespace std;
int main()
{
int x,y,g,z;
cout<<"请输入1到99的数宇"<<std::end1:
cin>> g ;
if(g <99 && g >1){
x = g/10;
y = g%10;
z = x+y;
cout<<"1到99的数位和答案是"<< z <<std::endl;
}else{
cout<<"您的条件不符合"<< z <<std::endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x,y,g,z;
cout<<"请输入100到999的数字"<<std:;endl;
cin>> g ;
if(g < 999 && g > 100){
x = g/100;
y = g%100/10;
s =g%100%10;
z = x+y+s;
cout<<"1到999的数位和答案是"<< z <<std::endl;
}else{
cout<<"您的条件不符合"<< z <<std::endl;
}
return 0;
}