Friday, 17 June 2016

Program to find the Second Maximum digit in a number without using Arrays.

Explanation :
1) If n=12 then output will be 1.
2) If n=71233568 then output will be 7.

Code:

import java.util.Scanner;
public class SecondMax
{
public static void main(String args[])
{
    Scanner sc=new Scanner(System.in);
    int max = 0,r=0;
    int secondmax = 0;
    System.out.println("Enter any number :");
    int n = sc.nextInt();
    int num=n;
     
    while(num % 10 != 0)
    {
        r = num % 10;
        if(r > max)
        {
            secondmax=max;
            max = r;
        }
        else if(r<max && r>secondmax)
        {
            secondmax=r;
        }
       
        num /= 10;
    }
    System.out.println("Maximum is :"+max);
    System.out.print("Second Maximum digit in "+n+" is :"+secondmax);
    sc.close();
}
}

No comments:

Post a Comment