fork download
  1. #include<iostream>
  2.  
  3. int main()
  4. {
  5. double n;
  6. std::cout << "Enter a positive real number: ";
  7. do
  8. {
  9. std::cin >> n;
  10. if(n <= 0) std::cout<<"The number must be positive, enter again: ";
  11. }while(n <= 0);
  12.  
  13. n *= 10000;
  14. int Integer = n;
  15.  
  16. int i = 4;
  17. int sum = 0;
  18. while(i--)
  19. {
  20. sum += (Integer%10);
  21. Integer /= 10;
  22. }
  23. std::cout << "The sum of the first 4 digits is: " << sum;
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 4436KB
stdin
5.1010
stdout
Enter a positive real number: The sum of the first 4 digits is: 2