Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
A C++ Program to find the Sum of Digits of input integer. More...
#include <cassert>
#include <iostream>
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
int main | ( | void | ) |
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.
void test | ( | ) |
Function for testing the sum_of_digits() with all the test cases.
void test1 | ( | ) |
Function for testing the sum_of_digits() function with a first test case of 119765 and assert statement.
void test2 | ( | ) |
Function for testing the sum_of_digits() function with a second test case of -12256 and assert statement.