Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,860 questions

51,781 answers

573 users

How to check if a number is composed of a pair of duplicate digits next to each other in C

1 Answer

0 votes
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool hasDuplicateDigits(int n) {
    int total_digits = (int)floor(log10(n)) + 1;
    
    if (total_digits % 2 == 1) { // total digits not even
        return false;
    }
    
    while (n != 0) {
        int currentDigit = n % 10; 
        n /= 10; 
        int prevDigit = n % 10; 
        printf("%d : %d\n", prevDigit, currentDigit);
        if (currentDigit != prevDigit) {
            return false; 
        }
        n /= 10;
    }

    return true;
}

int main() {
    int number = 11338855; 

    if (hasDuplicateDigits(number)) {
        printf("yes");
    } else {
        printf("no");
    }

    return 0;
}

 
 
 
/*
run:
 
5 : 5
8 : 8
3 : 3
1 : 1
yes
 
*/

 



answered Jan 31, 2024 by avibootz
...