Answer:
#include<iostream>
using namespace std;
int digits(int number){
int sum=0;
while(number != 0){
int rem = number % 10;
sum = sum + rem;
number = number/10;
}
return sum;
}
int main(){
int number;
cout<<"Enter the integer: ";
cin>>number;
int result = digits(number);
cout<<"The sum of digits is: "<<result<<endl;
}
Step-by-step explanation:
Include the library iostream for use of input/output.
Then, create the function digits with return type int.
Take a while loop and put a condition number not equal to zero.
the while loop executes the until the condition is not false.
In the while loop, take the remainder of the number and store in the rem variable.
after that, the store in the sum then reduces the number by dividing 10.
for example:
the number is 123.
rem = 123%10 it gives the value 3.
sum = 0+3=3
number = 123/10 it equal to 12
then,
rem = 12%10 it gives the value 2.
sum = 3+2=5
number = 12/10 is equal to 1
the same process executes until the number becomes zero and then the while loop terminates and the sum value returns to the main function.
create the main function and take the value from the user and call the function with the argument number.
and finally, print the result.