C++ program to compute the sum of digits in a given numbers

C++ program to compute the sum of digits in a given numbers

In this tutorial, we will discuss a concept of  C++ program to compute the sum of digits in a given numbers

In C++ programming  language, we can use for loop ,while loop and do-while loop to compute the sum of digits in a given numbers

In this article, we are going to learn  how to find the sum of digits in a given number in C++ language

program to calculate the sum of digits

Compute the sum of digits in a given number using for loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    long num,counter,digits,sum=0;
    cout<<"Enter a number of find sum of digits\n";
    cin>>num;//get input from the user for find sum of digits

for(counter=num; num>0; num/=10)
{
    digits=num%10;
    sum=sum+digits;

}
cout<<"Given number is:" <<counter;

cout<<"\nSum of the digits is"<<counter <<"is :" << sum;
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter a number for find sum of digits
1010
Given number is:1010

Sum of the digits  1010 is = 2

 

Case 2

Enter a number for find digits sum
54321
Given number is:54321

Sum of the digits  54321 is = 15

 

Compute the sum of digits in a given number using while loop

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    long num,counter,digits,sum=0;
    cout<<"Enter a number of find sum of digits\n";
    cin>>num;//get input from the user for find sum of digits

counter=num;
while( num>0)
{
    digits=num%10;
    sum=sum+digits;
     num/=10;

}
cout<<"Given number is:" <<counter;

cout<<"\nSum of the digits is"<<counter <<"is :" << sum;
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter a number for find sum of digits
1090
Given number is:1090

Sum of the digits  1090 is = 10

 

Case 2

Enter a number for find digits sum
123456
Given number is:123456

Sum of the digits  123456 is = 21

 

Compute the sum of digits in a given number using do-while loop

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    long num,counter,digits,sum=0;
    cout<<"Enter a number of find sum of digits\n";
    cin>>num;//get input from the user for find sum of digits

counter=num;

do{
    digits=num%10;
    sum=sum+digits;
     num/=10;

}while( num>0);
cout<<"Given number is:" <<counter;

cout<<"\nSum of the digits is"<<counter <<"is :" << sum;
getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter a number for find sum of digits
11001100
Given number is:11001100

Sum of the digits  11001100 is = 4

 

Case 2

Enter a number for find digits sum
876543
Given number is:876543

Sum of the digits  876543 is = 33

 

Methods

  • Declare the variable num, count and digits as long type.
  • Declare and initialize variable sum to Zero.
  • Receive input from the user for the value to store the variable num and to find the sum of digits
  • Using a loop (for loop, while loop, do-while loop) get each digit of the number and add it to the to a variable to find the sum.
  • Display the sum of the digits of the number.

 

Similar post

C program to compute the sum of digits in a given numbers

Java program to compute the sum of digits in a given numbers

Python program to compute the sum of digits in a given numbers

Addition of two numbers in Java using method

5 method to find sum of two numbers

JavaScript program to add two numbers 

PHP Addition of two numbers 

 

 

Suggested for you

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

The operator in C++ language

variable in C++ language

 

 

C program to compute the sum of digits in a given numbers
Python program to compute the sum of digits in a given numbers
C++ programsCpp language