TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
A C++ Program to find the Sum of Digits of input integer. More...
#include <cassert>
#include <iostream>
Go to the source code of this file.
Functions | |
int | sum_of_digits (int num) |
void | test1 () |
void | test2 () |
void | test () |
int | main () |
A C++ Program to find the Sum of Digits of input integer.
Copyright 2020
Definition in file sum_of_digits.cpp.
int main | ( | void | ) |
Main Function
Definition at line 68 of file sum_of_digits.cpp.
int sum_of_digits | ( | int | num | ) |
Function to find the sum of the digits of an integer.
num | The integer. |
\detail First the algorithm check whether the num is negative or positive, if it is negative, then we neglect the negative sign. Next, the algorithm extract the last digit of num by dividing by 10 and extracting the remainder and this is added to the sum. The number is then divided by 10 to remove the last digit. This loop continues until num becomes 0.
Definition at line 23 of file sum_of_digits.cpp.
void test | ( | ) |
Function for testing the sum_of_digits() with all the test cases.
Definition at line 58 of file sum_of_digits.cpp.
void test1 | ( | ) |
Function for testing the sum_of_digits() function with a first test case of 119765 and assert statement.
Definition at line 40 of file sum_of_digits.cpp.
void test2 | ( | ) |
Function for testing the sum_of_digits() function with a second test case of -12256 and assert statement.
Definition at line 49 of file sum_of_digits.cpp.