C Program to Remove consecutive repeated digits in a given number

Write a program to remove consecutive repeated digits in a given number

Example: 311200 => 3120
                   12341 => 12341                   

Input Format
Input contains an integer N

Output Format
Print the number after removing the digits

Constraint
1<=N<=10^12Sample Input 1

12288

Sample Output 1

128

Code:

#include<stdio.h>

int main()
{
    long int n;
    
    scanf("%ld",&n);
    
    int prev_digit = n % 10;
 
    long int pow = 10;
    long int res = prev_digit;
 

    while (n) {
        int curr_digit = n % 10;
 
        if (curr_digit != prev_digit) {
            res += curr_digit * pow;
 
            prev_digit = curr_digit;
            pow *= 10;
        }
 
        n = n / 10;
    }
         printf("%ld",res);
    return 0;
}
Code language: C/AL (cal)

Leave a Comment