Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp5
- {
- class Program
- {
- static void Main(string[] args)
- {
- // a number from user
- int n;
- // 10 counters (0 to 9) for each digit
- int[] count = new int[10];
- // get a number from user
- Console.Write("Enter a positive integer number: ");
- n = int.Parse(Console.ReadLine());
- // loop for each digit in the number n
- while (n > 0)
- {
- int ahad = n % 10; // first digit only
- n /= 10; // remove first digit from the number
- // update counter for the first digit found
- count[ahad]++;
- }
- // print digits and counters
- for (int i = 0; i < 10; i++)
- {
- // if digit exist in the number
- if (count[i] > 0)
- {
- Console.WriteLine("{0} x {1}", i, count[i]);
- }
- }
- // build max number with the same digits
- string max = "";
- // loop for each digit, start from 9 down to 0
- for (int i = 9; i >= 0; i--)
- {
- // loop to add digit to max
- for (int c = 0; c < count[i]; c++)
- {
- max += i;
- }
- }
- Console.WriteLine("max number with the same digits is {0}", max);
- }
- }
- }
Add Comment
Please, Sign In to add comment