i want all the task(add, find the average and reverse the digit of the inputed number) will be in 1 function and the body of the function have no cout or cin. How to make it?
#include<iostream>
#include<stdlib.h>
usingnamespace std;
int addition(int);
double average(int);
int reverse(int);
int main()
{
int val, count=0;
cout << "Enter a number that has minimum of 4 digits and maximum of 9: \n";
while(!(cin >> val) || val<=1000 || val>1000000000)
{
cout << "The value you inputed is invalid! Please try again."<<endl;
system("PAUSE");
system("CLS");
cin.clear();
cin.ignore(10000, '\n');
cout << "Enter a number : \n";
}
int sum= addition(val);
double avg= average(val);
cout <<endl<< "Sum= " << sum<<endl;
cout <<endl<< "Average= "<<avg<<endl;
cout <<endl<< "The reverse of the inputed numbers are: "<<endl;
cout <<endl<<reverse(val)<<endl;
}
int addition(int v)
{
int num=v, sum=0, count=0;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
count++;
}
return sum;
}
double average(int v)
{
int num=v, sum=0, count=0;
while (num > 0)
{
sum = sum + num % 10;
num = num / 10;
count++;
}
double avg= (double)sum/(double)count;
return avg;
}
int reverse(int v)
{
int n=v, reversedNumber = 0, remainder;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return reversedNumber;
}
Enter a number that has minimum of 4 digits and maximum of 9:
1234
Sum= 10
Average= 2.5
The reverse of the inputed numbers are:
4321