simple C++ program

I have a lab that takes user input for example user enters: 45341. The most significant/least significant digit needs to be displayed. 4 would be the most significant and 1 would be the least significant. The greatest/smallest number needs to be displayed. We are not allowed to use arrays. So far I am working on it piece by piece in main, then I want to create seperate functions that do the individual tasks. I am using mod and div to extract the digits. Here is what I have so far to get the biggest digit. I know there is a simpler way to do this. What I wrote only works for 5 digits but I want my program to work for n digits. Any help would be greatly appreciated. Here is what I have to get the biggest digit:

#include<iostream>

using namespace std;

int main(){

int x = 0;

cout << "enter an integer:";
cin >> x;

int var1 = x % 10;
int var2 = (x/10) % 10;
int var3 = (x/100) % 10;
int var4 = (x/1000) % 10;
int var5 = (x/10000) % 10;

if (var1 > var2 && var1 > var3 && var1 > var4 && var1 > var5)
cout << "The largest number is: " << var1 << endl;
else if( var2 > var1 && var2 > var3 && var2 > var4 && var2 > var5)
cout << "the greatest is: " << var2 << endl;
else if( var3 > var1 && var3 > var2 && var3>var4 && var3>var5)
cout << "the greatest is: " << var3 << endl;
else if( var4 > var1 && var4 > var2 && var4 > var3 && var4 > var5)
cout << "the largest is : " << var4 << endl;
else
cout << " the largest is: " << var5 << endl;


return 0;
}
are you allowed to use loops or the goto statement?
Last edited on
DEAR GOD. Do not ever use goto. That's criminal.
yes I could use loops
ok then check this out:

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
#include<iostream>
using namespace std;

int main()
{

    int x = 0;
    
    cout << "enter an integer:";
    cin >> x;
    
    int lsigd, msigd, mind, maxd, curd;
    //vars for least and most significant digits,
    //min and max digits, and current digit

    int pot=10; //pot -> Power Of Ten, LOL
    
    lsigd=msigd=mind=maxd=x%pot; //initialization of the vars to the first digit
    
    while (true)
    {
          if (x/pot==0) break; //if we reach the end of number stop
          
          curd=(x/pot)%10;
          
          if (curd<mind) mind=curd; //if current digit is less than min, it should be min
          if (curd>maxd) maxd=curd; //if current digit is greater than max, it should be max
          msigd=curd; //set most significant digit to current digit      
    
          pot*=10;
    }
    
    cout << "most significant digit: " << msigd << endl;
    cout << "least significant digit: " << lsigd << endl;
    cout << "smallest digit: " << mind << endl;
    cout << "greatest digit: " << maxd << endl;
    
    system("pause");
    return 0;
} 
Thank you m4ster r0shi. It worked out I can finish it from here...yea i'm sure "pot" means power of 10. Not like you use "pot" or anything! LOL
Topic archived. No new replies allowed.