In: Computer Science
IN C++ LANGUAGE PLEASE:::::
All Hail Modulus Agustu! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design (pseudocode) and implement (source code)a program to sum the total of all digits in an input integer number between 0 and 1000, inclusive. Notice that you need to extract individual digits from the input number using the remainder (modulus) and division mathematical operators. For example, if the input number is 123, the sum of its digits is 6.
pseudocode
Input a Number between 0 and 1000 inclusive
Initialize Sum to zero
While Number is not zero
Get Remainder by Number Mod 10
Add Remainder to Sum
Divide Number by 10
Print sum
code
#include<iostream>
using namespace std;
int getSum(int n)
{
int sum;
/* Single line that calculates sum */
for (sum = 0; n > 0; sum += n % 10, n /= 10);
return sum;
}
int main()
{
int number;
cout<<"Enter the number between 0 and 1000
inclusive: ";
cin>>number;
cout<<"\nThe sum of its digits is
"<<getSum(number)<<endl;
}
outputs
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.