Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
  • Contact Us

Count number of digits in an integer using while loop

We can count the number of digits in a given number in many ways using java.

One of the approach is that, we shall take the number, remove the last digit, and increment our counter for number of digits in the number. We can continue this, until there is no digit in the number.

Another approach is that, we can convert the given number into a string, and use the method string.length() to count the number of character in this string. This results in the number of digits in given number.

Here, while the loop is iterated until the test expression number != 0 is evaluated to 0(false).

After the first iteration, number will be divided by 10 and its value will be 345. Then, the count is incremented to 1.

After the second iteration, the value of number will be 34 and the count is increased to 2.

After the third iteration, the value of number will be 3 and the count is increased to 3.

After the fourth iteration, the value of number will be 0 and the count is increased to 4.

Then the test expression is evaluated to false and the loop terminates.

  • Program:

public class Main {

  public static void main(String[] args) {

    int count = 0, number = 0003452;

    while (number != 0) {
      // number = number/10
      number /= 10;
      ++count;
    }

    System.out.println("Number of digits: " + count);
  }
}

  • Output:

Number of digits: 4

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here
Java program to generate multiplication table Here
Write a java program to display Fibonacci series Here
Write a java program to find GCD of two numbers Here
Write a java program to find LCM of two numbers Here
Java program to display alphabets (A to Z) using loop Here

Write a java program to display alphabets (A to Z) using loop

In this program, we can loop through A to Z using for loop because they are stored as ASCII characters in java.

This java program iterates the ASCII code from 65 to 90, representing the alphabets A to Z and print them.

With a little modification you can display lowercase alphabets to simply replace the A with a and Z with z. In this case, internally your loop through 97 to 122.

  • Program:

class Main {
  public static void main(String[] args) {

    char n;

    for(n = 'A'; n <= 'Z'; ++n) // For lowercase: for(n = 'a'; n <= 'b'; ++n)
      System.out.print(n + " ");
    }
}

  • Output:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here
Java program to generate multiplication table Here
Write a java program to display Fibonacci series Here
Write a java program to find GCD of two numbers Here
Write a java program to find LCM of two numbers Here

Java program to find LCM of two numbers

In this program, LCM(Least common multiple) of two or more numbers is the least positive number that can be divided by both the numbers, without leaving the remainder. It is also known as Lowest common multiple, Least Common Denominator and smallest common multiple. It is denoted by LCM (a, b) or lcm (a, b) where a and b are two integers.

It is used when we add, subtract, or compare the fractions. While we perform addition or subtraction of the fractions, we find the LCM of the denominators and then solve the fraction. The LCM of the denominator is known as the Least Common Denominator (LCD).

  • Program:

public class Main {
    public static void main(String[] args) {

        int a = 72, b = 120, lcm;

        // maximum number between a and b is stored in lcm
        lcm = (a > b) ? a : b;
        while(true) {
            if( lcm % a == 0 && lcm % b == 0 ) {
                System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);
                break;
            }
            ++lcm;
        }
    }
}

  • Output:

The LCM of 72 and 120 is 360.

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here
Java program to generate multiplication table Here
Write a java program to display Fibonacci series Here
Write a java program to find GCD of two numbers Here

In this program, we have covered different logic in java programs to find GCD of two numbers.

We have initialized two numbers n1=11 and n2=102. After that, we have used a for loop that runs from 1 to the smallest of both numbers. It executes until the condition i<=n1 && i<=n2 returns true. Inside the for loop, we have also used if statement that tests the condition (n1%i== && n2%i==0) and returns true if both conditions are satisfied. At last, we have stored the value of i in the variable gcd and print the same gcd variable.

  • Program:

class Main {
  public static void main(String[] args) {
    
    int n1 = 11, n2 = 102; // find GCD between n1 and n2
   
    int gcd = 1; // initially set to gcd

    for (int i = 1; i <= n1 && i <= n2; ++i) {
   
      if (n1 % i == 0 && n2 % i == 0) // check if i perfectly divides both n1 and n2
        gcd = i;
    }

    System.out.println("GCD of " + n1 +" and " + n2 + " is " + gcd);
  }
}

  • Output:

GCD of 11 and 102 is 1

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
Java program to check whether a character is alphabet or not Here
Java program to calculate the sum of natural numbers Here
Java program to find factorial of a number Here
Java program to generate multiplication table Here
Write a java program to display Fibonacci series Here
Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
  • Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 )
  • Write a C program to interchange two numbers
  • Write a program to that performs as calculator (Addition, Multiplication, Division, Subtraction)
  • Write a C program to find out the Maximum and Minimum number from given 10 numbers

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • February 20241
  • January 20242
  • November 20234
  • October 20236
  • September 20236
  • August 20235
  • April 20231
  • March 20231
  • October 20221
  • August 20223
  • July 20222
  • May 20223
  • April 20222
  • March 20221
  • January 20221
  • December 20216
  • November 20214
  • October 20211
  • September 20216
  • August 202127
  • July 20216

Report Abuse

Copyright

  • Home
  • About us
  • Privacy Policy
  • Disclaimer
  • Contact Us

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes