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,857 questions

51,778 answers

573 users

How to check if a number has at least one pair of duplicate digits next to each other in C++

1 Answer

0 votes
#include <iostream>

bool hasDuplicateDigits(int n) {
    int prevDigit = n % 10; // Last digit
    n /= 10; // Remove the last digit
 
    while (n != 0) {
        int currentDigit = n % 10;
        if (currentDigit == prevDigit) {
            return true; 
        }
        prevDigit = currentDigit;
        n /= 10;
    }
 
    return false;
}
 
int main() {
    int number = 1233879; 
     
    if (hasDuplicateDigits(number)) {
        std::cout << number << " has at least one pair of duplicate digits";
    } else {
        std::cout << number << " does not have duplicate digits";
    }
}
 
  
  
  
/*
run:
  
1233879 has at least one pair of duplicate digits
  
*/


 



answered Jan 31, 2024 by avibootz
...