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

51,765 answers

573 users

How to check if number can be made prime by deleting a single digit in C

1 Answer

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

unsigned int remove_N_digit(unsigned int n, const int index_from, const int index_to) {
    static char tmp[10];
     
    sprintf(tmp, "%d", n);

    int sectionlen = index_to - index_from;
    int stringlen = strlen(tmp);

    memmove(tmp + index_from, tmp + index_from + sectionlen, stringlen - index_to);
    tmp[stringlen - sectionlen] = '\0';

    return atoi(tmp);
}

bool isPrime(int n) {
    if (n == 0) return false;
    if (n == 1) return false;
   
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
     
    return true;
}
 
int main(void) {
    unsigned int n = 78919;
    unsigned int total_digits = (int)log10(n) + 1;

    int i = 0;
    while (i < total_digits) {
        int tmp = remove_N_digit(n, i, i + 1);
        if (isPrime(tmp)) {
            printf("yes number = %d\n", tmp);
            break;
        }
        i++;
    }
     
    return 0;
}
 
 
   
/*
run:
   
yes number = 7919

*/

 

 



answered Jan 12, 2024 by avibootz

Related questions

...