Hey guys, writing a code is not a tough if you know the logic and you can write it at your own in any computer language that you know. so first we figure out the logic of getting digits of a number.

suppose we have a digit= 234. lets figure out it as;

digit =234/10 =23      (1 digit)

digit = 23/10 =2          (2 digit)

digit = 2/10 =0            (3 digit)

now at last we get zero (0). it goes until we get quotient as zero. then count the number of steps, here are 3 steps so number of digit s is here 3.

 

Program :

import java.util.*;
public class Main
{
    static int number=0;
    static Scanner scan =new Scanner(System.in);
 
    public static void main(String[] args) {
       System.out.println("Please enter a digit");
       number = scan.nextInt();
       
       int count = 0;
       while(number > 0){
          number = number/10;
          count++;
       }
 
      System.out.println("Number of digits in number is " + count);
   }
 
}

Similar posts: Write a program to find digits in a number.

Leave a Reply

Your email address will not be published. Required fields are marked *