1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;
int digitsum(int& value, int& sum);
int main()
{
int value, digits, sum;
cout<< "enter value here: "<< endl;
cin>> value;
if (value == 0)
{return 0;}
digitsum (value, sum);
digits=floor(log10(value))+1;
cout<< " the value entered is "<< value << " it has " << digits << " digits "<< " with the digit sum of " << sum << endl;
system ("pause");
return 0;
}
int digitsum(int& value, int& sum)
{
int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
num1 = value / 1000000000;
num2 = value % 1000000000 / 100000000;
num3 = value % 1000000000 % 100000000 / 10000000;
num4 = value % 1000000000 % 100000000 % 10000000 / 1000000;
num5 = value % 1000000000 % 100000000 % 10000000 % 1000000 / 100000;
num6 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 / 10000;
num7 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 / 1000;
num8 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 / 100;
num9 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 / 10;
num10 = value % 1000000000 % 100000000 % 10000000 % 1000000 % 100000 % 10000 % 1000 % 100 % 10 /1;
sum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;
return (sum);
}
|