C++ 程序求给定数字的数字之和
以下是使用 C++ 语言计算数字之和的示例:
示例
#include<iostream>
using namespace std;
int main() {
int x, s = 0;
cout << "Enter the number : ";
cin >> x;
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
cout << "\nThe sum of the digits : "<< s;
}输出
Enter the number : 236214828 The sum of the digits : 36
在上述程序中,声明变量 x 和 s,并将 s 初始化为零。用户输入数字,当数字不等于零时,将对数字的位数求和。
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP