Sum of digits of a number
In this article, we will discuss how to find the sum of digits of a given number. The task is to calculate the sum of all the individual digits present in the number, regardless of whether the number is positive or negative.
Problem Statement
Given a number, we need to calculate the sum of its digits. The input can be positive or negative.
Example:
- Input: 123
- Output: 6 (1 + 2 + 3 = 6)
- Input: 562
- Output: 13 (5 + 6 + 2 = 13)
- Input: -562
- Output: 13 (-5 + 6 + 2 = 13)
Algorithm
1. Start
2. Accept the number as input.
3. Initialize a variable called 'result' to store the sum of the digits.
4. If the number is negative, convert it to a positive number.
5. Use a while loop to iterate until the number becomes zero.
6. Inside the loop, extract the last digit of the number using the modulo operator and add it to the 'result' variable.
7. Divide the number by 10 to remove the last digit.
8. Repeat steps 6 and 7 until the number becomes zero.
9. Print the value of 'result', which represents the sum of the digits.
10. Stop
Pseudocode
sum_digits(number)
{
result = 0
if (number < 0)
number = -number
while (number != 0)
{
result += number % 10
number /= 10
}
print result
}
main()
{
sum_digits(123)
sum_digits(562)
sum_digits(-562)
}
Explanation
The given code calculates the sum of digits for a given number using the sum_digits function.
The sum_digits function takes a number as input and initializes a variable called 'result' to store the sum of the digits. If the number is negative, it converts it to a positive number by taking its absolute value.
It then uses a while loop to extract the last digit of the number using the modulo operator (%), adds it to the 'result', and divides the number by 10 to remove the last digit. This process continues until the number becomes zero.
Finally, the function prints the value of 'result', which represents the sum of the digits.
In the main function, several test cases are provided to demonstrate the functionality of the sum_digits function. The outputs for the given test cases are also shown.
Code Solution
Here given code implementation process.
//C Program
//Sum of digits of a number
#include <stdio.h>
// Method which are sum of given number
// This method are add all digits and not take negative sign
void sum_digits(int number)
{
int result = 0;
if(number<0)
{
// Convert negative number to positive number
number = - number;
}
while(number!=0)
{
// Adding last digit of a given number
result+=number%10;
// Remove last digit
number/=10;
}
printf("%d\n",result );
}
int main()
{
//Test Case
sum_digits(123);
sum_digits(562);
//when passes negative number
sum_digits(-562);
return 0;
}
Output
6
13
13
/*
C++ Program
Sum of digits of a number
*/
#include<iostream>
using namespace std;
class MyNumber {
public:
// Method which are sum of given number
// This method are add all digits and not take negative sign
void sum_digits(int number) {
int result = 0;
if (number < 0) {
// Convert negative number to positive number
number = -number;
}
while (number != 0) {
// Adding last digit of a given number
result += number % 10;
// Remove last digit
number /= 10;
}
cout << result << "\n";
}
};
int main() {
MyNumber obj ;
//Test Case
obj.sum_digits(123);
obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562);
return 0;
}
Output
6
13
13
/*
Java Program
Sum of digits of a number
*/
public class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
public void sum_digits(int number)
{
int result = 0;
if(number<0)
{
// Convert negative number to positive number
number = - number;
}
while(number!=0)
{
// Adding last digit of a given number
result+=number%10;
// Remove last digit
number=number/10;
}
System.out.print(result+"\n" );
}
public static void main(String[] args) {
MyNumber obj = new MyNumber();
//Test Case
obj.sum_digits(123);
obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562);
}
}
Output
6
13
13
/*
C# Program
Sum of digits of a number
*/
using System;
public class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
public void sum_digits(int number) {
int result = 0;
if (number < 0) {
// Convert negative number to positive number
number = -number;
}
while (number != 0) {
// Adding last digit of a given number
result += number % 10;
// Remove last digit
number = number / 10;
}
Console.Write(result + "\n");
}
public static void Main(String[] args) {
MyNumber obj = new MyNumber();
//Test Case
obj.sum_digits(123);
obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562);
}
}
Output
6
13
13
# Python 3 Program
# Sum of digits of a number
class MyNumber :
# This method are add all digits and not take negative sign
# Method which are sum of given number
def sum_digits(self, number) :
result = 0
if (number < 0) :
# Convert negative number to positive number
number = -number
while (number != 0) :
# Adding last digit of a given number
result += number % 10
# Remove last digit
number = int(number / 10)
print(result)
def main() :
obj = MyNumber()
#Test Case
obj.sum_digits(123)
obj.sum_digits(562)
#When passes negative number
obj.sum_digits(-562)
if __name__ == "__main__":
main()
Output
6
13
13
# Ruby Program
# Sum of digits of a number
class MyNumber
# This method are add all digits and not take negative sign
# Method which are sum of given number
def sum_digits(number)
result = 0
if (number < 0)
# Convert negative number to positive number
number = -number
end
while (number != 0)
# Adding last digit of a given number
result += number % 10
# Remove last digit
number = number / 10
end
print(result ,"\n")
end
end
def main()
obj = MyNumber.new()
#Test Case
obj.sum_digits(123)
obj.sum_digits(562)
#When passes negative number
obj.sum_digits(-562)
end
main()
Output
6
13
13
/*
Scala Program
Sum of digits of a number
*/
class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
def sum_digits(value: Int): Unit = {
var result: Int = 0;
var number: Int = value;
if (number < 0) {
// Convert negative number to positive number
number = -number;
}
while (number != 0) {
// Adding last digit of a given number
result += number % 10;
// Remove last digit
number = number / 10;
}
print(result);
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.sum_digits(123);obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562);
}
}
Output
6
13
13
/*
Swift 4 Program
Sum of digits of a number
*/
class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
func sum_digits(_ value: Int) {
var result: Int = 0;
var number: Int = value;
if (number < 0) {
// Convert negative number to positive number
number = -number;
}
while (number != 0) {
// Adding last digit of a given number
result += number % 10;
// Remove last digit
number = number / 10;
}
print(result);
}
}
func main() {
let obj: MyNumber = MyNumber();
//Test Case
obj.sum_digits(123);
obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562);
}
main();
Output
6
13
13
<?php
/*
Php Program
Sum of digits of a number
*/
class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
public function sum_digits($number) {
$result = 0;
if ($number < 0) {
// Convert negative number to positive number
$number = -$number;
}
while ($number != 0) {
// Adding last digit of a given number
$result += $number % 10;
// Remove last digit
$number = intval($number / 10);
}
echo ($result."\n");
}
}
function main() {
$obj = new MyNumber();
//Test Case
$obj->sum_digits(123);
$obj->sum_digits(562);
//When passes negative number
$obj->sum_digits(-562);
}
main();
?>
Output
6
13
13
/*
Node Js Program
Sum of digits of a number
*/
class MyNumber {
// Method which are sum of given number
// This method are add all digits and not take negative sign
sum_digits(number) {
var result = 0;
if (number < 0) {
// Convert negative number to positive number
number = -number;
}
while (number != 0) {
// Adding last digit of a given number
result += number % 10;
// Remove last digit
number = parseInt(number / 10);
}
process.stdout.write(result + "\n");
}
}
function main(args) {
var obj = new MyNumber();
//Test Case
obj.sum_digits(123);
obj.sum_digits(562);
//When passes negative number
obj.sum_digits(-562)
}
main();
Output
6
13
13
Time Complexity
The time complexity of the given code is O(log n), where n is the absolute value of the input number. This is because the while loop iterates the number of digits times, and the number of digits in a number is proportional to log n.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment