Newbie, please help me condense this function, or to write it in a better way

I am writing a program for a c++ class that will take in an integer value. display the most significant digit, least significant digit, smallest digit, largest digit, and display the sum of all the digits.

so far, I am trying to cin the integer to iVal then use the % and division to get each digit stored to its own variable for a number below 1,000,000,000. the max integer size on my console is 999,999,999. although, I would like not to set an upper limit for the value this way, but this is what I have so far.

should I be doing this in a different way? I not sure how I can complete all these operations if I don't follow this method.

much appreciated for the help!

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

int main() {

        iVal = a1 = b2 = c3 = d4 = e5 = f6 = g7 = h8 = i9 = 0;

        cout << "\nPlease enter an integer: ";  
        cin >> iVal;


        i9 = (iVal % 1000000000 / 100000000);
	h8 = (iVal % 100000000 / 10000000);
	g7 = (iVal % 10000000 / 1000000);
	f6 = (iVal % 1000000 / 100000);
	e5 = (iVal % 100000 / 10000);
	d4 = (iVal % 10000 / 1000);
	c3 = (iVal % 1000 / 100);
	b2 = (iVal % 100 / 10);
	a1 = (iVal % 10 );
	
	cout << iVal % 1000000000 / 100000000<< endl;
	cout << iVal % 100000000 / 10000000<< endl;
	cout << iVal % 10000000 / 1000000<< endl;
	cout << iVal % 1000000 / 100000<< endl;
	cout << iVal % 100000 / 10000<< endl;
	cout << iVal % 10000 / 1000<< endl;
	cout << iVal % 1000 / 100<< endl;
	cout << iVal % 100 / 10<< endl;
	cout << iVal % 10 / 1<< endl;

return 0;

}
32 bit integers won't hold more than 10 digits in them, no matter what you do. The best way would be to use an array? Then you would just arr[i] = iVal%10 and iVal = iVal/10 in a loop. Are you familiar with those?
I am somewhat familiar with what an array can do. how to work with it, not so familiar.

a classmate recommended this code, I guess i'll do it this way. what do you think?

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

/ Print out least significant digit & most significant digit

void printLeastMostSignificantDigit(int a_integer) {

     

     int tempInteger = a_integer, count = 0;

     

     // Determine the number of digits of the integer

     do {

         tempInteger = tempInteger / 10;

         count++;

     } while (tempInteger != 0);

     

     // Set the digits to positive if the integer is negative

     if(a_integer < 0)

         a_integer = -a_integer;

 

     // Dynamically allocating array to store all the digits

     int* digit = new int[count];

 

    // Find the remainder, then move 1 place to the left   

    for(int index = count - 1; index >= 0; index--) {

        digit[index] = a_integer % 10;

        a_integer = a_integer / 10;

    }

     

    // -1 because array start @ 0, ortherwise out of bound

    cout << "The least significant digit: " << digit[count - 1] << endl;

    cout << "The most significant digit: " << setw(2) << digit[0] << endl << endl;

     

    displaySmallestLargestSumDigits(digit, count);

 

    delete [] digit;

    digit = 0;

 

    return;

}
It's fine as long as you understand what it does.
Topic archived. No new replies allowed.