TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    C++

    Sum of Digits of a Number C++ Program

    Overview

    C++ program to calculate the sum of digits of a number. The program prompts the user to enter the number. For example: Enter number: 7865 The output should be  7+ 8 + 6 + 5 = 26

    C++ Program

    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
     int sumOfDigits;
     int quotient;
     int digit;
     int number;
    
    
     cout << "Enter a positive number:= "; cin >> number;
     sumOfDigits =0;
     if (number>=0)
     {
     quotient =number;
     while (!(quotient==0))
     {
     digit =quotient % 10;
     sumOfDigits =sumOfDigits+digit;
     quotient =floor(quotient/10);
     }
     cout << "Sum of digits of the number " << number << " is =: " << sumOfDigits << endl;
     }
     else
     {
     cout << "ERROR : Enter positive number" << endl;
     }
    
     return 0;
    }

    sum of digits c++ program

     

    Sample Output

    Sample output of the program is shown below:

    Enter a positive number:= 7865

    Sum of digits of the number 7865 is =: 26 

     

    The IDE used in the program is Code:: Blocks. To download and install Code Blocks follow the link:

    https://www.testingdocs.com/download-and-install-codeblocks/

    For more information on Code Blocks IDE, visit the official website of Code blocks IDE:

    http://www.codeblocks.org/

    —

    C++ Tutorials

    C++ Tutorials on this website:

    https://www.testingdocs.com/c-coding-tutorials/

    For more information on the current ISO C++ standard

    https://isocpp.org/std/the-standard

    Related Posts

    g not found OpenSuse

    C++ /

    Install GCC C++ Compiler on OpenSuse

    C++ Hello World Program

    C++ /

    C++ Hello World Program

    C++ /

    Object-Oriented Programming Language Examples

    C++ /

    Object-Oriented Programming Features

    C++ /

    C++ Program to Find Quadratic Equation Roots

    ‹ C++ Hello World Program› Install GCC C++ Compiler on OpenSuse

    Recent Posts

    • ChatGPT Subscription Plans
    • Stellar Converter for Database
    • Stellar Log Analyzer for MySQL
    • Stellar Repair for MySQL
    • ChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI Features
    • Shaping the Future of Development: Exploring Key Trends in Software Engineering
    • Improving Java Performance with Multithreading
    • Open-source Vector Databases

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com