# C/C++ Program to remove alternate digits of an integer

Write a program to remove alternate digits of an integer in c/c++.

```Intput
1234567

Output
1357```

The simplest way to remove alternate digits of an integer is to convert the integer to a string using itoa() and then read the alternate digits using a loop and combine the digits together.

Steps

1. Let n be the input integer.
2. Covert n to a string using itoa(). Let the string be str.
3. Set temp = 0 and i = 0.
4. Repeat the following step while i < str.Length
1. temp = temp * 10 + str[i]
2. i = i + 2
5. The final answer is stored in temp.

## Program to remove alternate digits

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int removeAlternateDigits(int n)
{
char str;
int temp, i;

// convert integer n into a character string
// the string is placed in str
// 10 indicates n base 10
itoa(n, str, 10);

temp = 0;
// reading only alternate digits of the string
for (i = 0; i < strlen(str); i = i + 2)
{
// append str[i] to right side of temp
// str[i]-'0' converts str[i] to corresponding integer
temp = temp * 10 + (str[i] - '0');
}

return temp;

}

int main()
{
int n;
printf("Enter a Number: ");
scanf("%d", &n);

n = removeAlternateDigits(n);

printf("Number after removing alternate digits: %d", n);

}```

Output

```Enter a Number: 123456789
Number after removing alternate digits: 13579```

## Program Remove alternate digits without using string

In this section, we will discuss how we can remove alternate digits from an integer without converting integer to string.

Steps

1. Let n be the input integer.
2. Reverse n.
3. Set temp = 0.
4. Repeat the following steps while n > 0
1. temp = temp*10 + n%10
2. n = n / 100
5. The final answer is stored in temp.
```#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// function to reverse n
int reverse(int n)
{
int temp = 0;
while (n > 0)
{
temp = temp * 10 + n % 10;
n = n / 10;
}
return temp;
}

int removeAlternateDigits(int n)
{
int temp = 0;
n = reverse(n);
while (n > 0) {
temp = temp * 10 + n % 10; // append n%10 to right side of temp
n = n / 100; // removing 2 digits from right side of n
}
return temp;
}

int main()
{
int n;
printf("Enter a Number: ");
scanf("%d", &n);

n = removeAlternateDigits(n);

printf("Number after removing alternate digits: %d", n);

}```

Output

```Enter a Number: 987654321
Number after removing alternate digits: 97531```

The problem you just solved is very simple. It won’t be enough to crack rounds of many companies. We recommend you to visit Coding Ninjas and check out their courses. You will learn about all the important algorithms and data structures from industry experts that will help you a lot in cracking coding interviews.

### 2 thoughts on “C/C++ Program to remove alternate digits of an integer”

1. How to do this in Java?

1. I will upload the Java version by tomorrow. The logic is the same tho.