47) Write a program to input a number and
find & print the Middle Digit of
the number, if does not exist find & print the average of middle digits?
(33125==1, 8974==9+7=16/2=8)
import java.util.*;public class Middle_digit
{
public static void main(String args[ ])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number");
int a = sc.nextInt();
int k = a;
int c = 0;
while(k>0)
{
c++;
k=k/10;
}
if(c%2==0)
{
int x = (int)Math.pow(10,((c/2)-1));
int t = a/x;
int m = t%10;
t=t/10;
int n = t%10;
System.out.println("Avereage of middle digits of a number = "+(m+n)/2.0);
}
else
{
int x = (int)Math.pow(10,(c/2));
int t = a/x;
System.out.println("Middle digit of a number = "+t%10);
}
}
}
Output :
|
middle-digit-of-a-number
|
Output :