C Program to Remove Every Alternate Digit from a Number

Introduction

In this post, I am going to write a c program to remove every alternate digit from a number. Basically, we will read a number from the console through the keyboard. After that, we will remove every alternate digit, i.e. position 2, 4, 6, etc. Let us see the expected output.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Expected Output

c program to remove every alternate digit
Output – c program to remove every alternate digit

As you can see in the above-expected output, we are removing every alternate digit from the number. For writing this program, let us perform the following steps.

  1. Read a number from the console.
  2. Reverse the number.
  3. After reversing the number, using modulo and division operator.
  4. Repeat steps 5, 6, 7 and 8 until the reversed number is greater than 0.
  5. Find the remainder by using the modulo operator. Suppose that reversed number is 987654321.
  6. Now, divide that number by 10 using the modulo operator.
  7. We have to remove only those number which is at position 2, 4, 6 and so on. For this, we will use the counter variable count and if the value of that count variable is even, then we will remove that number.
  8. Now, make the number less by 1. How? Just divide the number by 10 and you will get the quotient.

I hope you have understood the above steps. Now, see the following program.

Also Read: C Program to Print Multiples of 5 using do-while loop

C Program to Remove Every Alternate Digit from a Number

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int rem,rev=0,rnum=0,count=0;;
    long int n,an;
    printf("Enter any Number\n");
    scanf("%ld",&n);
    an=n;
    while(n>0)
    {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }
    while(rev>0)
    {
        rem=rev%10;
        count++;
        if(count%2==1)
        rnum=rnum*10+rem;
        rev=rev/10;
    }
    printf("Original Number = %ld\n",an);
    printf("Number after removing alternate digits = %d",rnum);
    return 0;
}

In the above c program, there are six variables of type int and long int. They are

  1. rem is for the remainder,
  2. rev=0 will store reverse number,
  3. rnum=0 is for storing number after removing alternate digit,
  4. count=0 is for counting to check whether the position is even or odd,
  5. n is the number,
  6. an is the actual number

I hope you have understood the above program. If you have any difficulty understanding this program, please let me know.

Thank you.

Some Important C Programs

  1. C Program to Remove Zeros from a number
  2. The while loop in C Programming
  3. Reverse a Number using getchar and putchar function in c
  4. C Program to Print Numbers Except Multiples of n
  5. C Program to Remove White Spaces and Comments from a File
  6. Program in C to Find Longest Line in a File
  7. Palindrome in C using Pointers
  8. Insert and Delete element in Array in C using switch case
  9. C Program to Add Alternate Elements of a 2D Array
  10. Arrays in C for Complete Beginners
  11. C Program to Find Area of a Circle using Preprocessor

Leave a Comment