KnowledgeBoat Logo

Computer Applications

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

Java

Java Iterative Stmts

3 Likes

Answer

import java.util.Scanner;

public class KboatRemoveZeros
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number: ");
        int num = in.nextInt();
        int idx = 0;
        int newNum = 0;
        int n = num;
        
        while (n != 0) {
            int d = n % 10;
            if (d != 0) {
                newNum += (int)(d * Math.pow(10, idx));
                idx++;
            }
            n /= 10;
        }
        
        System.out.println("Original number = " + num);
        System.out.println("New number = " + newNum);
    }
}

Output

BlueJ output of 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

Answered By

2 Likes


Related Questions