-1

I would like to make my 2 ints get together in one number but where the biggest of the 2 get in the front and the other one in the back

I have a var named num1 and one named num2. The numbers of those 2 are getting assigned thru a random. I would like to make them into one number but with the highest number first and the other one after it. I don't want to + them together but make a big number.

For example num1 = 5 and num2 = 6 my whole number should then be 65.

Thx in advance I tried googling this but I could not really find what I was. Looking for sry for bad English

CubeCrafter360
  • 192
  • 1
  • 13

3 Answers3

4

Utilise Math.Max and Math.Min to find the largest and smallest of the two numbers, then concatenate and parse to an int.

int result = int.Parse(Math.Max(num1, num2) + ""+  Math.Min(num1, num2));

or if the number can get large after concatenation then use the long data type.

long result = long.Parse(Math.Max(num1, num2) + ""+  Math.Min(num1, num2));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Why do you suggest stringly typed code? – Alexei Levenkov Dec 29 '17 at 21:57
  • https://www.bing.com/search?q=stringly+typed+code ... You should not recommend anti-patterns even if OP demonstrated no efforts including no understanding of `if`... (and just in case if you don't think OP knows how to count digits - https://stackoverflow.com/questions/4483886/how-can-i-get-a-count-of-the-total-number-of-digits-in-a-number or how to deal with big integer - https://stackoverflow.com/questions/176775/big-integers-in-c-sharp) – Alexei Levenkov Dec 29 '17 at 22:08
3

It sounds like you wish to randomly generate the digits separately, then combine the digits to form a two-digit number. So:

var num1 = 5;
var num2 = 6;
var bigNumber = num1 + 10 * num2;  //65
John Wu
  • 50,556
  • 8
  • 44
  • 80
1

I found a solution i used a little of both of your answers This is what it ended like

        num1 = randomNum.Next(1, 7);
        num2 = randomNum.Next(1, 7);
        maxNum = Math.Max(num1, num2);
        minNum = Math.Min(num1, num2);
        wholeNum = minNum + 10 * maxNum;`
CubeCrafter360
  • 192
  • 1
  • 13