0
#include <iostream>

using namespace std;

int main()
{
    long long number, temp, new_temp;
    long long sum = 0;

    std::cout << "please enter a number" << std::endl;
    std::cin >> number;
    temp = number;

    while(temp>0)
    {
        new_temp = temp%10;
        sum = sum + new_temp;
        temp = temp/10;

    }

    cout<<sum;
    return 0;
}

Output: please enter a number
11111111111111111111 //the range of long long exceeeded
88 // What it represent

  • This is a good question (have an upvote). The duplicate answers it well (and the accepted answer is a good one; the heavily downvoted answer - which I've further downvoted - is hogwash). – Bathsheba Apr 07 '20 at 06:58
  • If a `long long` (or any standard signed integral type) is overflowed, then the behaviour is undefined. The result you get is therefore meaningless. It might be meaningful to your particular compiler, it it might not. – Peter Apr 07 '20 at 06:58
  • Though I have understood the basic concept of this problem, but I didn't get my desired answer. The Output 88 is common for all value. for int, it gives 46 I want to know why it is giving only specific value. – Sandeep Chaurasiya Apr 07 '20 at 10:29

0 Answers0