Sunday, January 26, 2014

Extracting The First Digit Of Any Number Using BlueJ Program




In this program user will enter any number and the first digit of the number is to be extracted.
Suppose the number is 12343 and the output will be 1.

import java.io.*;

class Num
{
  int number,i,p,len;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void takeNumber()throws Exception
 {
  System.out.println("Enter the number:");
  number=Integer.parseInt(br.readLine());
  p=number;
  i=0;
  while(p>0)
  {
  i++;
  p=p/10;
  }
  p=(int)Math.pow(10,i-1);
  p=number/p;
  System.out.println("First digit of the number is="+p);
  }
  public static void main(String args[])throws Exception
  {
   Num object=new Num();
   object.takeNumber();
   }
   }


Related PostBlueJ Programs on Number

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner