Q2. Write a program in JAVA to count the number of digits in a given number.

Example 1:Input: 654845 Output: The number of digits in the given number are 6

Example 2:Input: 6845 Output: The number of digits in the given number are 4


Solution: 
import java.util.Scanner;
public class CountingDigitsInInteger {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      int count = 0;
      System.out.println("Enter a number ::");
      int num = sc.nextInt();
      while(num!=0){
         num = num/10;
         count++;
      }
      System.out.println("Number of digits in the entered integer are :: "+count);
   }
}

Comments

Popular posts from this blog

B.Tech. Computer Science and Engineering Notes - Semester 8

Gate Exam Solutions