Description

What is sum of digit?

We can get the sum of the digit by adding every digits of the given number forgetting the place of value of the digit.

For example:

if we have the number: 382

We can calculate the digit sum as 3 + 8 + 2 = 13.

Algorithm of the sum of digit.

Step 1: Get number by user.

Step 2: Calculate the remainder of the number.

Step 3: sum of the remainder.

Step 4: Divide by the 10 (for remove the digit).

Step 5: REPEAT the step 2, step 3 and step 4 while the number is not equal to zero (n!=0).

Sum of digit program in c programming language.

#include<stdio.h>
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
int sum = 0, x;
while(n!=0){
x = n % 10;
sum = sum + x;
n = n / 10;
}
printf("Sum is : %d",sum); 
return 0;
}

Sum of digits c++

#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the number : ";
cin>>n;
int sum = 0, x;
while(n!=0){
x = n % 10;
sum = sum + x;
n = n / 10;
}
cout<<"Sum is : "<<sum; 
return 0;
}

Output :

Enter the number: 123
Sum is : 6

Comments

Login is mandatory to comment Please login

Recommended Posts

Program to Calculate the power of given numbers by user in c and cpp

In this program, we will take two inputs from the users, one will be the base and the another one is exponent.

Write a program to count the digit in a Number

To count the digits of a given number, divided that number by 10 until that number is greater than 0.

Insertion and Deletion of all operation at Singly Linked List in C Programming Language

Insertion and Deletion in Singly(singular) linked list in C programming langauge Data Structure. How to Implement insertion and deletion.

Simple Macro Substitution(#define) in C and C++ programming language

Program to Explain Simple Macro Substitution ( #define ) c and c++ programming language

Print numbers from 1 to 100 using while loop c and cpp program

C program to print numbers from 1 to 100 using while loop and also c++ program.

How to make swastik in c and cpp programming language

Write a c and c++ program to print the Swastik. How to make Swastik in c and cpp.