Check if number can be made prime by deleting a single digit | C ++ implementation to check if a number becomes prime by deleting any digit ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given string str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to string ; length of string ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the string ; converting string to int ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } string deleteIth ( string str , int i ) { str . erase ( str . begin ( ) + i ) ; return str ; } bool isPrimePossible ( int N ) { string s = to_string ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l ; i ++ ) { string str = deleteIth ( s , i ) ; int num = stoi ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } int main ( ) { int N = 610 ; isPrimePossible ( N ) ? cout << " Yes " : cout << " No " ; return 0 ; }