Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a function which checks if all digits of a number are equal (11,22,666 and so on). It seems like the function returns always true no matter the argument. I checked the math over and over again and went through the code many times with debugger but I cant figure out what is the problem.

What I have tried:

C
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool hasidenticdigits(long long int number){

    int i, divisor = 10, size = 0, firstnumber, checkfirst;
    int temp = number;
    bool isidentic = true;

    if(temp>=10){
        while(temp>=10){                    //find size of array
            temp = (temp - (temp%divisor)) / 10;
            size++;
        }
        int array[size];

        for(i=0; i<=size; i++){
            if(number>=10){              //assign digits to array
                array[i] = number%divisor;
                number = (number - (number%divisor)) / 10;
            }
            else array[i] = number;
        }

        for(i=0; i<size; i++){
            checkfirst = array[0];                  //check the number
            if(array[i] != checkfirst){
                isidentic == false;
                break;
            }
        }
    }
    return isidentic;
}


int main(){
    char charprimecount[100];
    long long int number = 3, i, primecount;
    bool isprime;

    printf("how many prime numbers do you want to print?\n");
    primecount = atoi(fgets(charprimecount, 100, stdin));

        while(primecount>0){
            isprime = true;
            for(i=2; i<number; i++){
                if(number%i == 0)
                    isprime = false;
            }
            if(isprime == true){
                if(hasidenticdigits(number) == true){
                    primecount -= 1;
                    printf("%lld\t", number);
                }
            }
            number++;
        }

    return 0;
}
Posted
Updated 26-Nov-20 7:23am
v3

You can further improve your code writing, for instance
C
bool has_identic_digits(long long int number)
{ 
  long long int reminder = number % 10;
  while (number > 9)
  { 
    number /= 10; 
    if ( number % 10 != reminder)
      return false;
  }
  return true;
}
 
Share this answer
 
Comments
Richard MacCutchan 26-Nov-20 15:15pm    
Have 5 yourself my friend.
CPallini 26-Nov-20 17:28pm    
Thank you very much, my friend!
You could improve the following loop by adding a break command:
C++
for(i=2; i<number; i++){
    if(number%i == 0)
    {
        isprime = false;
        break; // no need to check any further
    }
}

And as Karsten has already mentioned the expression isidentic == false;, in the last loop of the hasidenticdigits function, does not change the variable.
 
Share this answer
 
Comments
CPallini 26-Nov-20 13:22pm    
5.
you must correct your code to this:
C++
isidentic = false;
Tip: learn to use the debugger, best when reproducing the error cases ;-)
 
Share this answer
 
v3
Comments
CPallini 26-Nov-20 13:21pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900