Ankit Sahu
Find Second largest digit in given number without using any collection like array list
By Ankit Sahu in C# on Nov 14 2017
  • Laxmidhar Sahoo
    Nov, 2017 27

    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();}} }

    • 3
  • Gajendra Jangid
    Jan, 2018 22

    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());

    • 2
  • Sourabh Panchal
    Jan, 2018 18

    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(); }

    • 2
  • Sourabh Panchal
    Jan, 2018 18

    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();}

    • 2
  • Ankit Sahu
    Nov, 2017 14

    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

    • 2
  • Дмитрий Михолап
    Jan, 2018 22

    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);

    • 1
  • Gajendra Jangid
    Jan, 2018 22

    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());}}

    • 1
  • Sourabh Panchal
    Jan, 2018 18

    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();}

    • 1
  • Sourabh Panchal
    Jan, 2018 18

    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();}

    • 1
  • Sumit Khandelwal
    Jan, 2018 9

    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();}}

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS