One real number is entered, which does not exceed 105105, it is specified with an accuracy of four decimal places. Output format

Print an integer - the desired amount.

Examples

input data
123.4567

output 15

input data
42.4242

output 10

PS The solution should not include neither strings, nor arrays, nor cycles, nor conditional statements. You can use the header files <iomanip> and <cmath>

Closed due to the fact that the essence of the question is unclear by the participants of pavel , aleksandr barakin , HamSter , fori1ton , Alexey Shimansky 13 Nov '16 at 7:44 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    what are the problems? - Grundy
  • I do not know how to find the numbers after the comma - Skufler
  • multiplying the original number by 1000 and then taking the remainder of the division by 1000 from the result, we get a number of three digits of the fractional part of the original number - Grundy
  • @Matviei Skufin How did you get the output data for this number 123.4567 15? :) - Vlad from Moscow
  • @VladfromMoscow, 4 + 5 + 6 = 15, and in the title: find the sum of the first three digits of the fractional part of a real number - Grundy

2 answers 2

I do not know where is the condition

One real number is entered which does not exceed 105105

that is, the limitation on the absolute value, since it does not play any role.

Without checking this condition (which you yourself can insert into the program), the program may, for example, look like this

 #include <iostream> #include <cmath> int main() { while (true) { const int Base = 10; const int N = 3; std::cout << "Enter a float number with " "a fraction of at least " << N + 1 << " digits (0 - exit ): "; double d; if (!(std::cin >> d) || d == 0) break; double int_part; double fraction; fraction = std::modf(std::fabs( d ), &int_part); int sum = int(fraction * Base) % Base + int(fraction * Base * Base) % Base + int(fraction * Base * Base * Base) % Base; std::cout << "The sum of " << N << " digits of the fraction is " << sum << std::endl; } } 

Indeed, if you consistently enter the values ​​123.4567 and 42.4242, the output to the console will look as intended in your task

 Enter a float number with a fraction of at least 4 digits (0 - exit ): 123.4567 The sum of 3 digits of the fraction is 15 Enter a float number with a fraction of at least 4 digits (0 - exit ): 42.4242 The sum of 3 digits of the fraction is 10 Enter a float number with a fraction of at least 4 digits (0 - exit ): 0 

Instead of the modf function from <cmath> you could just write

 double fraction = std::fabs( d ) - int( std::fabs( d ) ); 

to remove the integer part of the number.

  • This is a good idea, but not necessarily modf () to use, given the input range, an example is jfs
  • @jfs I have written at the end of the answer how to replace the function call. :) - Vlad from Moscow
  • And deduct is also not necessary. Click on the link in my comments. - jfs
  • @jfs With subtraction no limit on the size of the entered number is required. - Vlad from Moscow
  • 1- There is a limitation in the question. 2- the subtraction option will break if there is no limit. - jfs

http://ideone.com/Ti9lse

 #include <cstdio> int main() { char a='0', b='0', c='0'; scanf("%*d.%c%c%c", &a, &b, &c); printf("%d\n", a+b+c-'0'*3); return 0; }