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;
}

 

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

C++ /

Install GCC C++ Compiler on OpenSuse

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

  • Scaler Academy – An Online Learning Platform
  • Difference between PHP and JavaScript?
  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com