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

Write a process for summing the digits in the odd places (working from right to

ID: 3680748 • Letter: W

Question

Write a process for summing the digits in the odd places (working from right to left) of an integer number. For example given the number 72391, 1 is in the first place, 3 is in the third place, and 7 is in fifth place so the sum would be 11. Given the number 5421, 1 is in the first place and 4 is in the third place so the sum would be 5. You may assume the integer number is less than 1000000.

NOTE: *NO* code needs to be written for this. You just need to explain how this would be done in C++. The problem should be restated, specify what input is needed, what output is expected, and the step-by-step process to get the output from the input.

Explanation / Answer

Process:
Since the input number is less than 1000000,
we can take data type of number as Integer.
Step1 Declare number with data type integer
Step2 Take input from the user
Step3   Declare a vector which will hold the digits of the number
Step4    Declare a variable temp which will hold the digits
of the number one at a time
Step5   Create a While loop which will run till number is not equal to 0
temp will hold number % 10 i.e the last digit in number
push the number in the vector
Divide the number by 10
Run the while loop till number is not equal to zero.

Here the vector will store the digits from units place
i.e in reverse order and the index will start from zero.

Step6   Declare variable sum = 0 which will hold the
sum of digits at even place
Step7 Now run a for from index 0 to size of vector -1
since we started from index zero, the required
digits will be at even places in our vector
This for loop will sum the digits present in
even index.
Step8   Print the sum

/** C++ program demonstrating the above process ***/

#include <iostream>
#include <vector>

using namespace std;

int main()
{
int number;
cin >> number;

vector<int> v;

int temp;

while(number != 0)
{
temp = number%10;
v.push_back(temp);
number = number/10;
}

int sum = 0;

for (int i = 0; i < v.size(); ++i)
{
if(i % 2 == 0){ sum = sum + v[i]; cout<< v[i]<< endl;}
  
}

cout << sum;
return 0;
}

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