- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Sum the digits of a given number
Here is an example to calculate the sum of digits in C++ language,
Example
#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; }
Output
Enter the number : 236214828 The sum of the digits : 36
In the above program, two variables x and s are declared and s is initialized with zero. The number is entered by the user and when number is not equal to zero, it will sum up the digits of number.
while (x != 0) { s = s + x % 10; x = x / 10; }
Advertisements