Advertisement
cska1312

Enter n and take the sum of the digits.If the sum is > 9 ,add them until you get one digit.

Jan 13th, 2023 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   int a,b,d;
  7.   int sum = 0;
  8.   int sum1 = 0;
  9.   int c = 0;
  10.   int number;
  11.   cin >> number;
  12.   while(number > 0)
  13.   {
  14.         a = number % 10;
  15.         sum = sum + a;
  16.         number = number / 10;
  17.     }
  18.   cout << sum << endl;
  19.   if(sum > 9)
  20.   {
  21.   while(sum > 0)
  22.   {
  23.    b = sum % 10;
  24.    c = c + b;
  25.     sum = sum / 10;
  26.   }
  27.     }else{
  28.     return 0;
  29.     }
  30.   cout << c << endl;
  31.   if(c > 9)
  32.   {
  33.     while(c > 0)
  34.     {
  35.       d = c % 10;
  36.       sum1 = sum1 + d;
  37.       c = c / 10;
  38.     }
  39.   }else{
  40.     return 0;
  41.   }
  42.   cout << sum1 << endl;
  43. }
  44.  
  45.  
  46. /*При дадено n вземете сумата от цифрите на n. Ако тази стойност има повече от една цифра, продължете да намалявате по този начин, докато получите едноцифрено число. Входът ще бъде неотрицателно цяло число.
  47. 16  -->  1 + 6 = 7
  48. 942  -->  9 + 4 + 2 = 15  -->  1 + 5 = 6
  49. 132189  -->  1 + 3 + 2 + 1 + 8 + 9 = 24  -->  2 + 4 = 6
  50. 493193  -->  4 + 9 + 3 + 1 + 9 + 3 = 29  -->  2 + 9 = 11  -->  1 + 1 = 2
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement