#include <iostream> #include <string> using namespace std; /* 375 / 10 = 37; 375 % 10 = 5 ; So here you are separating one number in one loop from the main number */ int main() { int countDigits = 0, countSum = 0; int num,currDigit; cout << "Enter any number" << endl; cin >> num; while (num>0) { currDigit = (num % 10); countSum += currDigit; countDigits++; num = num / 10; } cout << "The sum of digits is: " << countSum << " and the count of digits is: " << countDigits << endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter