How to check if total digits before and after the decimal point are equal in C

1 Answer

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

void removeTrailingZeros(char *str) {
    int len = strlen(str);
    
    while (len > 0 && str[len - 1] == '0') {
        str[len - 1] = '\0';
        len--;
    }
}

int is_equal(double d) {
    int left, right;
    char strnum[32] = "";
    
    sprintf(strnum, "%f", d);
    removeTrailingZeros(strnum);
    
    sscanf(strnum, "%d.%d", &left, &right);

    return (int)log10(left) + 1 == (int)log10(right) + 1;
}
 

int main() {
    double d = 381.902;

    printf("%d", is_equal(d));  
     
    return 0;
}


 
/*
run:
 
1
 
*/
 

 



answered Nov 13, 2024 by avibootz

Related questions

...