Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++, no algorithms use loops and function thank you Part 1: write a function tha

ID: 3596165 • Letter: C

Question

C++, no algorithms

use loops and function

thank you

Part 1: write a function that the computes the non-negative sum of a numbers digits. If the number is a negative number, nevertheless, compute the positive sum of the number's digits. The function has one parameter, the number, and returns an integer, the non-negative sum of the number's digits. Do not assume that the number has any particular size or upper or lower bounds on its size. Use a loop to copy, add, and remove the number's right most digits. What is the loop's stopping condition?

Part 2: replace the appropriate statements in the main function of the attached file with statements that invoke the function you wrote in Part 1.

You don't to write all of the function in Part 1 to write Part 2. You can write a function with only a return statements and nothing else.

use this function

#include <ctime>

#include <iostream>

#include <climits>

#include <random>

using namespace std;

int main()

{

const int max_int = 100000;

//default_random_engine e(time(0));

default_random_engine e;

uniform_int_distribution<int> u(1, max_int);

int this_number = u(e);

int original_this_number = this_number;

int this_sum = 0;

int this_digit = 0;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

this_digit = this_number % 10;

this_sum = this_sum + this_digit;

this_number = this_number / 10;

int that_number = u(e);

int original_that_number = that_number;

int that_sum = 0;

int that_digit = 0;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

that_digit = that_number % 10;

that_sum = that_sum + that_digit;

that_number = that_number / 10;

cout << original_this_number << '(' << this_sum << ") is ";

if (this_sum != that_sum)

cout << "not";

cout << " digit equal to ";

cout << original_that_number << '(' << that_sum << ')' << endl;

system("pause");

return 0;

}

Explanation / Answer

#include<iostream>
using namespace std;

int digitSum(int n)
{
// making -ve integer a positive one
if(n<0)
n = n*(-1);
int sum=0;
  
// going through each digit and adding it to sum
while (n != 0)
{
sum = sum + n % 10;
n = n / 10;
}
return sum;
}

int main()
{
// taking user input
int x;
cout << "Enter the number : ";
cin >> x;
  
// calling functino and printing output
cout << "The sum of the digits of "
<< x << " is " << digitSum(x);
}

/*SAMPLE OUTPUT

Enter the number : -1234
The sum of the digits of -1234 is 10

Enter the number : 2345
The sum of the digits of 2345 is 14

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote