Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Iterative Constructs in Java

Chapter 9

Iterative Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Remove All Zeros from a Number


17. Write a program in Java to read a number, remove all zeros from it, and display the new number. For example,

Sample Input: 45407703
Sample Output: 454773

import java.util.Scanner;

 

public class RemoveZeros {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter a number: ");

        long num = sc.nextLong();

 

        long temp = num;

        long result = 0;

        long place = 1;

 

        while (temp > 0) {

            long digit = temp % 10;

 

            if (digit != 0) {

                result = result + digit * place;

                place = place * 10;

            }

 

            temp = temp / 10;

        }

 

        System.out.println("Number after removing zeros: " + result);

    }

}

Output

Sample Input
45407703
Sample Output
Number after removing zeros: 454773

📝 Explanation

  • Extract digits one by one using % 10
  • Ignore the digit if it is 0
  • Rebuild the number using positional value (place)
  • / 10 removes the last digit in each loop iteration

Key Logic

If digit ≠ 0 → keep it

If digit = 0 → discard it