Introduction
In this post, you can find a c program to remove zeros from a number with a detailed explanation. In this c program, we will read a number and then remove zeros from that number. See the following expected output.
Expected Output
Enter any Number
1020304050607
Original Number = 1020304050607
Number after removing all the Zeros = 1234567
I hope you have got an idea about the c program to remove zeros from a number. Let’s see the program.
Also read: Switch Case in C Program to Calculate Area of Circle and Triangle
C Program to Remove Zeros from a Number
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rem,rev=0,rnum;
long int n,an;
printf("Enter any Number\n");
scanf("%ld",&n);
an=n;
while(n>0)
{
rem=n%10;
if(rem!=0)
rev=rev*10+rem;
n=n/10;
}
while(rev>0)
{
rem=rev%10;
rnum=rnum*10+rem;
rev=rev/10;
}
printf("Original Number = %ld\n",an);
printf("Number after removing all the Zeros = %d",rnum);
return 0;
}
Detailed Explanation
Here, I am explaining the whole program.
int rem,rev=0,rnum;
These three variables are declared as an integer. The rem for storing remainder, rev is for storing the reversed number and rnum will store our actual output i.e. the number without zeros.
long int n,an;
These two variables are long integers. We have to write a c program to remove zeros from a number. So, there is a possibility that a user can enter a very large number, therefore, we have declared these variables as long integer. The variable n will store the number and we will copy that number in the variable ‘an’.
printf(“Enter any Number\n”);
This printf() statement display the message Enter any Number on the screen.
scanf(“%ld”,&n);
Using this scanf() statement, we are reading a long integer from the user and storing that number in the variable n.
an=n;
We are assigning the value of the variable n to the variable an. As we go further, the value of n will become 0. But, we need to display this number at the last in our output. Therefore, we are keeping extra copy of this variable. This is optional.
While loop to remove zeros from a number
while(n>0)
This is the while loop which contains one expression i.e. n>0. That means this while loop will be executed until the value of n is greater than 0. We have written this expression because we have to read each and every digit of a given number. Now, the following four statements are written inside this for loop.
i. rem=n%10;
We are dividing the given number by 10 and storing the remainder in the variable rem. Whenever we divide any number by 10, then the last digit of that number is the remainder. So, here we are storing the remainder in the variable rem. So, every time we will read the last digit and will store that digit in the variable rem.
ii. if(rem!=0)
This is the second statement written inside the first while loop. As we know, we have to write a c program to remove all zeros from a number. So, in this if statement, we are checking whether the value of rem is 0 or not. If it is 0 then we will remove it from the number, otherwise, we will move to the next digit.
iii. rev=rev*10+rem;
This is the third statement in the first while loop. This statement will be executed only when the if statement is true. Here, we are using the variable rev to store all the digits without zeros. If the value of rem is not equal to zero, only when this statement will be executed. Here, after executing the whole while loop, this variable i.e. rev will store the number without zeros but in reverse order.
iv) n=n/10;
This statement will update the value of n. Here, the length of the number is reduced by 1 for each iteration. Whenever we divide any number by 10, then we will get the same number excluding the last digit as a quotient. Suppose, the value of n is 12345, then after n=n/10, the value of n is 1234 which is our quotient. So, every time this value will be reduced and at the last, the value of n becomes 0. After this, our while loop will be terminated.
After executing this while loop, we have removed all the zeros from the number. But, there is one problem. Suppose, our given number is 102030405, then after executing this while loop, the value of the variable rev is 54321. So, this is not our output. We know, we have to write a c program to remove zeros from a number but after removing zeros, this number is reversed. So, we will again write one while loop to reverse this number. So that we will get our actual output.
Please read Reverse a Number in C. In this article, I have explained each and everything how to reverse a number.
See the following second while loop.
while(rev>0)
{
rem=rev%10;
rnum=rnum*10+rem;
rev=rev/10;
}
This code is reversing the number. After executing this loop, we will get our actual output. I hope, you have understood this program. If you have any difficulties, please feel free to contact me.
Thank you.