~riderius/cpp-code

cpp-code/digits_analysis.cpp -rw-r--r-- 1.4 KiB
380d65aariderius digits_analysis.cpp: argv[1] can replace n 11 days ago
                                                                                
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <climits>
#include <cstdlib>
#include <iostream>

int SumOfDigits(int n);
int NumberOfZeroes(int n);
int MinDigit(int n);
int MaxDigit(int n);
int reverse(int n);

int main(int argc, char* argv[])
{
    using namespace std;

    int n;
    if(argc > 1)
        n = atoi(argv[1]);
    else {
        cout << "Enter a number: ";
        cin >> n;
        cout << endl;
    }
    cout << "Sum Of Digits: " << SumOfDigits(n) << endl;
    cout << "Number Of Zeroes: " << NumberOfZeroes(n) << endl;
    cout << "Min Digit: " << MinDigit(n) << endl;
    cout << "Max Digit: " << MaxDigit(n) << endl;
    cout << "Reverse: " << reverse(n) << endl;

    return 0;
}

int NumberOfZeroes(int n)
{
    int result = 0;
    while (n != 0) {
        if (n % 10 == 0) {
            result++;
        }
        n /= 10;
    }
    return result;
}

int SumOfDigits(int n)
{
    int result = 0;
    while (n != 0) {
        result += n % 10;
        n /= 10;
    }
    return result;
}

int MinDigit(int n)
{
    int min = INT_MAX;
    while (n != 0) {
        if (n % 10 < min) {
            min = n % 10;
        }
        n /= 10;
    }
    return min;
}

int MaxDigit(int n)
{
    int max = INT_MIN;
    while (n != 0) {
        if (n % 10 > max) {
            max = n % 10;
        }
        n /= 10;
    }
    return max;
}

int reverse(int n)
{
    int result = 0;
    while (n != 0) {
        result = result * 10 + n % 10;
        n /= 10;
    }
    return result;
}