i have a program that i need to write that requires the following output:
"the entered value is "23557" it has 5 digits with the digit sum of 22.
the number of 5's in the value is 2."
i need help adding the digits together and telling the program to count the number of how many digits there are. i've been told to use the modulus operator but i just cant figure out how to execute this.
#include <iostream>
#include <cmath>
long valueinput(long);
int main()
{
usingnamespace std;
long value, digits;
cout<< "enter value here: "<< endl;
cin>> value;
long givenvalue = valueinput(value);
digits=floor(log10(value))+1;
cout<< " the value entered is "<< value << " it has " << digits << " digits "<< " with the digit sum of " << givenvalue << endl;
system ("pause");
return 0;
}
long valueinput(long digits)
{
return ;
}
when you do the modulus %, it will give you remainder.
using %:
in regular math, 23557/10 = 2355 remainder 7,
so that 23557% 10 = 7, then divide 23557/10 = 2355. (in C++, integer division will remove decimal).
then:
2355%10 = 5, then 2355/10 = 235. and so on, you will get all the digits.
set sum to 0, each time when you do modulus, you add the remainder to the sum.