Showing posts with label Magic number. Show all posts
Showing posts with label Magic number. Show all posts

Wednesday, June 29, 2022

Magic number

 Magic number

Problem Statement
A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.


static boolean check(int n){
boolean s=false;
boolean p=false;
while(n>0){
if(n%10==7){s=true;}
if(n%10==9){p=true;}
n/=10;
}
if(s && p){return false;}
return true;

}
static int MagicNumber(int n){
int i=0;

while(check(n-i)==true && check(n+i)==true){

i++;

}

if(check(n-i)==false){return n-i;}

else{

return n+i;}

}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...