39.4k views
5 votes
Make a program (C++). Sum of a 4 digits number and eliminating last digitsȘ

E.g: x=2347 -> 347+47+7=401

User Pranag
by
8.0k points

1 Answer

4 votes

Answer:

#include <iostream>

#include <string>

int GetEliminationSum(int number)

{

int sum = 0;

std::string s = std::to_string(number);

while (s.length() > 1)

{

s = s.substr(1);

sum += std::stoi(s);

}

return sum;

}

int main()

{

std::cout << "Enter your 4-digit number: ";

int number;

std::cin >> number;

std::cout << "The elimination sum is " << GetEliminationSum(number);

}

Step-by-step explanation:

User Gareth Luckett
by
8.2k points

Related questions

1 answer
3 votes
190k views
asked Nov 2, 2024 117k views
Tony Kh asked Nov 2, 2024
by Tony Kh
7.8k points
2 answers
5 votes
117k views
1 answer
1 vote
59.5k views