Remove all zeroes from a given number

Given an integer number N, the task is to remove all zeroes from the given number.

Example:

Input: N = 230404
Output: 2344

Input: N = 1004
Output: 14

Approach

First we count the number of digits of N and then create the array of size of number of digits present in the N , then add all the digits in the array from the backwards and then we traverse the array and then create a new number and if the arr[i] is not equal to 0 so we add the number in the 10th position.

Step-by-step approach:

  • First, find the number of digits of given number N
  • Create a empty array of size N
  • Find the last digit of a number using modulo operator and module the number by 10 for example 12%10 = 2;
  • Remove the last digit of a number by 10 until it become 0
  • Traverse the array of arr[i] is not equal to zero so we add the number in the 10th position.

Below is the implementation of the above approach:

C++
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main() {
    int N = 50505; // given Number
    int ans = 0;
    // log function to find the number of digit in
    // constant time
    int length_of_N = log10(N) + 1;

    // storing the values for every digit in the number
    // N
    vector<int> arr(length_of_N);

    for (int i = arr.size() - 1; i >= 0; i--) {
        arr[i] = N % 10;

        N /= 10;
    }

    for (int i = 0; i < arr.size(); i++) {

        if (arr[i] != 0) {
            ans *= 10;
            ans += arr[i];
        }
    }
    // showing the output on the console
    cout << ans << endl;

    return 0;
}
Java
import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int N = 50505; // given Number
        int ans = 0;
        // log function to find the number of digit in
        // constant time
        int length_of_N = (int)Math.log10(N) + 1;

        // storing the values for every digit in the number
        // N
        int[] arr = new int[length_of_N];

        for (int i = arr.length - 1; i >= 0; i--) {
            arr[i] = N % 10;

            N /= 10;
        }

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] != 0) {
                ans *= 10;
                ans += arr[i];
            }
        }
        // showing the output on the console
        System.out.println(ans);
    }
}
Python
import math

# Given number
N = 50505
ans = 0

# Using log function to find the number of digits in constant time
length_of_N = int(math.log10(N)) + 1

# Storing the values for every digit in the number N
arr = [0] * length_of_N

for i in range(len(arr) - 1, -1, -1):
    arr[i] = N % 10
    N //= 10

for i in range(len(arr)):
    # If the digit is not zero, add it to the answer
    if arr[i] != 0:
        ans *= 10
        ans += arr[i]

# Printing the output
print(ans)
JavaScript
function main() {
    let N = 50505; // Given number
    let ans = 0;
    // Using Math.log10 to find the number of digits in
    // constant time
    let length_of_N = Math.floor(Math.log10(N)) + 1;

    // Storing the values for every digit in the number N
    let arr = new Array(length_of_N);

    for (let i = arr.length - 1; i >= 0; i--) {
        arr[i] = N % 10;

        N = Math.floor(N / 10);
    }

    for (let i = 0; i < arr.length; i++) {

        if (arr[i] !== 0) {
            ans *= 10;
            ans += arr[i];
        }
    }
    console.log(ans);
}

main();

Output
555

Time complexity: O(Number of Digits).
Auxiliary Space: O(Number of Digits)