
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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; }
- Related Articles
- C Program to sum the digits of a given number in single statement
- Write a Golang program to find the sum of digits for a given number
- Program to find the sum of all digits of given number in Python
- PHP program to sum the digits in a number
- Find the Largest number with given number of digits and sum of digits in C++
- Find smallest number with given number of digits and sum of digits in C++
- C# program to find the sum of digits of a number using Recursion
- Java program to Count the number of digits in a given integer
- The sum of digits of a two-digit number is 15. The number obtained by reversing the order of digits of the given number exceeds the given number by 9. Find the given number.
- C program to find sum of digits of a five digit number
- Java Program to Find Sum of Digits of a Number using Recursion
- Python Program to Find the Sum of Digits in a Number without Recursion
- C++ program to find sum of digits of a number until sum becomes single digit
- Write a program in Python to count the number of digits in a given number N
- 8085 program to find sum of digits of 8 bit number

Advertisements