add, average and reverse in 1 function but no cout in the body of the function

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<stdlib.h>
using namespace 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

Last edited on
Please, do not doublepost. The other thread: http://www.cplusplus.com/forum/beginner/230642/
Topic archived. No new replies allowed.