10
Reply

Find Second largest digit in given number without using any collection like array list

Ankit Sahu

Ankit Sahu

7y
3.1k
3
Reply

    using System; namespace ConsoleApplication1 { class Program{ static void Main(string[] args) {int num,k=0; int a = 0, b = 0, tmp = 0;num = 975846; int[] arr = new int[6];while (num > 0) { tmp = num % 10;num = num / 10;arr[k] = tmp;k++;}for (int i = 0; i < arr.Length; i++){for (int j = 0; j < arr.Length - i - 1; j++){if (arr[j] < arr[j + 1]){int t = arr[j];arr[j] = arr[j + 1];arr[j + 1] = t;}}}Console.WriteLine("Second largest number is b={0}", arr[1]);Console.ReadKey();}} }

    this is perfectly work for any number-- if number is single digit or more then one digit number ==> static void Main(string[] args) { Console.WriteLine("Enter number: "); int num = Convert.ToInt32(Console.ReadLine()); int FirstHigest = 0, SecondHigest = 0, temp = 0; if (num >= 10) { while (num > 0) { temp = num % 10; num = num / 10; if (temp > FirstHigest) { SecondHigest = FirstHigest; FirstHigest = temp; } else if (temp > SecondHigest && temp != FirstHigest) { SecondHigest = temp; } } } else { SecondHigest = num; } Console.WriteLine("Second highest digit is: "+ SecondHigest); Console.ReadKey(); }

    static void Main(string[] args){Console.WriteLine("Enter number: ");int num = Convert.ToInt32(Console.ReadLine());int FirstHigest = 0, SecondHigest = 0, temp = 0;while (num>0){temp = num % 10;num = num / 10;if (temp > FirstHigest){SecondHigest = FirstHigest;FirstHigest = temp;}else if (temp > SecondHigest){SecondHigest = temp;}}Console.WriteLine("Second highest digit is: "+SecondHigest);Console.ReadKey();}

    Find second largest number For example I have any number like 975846 so second largest digit is 8 Please find bellow C# program. using System;namespace ConsoleApplication1 {class Program{static void Main(string[] args){int num;int a = 0, b = 0, tmp = 0;num = 975846;a = num % 10;b = num / 10 % 10;while (num > 0){tmp = num % 10;if (a < tmp){b = a;a = tmp;}else if (a != b && b < tmp){b = tmp;}num = num / 10;}Console.WriteLine("Second largest number is b={0}", b);Console.ReadKey();}} }Output is 8

    string a = "517378";int max = 0;while (a.ToString().Length > 1){max = Convert.ToInt32(a.Substring(0, 1));a = a.Substring(1, a.ToString().Length - 1);if (max > Convert.ToInt32(a.Substring(0, 1))){a = a.Remove(0, 1).Insert(0, max.ToString());}}MessageBox.Show("Second Max Num is : "+max.ToString());

    string a = "517378";int max = 0;while (a.ToString().Length > 1){max = Convert.ToInt32(a.Substring(0, 1));a = a.Substring(1, a.ToString().Length - 1);if (max > Convert.ToInt32(a.Substring(0, 1))){a = a.Remove(0, 1).Insert(0, max.ToString());}}

    static void Main(string[] args){Console.WriteLine("Enter number: ");int num = Convert.ToInt32(Console.ReadLine());int FirstHigest = 0, SecondHigest = 0, temp = 0;while (num>0){temp = num % 10;num = num / 10;if (temp > FirstHigest){SecondHigest = FirstHigest;FirstHigest = temp;}else if (temp > SecondHigest){SecondHigest = temp;}}Console.WriteLine("Second highest digit is: "+SecondHigest);Console.ReadKey();}

    static void Main(string[] args){Console.WriteLine("Enter number: ");int num = Convert.ToInt32(Console.ReadLine());int FirstHigest = 0, SecondHigest = 0, temp = 0;while (num>0){temp = num % 10;num = num / 10;if (temp > FirstHigest){SecondHigest = FirstHigest;FirstHigest = temp;}else if (temp > SecondHigest){SecondHigest = temp;}}Console.WriteLine("Second highest digit is: "+SecondHigest);Console.ReadKey();}

    static void Main(string[] args){int num;int h = 0, sh = 0, tmp = 0;num = 1415711;h = num % 10;sh = num / 10 % 10;if (sh > h){h = h + sh;sh = h - sh;h = h - sh;}while (num>0){int x = num % 10;if (x > h){sh = h;h = x;}else if (x > sh && x < h){sh = x;}num = num / 10;}Console.WriteLine("Second largest number is b={0}", sh);Console.ReadKey();}}

    long number = 8811191;short max = -1;short maxSecond = -1;while (number > 0){byte digit = (byte)(number % 10);number /= 10;if (maxSecond < digit){if (max <= digit){maxSecond = max;max = digit;}else{maxSecond = digit;}}}Console.WriteLine(maxSecond);