Advertisement
BojidarDosev

insert a number abc, receive output a+b+c;

Nov 12th, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. // funtions.cpp.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. int sum(int num)
  7. {
  8.     int sum = 0;
  9.     int num2 = 0;
  10.     while (num > 0)
  11.     {
  12.         for (int i = 0; i < num; i++)
  13.         {
  14.             num2 = num % 10;
  15.             sum = sum + num2;
  16.             num = num / 10;
  17.         }
  18.     }
  19.     return sum;
  20. }
  21.  
  22. int main()
  23. {
  24.     int num;
  25.     cout << "Enter a number: \n";
  26.     cin>>num;
  27.     cout<<sum(num);
  28. }
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement