Write a c++ program to Print the sum of digits of a given number

 #include <iostream>

#include <cmath>

using namespace std;


// Method 1: Using iteration (while loop)

int sumOfDigitsIterative(int num) {

    int sum = 0, digit;

    

    // Handle negative numbers

    num = abs(num);

    

    while (num > 0) {

        digit = num % 10;      // Get last digit

        sum += digit;          // Add to sum

        num = num / 10;        // Remove last digit

    }

    

    return sum;

}


// Method 2: Using recursion

int sumOfDigitsRecursive(int num) {

    // Handle negative numbers

    num = abs(num);

    

    // Base case

    if (num == 0)

        return 0;

    

    // Recursive case: last digit + sum of remaining digits

    return (num % 10) + sumOfDigitsRecursive(num / 10);

}


// Method 3: Using string conversion

int sumOfDigitsString(int num) {

    int sum = 0;

    

    // Convert to string

    string numStr = to_string(abs(num));

    

    // Iterate through each character

    for (char ch : numStr) {

        sum += (ch - '0');  // Convert char to int and add

    }

    

    return sum;

}


int main() {

    int number;

    

    cout << "========== SUM OF DIGITS PROGRAM ==========\n\n";

    

    cout << "Enter a number: ";

    cin >> number;

    

    // Method 1: Iterative approach

    cout << "\n***** METHOD 1: ITERATIVE APPROACH *****\n";

    int sum1 = sumOfDigitsIterative(number);

    cout << "Sum of digits of " << number << " = " << sum1 << endl;

    

    // Show step-by-step process

    cout << "\nStep-by-step breakdown:\n";

    int temp = abs(number);

    int digit;

    while (temp > 0) {

        digit = temp % 10;

        cout << "Digit: " << digit << endl;

        temp /= 10;

    }

    

    // Method 2: Recursive approach

    cout << "\n***** METHOD 2: RECURSIVE APPROACH *****\n";

    int sum2 = sumOfDigitsRecursive(number);

    cout << "Sum of digits of " << number << " = " << sum2 << endl;

    

    // Method 3: String approach

    cout << "\n***** METHOD 3: STRING APPROACH *****\n";

    int sum3 = sumOfDigitsString(number);

    cout << "Sum of digits of " << number << " = " << sum3 << endl;

    

    // Additional examples

    cout << "\n\n***** TESTING WITH DIFFERENT NUMBERS *****\n";

    int testNumbers[] = {123, 4567, 9999, -456, 1000};

    

    for (int i = 0; i < 5; i++) {

        int num = testNumbers[i];

        int result = sumOfDigitsIterative(num);

        cout << "Sum of digits of " << num << " = " << result << endl;

    }

    

    // Summary

    cout << "\n\n========== HOW IT WORKS ==========\n";

    cout << "Example: Number = 12345\n";

    cout << "Step 1: Extract last digit (5) using num % 10\n";

    cout << "Step 2: Add to sum (sum = 0 + 5 = 5)\n";

    cout << "Step 3: Remove last digit using num / 10 (num = 1234)\n";

    cout << "Step 4: Repeat until num becomes 0\n";

    cout << "Process: 5 + 4 + 3 + 2 + 1 = 15\n";

    

    return 0;

}




Comments