Sunday, July 12, 2020

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)

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 :

middle-digit-of-a-number


Write a program to input a number and check whether it is Special two digit number or not?(59, 5+9=14, 5*9=45, sum=14+45=59)



No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj