vlatkovski

Zbir na cifri

Aug 8th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. //you could do this with only math and no strings, but why should you when this is simpler?
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. int main() {
  7.     std::string N;
  8.     std::cin >> N;
  9.  
  10.     int brCif = N.length();
  11.  
  12.     int suma = 0;
  13.  
  14.     for (int i = 0; i < brCif; i++) {
  15.         char x = N.at(i);
  16.         int ix = x - '0'; //char --> int
  17.         suma += ix;
  18.     }
  19.  
  20.     std::cout << suma << std::endl;
  21.  
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment