From de4578c1a47d9c03eb96aa863ed109bec80933f5 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Mon, 15 Jun 2020 22:11:20 +0530 Subject: [PATCH 1/7] feat: add sum of digits --- math/sum_of_digits.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 math/sum_of_digits.cpp diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp new file mode 100644 index 000000000..d3e7d3291 --- /dev/null +++ b/math/sum_of_digits.cpp @@ -0,0 +1,28 @@ +/** + * A simple C++ Program to find the Sum of Digits of input integer. + */ +#include + +/** + * Function to find the sum of the digits of an integer. + * @param num The integer. + * @return Sum of the digits of the integer. + */ +int sum_of_digits(int num) { + int sum = 0; + while (num > 0) { + sum = sum + (num % 10); + num = num / 10; + } + return sum; +} + +int main() { + int num; + std::cin >> num; + if (num < 0) { + num = -1 * num; + } + std::cout << sum_of_digits(num); + return 0; +} \ No newline at end of file From d86a9b56df77b0b6544c2d40fb1482c41a779dc3 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Mon, 15 Jun 2020 23:17:50 +0530 Subject: [PATCH 2/7] fix: sum of digits bug --- math/sum_of_digits.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index d3e7d3291..b259e48da 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -1,7 +1,11 @@ /** - * A simple C++ Program to find the Sum of Digits of input integer. + * Copyright 2020 @author iamnambiar + * + * @file + * A C++ Program to find the Sum of Digits of input integer. */ #include +#include /** * Function to find the sum of the digits of an integer. @@ -9,6 +13,9 @@ * @return Sum of the digits of the integer. */ int sum_of_digits(int num) { + if (num < 0) { + num = -1 * num; + } int sum = 0; while (num > 0) { sum = sum + (num % 10); @@ -17,12 +24,18 @@ int sum_of_digits(int num) { return sum; } -int main() { - int num; - std::cin >> num; - if (num < 0) { - num = -1 * num; +/** + * Test function. + */ +void test() { + int test_case_1 = sum_of_digits(119765); + int test_case_2 = sum_of_digits(-12256); + assert(test_case_1 == 29); + assert(test_case_2 == 16); } - std::cout << sum_of_digits(num); + +int main() { + test(); + std::cout << "Success."; return 0; -} \ No newline at end of file +} From 2191ef4881f6b3a94b9f26189b320fb27ef06692 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Mon, 15 Jun 2020 23:26:58 +0530 Subject: [PATCH 3/7] fix: whitespace issue in sum_of_digits --- math/sum_of_digits.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index b259e48da..cf8c28360 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -27,7 +27,7 @@ int sum_of_digits(int num) { /** * Test function. */ -void test() { +void test() { int test_case_1 = sum_of_digits(119765); int test_case_2 = sum_of_digits(-12256); assert(test_case_1 == 29); From 12f0ec3c45fc2789be69a8a579b0eee92e356412 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Tue, 16 Jun 2020 08:03:14 +0530 Subject: [PATCH 4/7] test: add two test functions --- math/sum_of_digits.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index cf8c28360..460bff691 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -11,8 +11,17 @@ * Function to find the sum of the digits of an integer. * @param num The integer. * @return Sum of the digits of 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. */ int sum_of_digits(int num) { + // If num is negative then negative sign is neglected. if (num < 0) { num = -1 * num; } @@ -25,17 +34,38 @@ int sum_of_digits(int num) { } /** - * Test function. + * Function for testing the sum_of_digits() function with a + * first test case of 119765 and assert statement. */ -void test() { +void test1() { int test_case_1 = sum_of_digits(119765); - int test_case_2 = sum_of_digits(-12256); assert(test_case_1 == 29); - assert(test_case_2 == 16); } +/** + * Function for testing the sum_of_digits() function with a + * second test case of -12256 and assert statement. + */ +void test2() { + int test_case_2 = sum_of_digits(-12256); + assert(test_case_2 == 16); + } +/** + * Function for testing the sum_of_digits() with + * all the test cases. + */ +void test() { + //First test. + test1(); + //Second test. + test2(); + } + +/** + * Main Function +*/ int main() { test(); - std::cout << "Success."; + std::cout << "Success." << std::endl; return 0; } From 05aa382a168b817145e6a956096fe88dac147e95 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Tue, 16 Jun 2020 08:05:06 +0530 Subject: [PATCH 5/7] docs: comment space fix --- math/sum_of_digits.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index 460bff691..42f0b5e82 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -55,9 +55,9 @@ void test2() { * all the test cases. */ void test() { - //First test. + // First test. test1(); - //Second test. + // Second test. test2(); } From d9e3053fade7b68a9fd61b72b5a2b0db72cd98a3 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Thu, 18 Jun 2020 11:04:27 +0530 Subject: [PATCH 6/7] fix: formatting code style. --- math/sum_of_digits.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index 42f0b5e82..bcb6d7956 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -40,7 +40,7 @@ int sum_of_digits(int num) { void test1() { int test_case_1 = sum_of_digits(119765); assert(test_case_1 == 29); - } +} /** * Function for testing the sum_of_digits() function with a @@ -49,7 +49,8 @@ void test1() { void test2() { int test_case_2 = sum_of_digits(-12256); assert(test_case_2 == 16); - } +} + /** * Function for testing the sum_of_digits() with * all the test cases. @@ -59,7 +60,7 @@ void test() { test1(); // Second test. test2(); - } +} /** * Main Function From f7a8b7a85f3b227e8ebb7f865e211be9b3a0a61c Mon Sep 17 00:00:00 2001 From: Neeraj C <35414531+iamnambiar@users.noreply.github.com> Date: Sat, 20 Jun 2020 21:09:43 +0530 Subject: [PATCH 7/7] Update math/sum_of_digits.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- math/sum_of_digits.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index bcb6d7956..d5a705c48 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -2,7 +2,7 @@ * Copyright 2020 @author iamnambiar * * @file - * A C++ Program to find the Sum of Digits of input integer. + * \brief A C++ Program to find the Sum of Digits of input integer. */ #include #include