fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int summa(int a){ //Нормальный способ с остатком от деления
  6. int x=0;
  7. while (a>0){
  8. x=x+a%10;//Добавляю к сумме остаток от деления на 10 (последнюю цифру числа)
  9. a=a/10; //Делю число на 10
  10. }
  11. return x;
  12. }
  13.  
  14.  
  15. int summa2(int a){//Вот тут "ручной" способ посчитать тот же самый остаток
  16. int x=0;
  17. while (a>0){
  18. x=x+a-(a/10)*10;
  19. a=a/10;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25.  
  26. int main() {
  27. cout<<summa(123)<<endl;
  28. cout<<summa(79813)<<endl;
  29. cout<<"Sposob s deleniem\n";
  30. cout<<summa2(123)<<endl;
  31. cout<<summa2(79813)<<endl;
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
6
28
Sposob s deleniem
6
28