7

C++ Program to Find the Sum of All Digits of a Number

 1 year ago
source link: https://www.geeksforgeeks.org/videos/cpp-program-to-find-the-sum-of-all-digits-of-a-number/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++ Program to Find the Sum of All Digits of a Number

C++ Program to Find the Sum of All Digits of a Number
  • 20 Views
  • 30/05/2022

In this video, we will see a how-to find Sum of the digits of a given number. Let's understand this concept with the help of examples:

Suppose an Input : n = 123 = 1+2+3 = Output: 6

Now take another example and Input n = 1990 = 1+9+9+0 = Output : 19

General Algorithm for the sum of digits in a given number:

Step 1: Get the number. Step 2: Declare a variable to store the sum and set it to 0. Step 3: Repeat the next two steps till the number is not 0. Step 4: Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and add it to sum. Step 5: Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit. Step 6: Print or return the sum.

Program for sum of the digits of a given number: https://www.geeksforgeeks.org/program-for-sum-of-the-digits-of-a-given-number/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK