A Belphegor number is a palindromic number such that the number is of form
1(0...)666(0...)1
The Nth Belphegor number is given by:
Check if N is a Belphegor number
Given a number N, the task is to check if N is an Belphegor Number or not. If N is an Belphegor Number then print “Yes” else print “No”.
Examples:
Input: N = 16661 Output: Yes Explanation: This number is palindrome and is in the form 1(0…)666(0…)1. Input: N = 1661 Output: No
Approach: The idea is to check if the number is palindrome or not, if it is palindrome then check for number should be of form 1(0…)666(0…)1, if the number is in this form then print “Yes”, else print “No”. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isinform( int n)
{
string temp = to_string(n);
int Len = temp.length();
if (temp.find( "666" ) == string::npos || Len < 5
|| Len % 2 == 0)
return false ;
int mid = (Len / 2);
if (!(temp[mid - 1] == temp[mid] && temp[mid - 1] == '6'
&& temp[0] == '1' ))
return false ;
for ( int i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false ;
return true ;
}
bool ispalindrome( int n)
{
string temp = to_string(n);
string revtemp = to_string(n);
reverse(revtemp.begin(), revtemp.end());
return (temp == revtemp);
}
bool isBelphegor( int n)
{
return ispalindrome(n) && isinform(n);
}
int main()
{
int n = 100666001;
if (isBelphegor(n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG {
public static String Reverse(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
sb.reverse();
return new String(sb);
}
static boolean isinform( int n)
{
String temp = String.valueOf(n);
int Len = temp.length();
if ((temp.indexOf( "666" ) >= 0 ) || Len < 5
|| Len % 2 == 0 )
return false ;
int mid = (Len / 2 );
if (!(temp.charAt(mid - 1 ) == temp.charAt(mid)
&& temp.charAt(mid - 1 ) == '6'
&& temp.charAt( 0 ) == '1' ))
return false ;
for ( int i = mid + 2 ; i < Len - 1 ; i++)
if (temp.charAt(i) != '0' )
return false ;
return true ;
}
static boolean ispalindrome( int n)
{
String temp = String.valueOf(n);
String revtemp = Reverse(String.valueOf(n));
return (temp.equals(revtemp));
}
static boolean isBelphegor( int n)
{
return ispalindrome(n) && isinform(n);
}
public static void main(String[] args)
{
int n = 100666001 ;
if (isBelphegor(n))
System.out.println( "Yes" );
else
System.out.println( "Yes" );
}
}
|
Python3
def isinform(n):
temp = str (n)
Len = len (temp)
if "666" not in temp or \
Len < 5 or Len % 2 = = 0 :
return False
mid = Len / / 2
if not (temp[mid - 1 ] = = temp[mid]\
and temp[mid - 1 ] = = '6' and \
temp[ 0 ] = = '1' ):
return False
for i in range (mid + 2 , Len - 1 ):
if temp[i] ! = '0' :
return False
return True
def ispalindrome(n):
temp = str (n)
if temp = = temp[:: - 1 ]:
return True
return False
def isBelphegor(n):
if ispalindrome(n):
if isinform(n):
return True
return False
n = 100666001 ;
if isBelphegor(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static string Reverse( string s )
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
static bool isinform( int n)
{
string temp = Convert.ToString(n);
int Len = temp.Length;
if ( (temp.IndexOf( "666" ) >= 0) || Len < 5
|| Len % 2 == 0)
return false ;
int mid = (Len / 2);
if (!(temp[mid - 1] == temp[mid] && temp[mid - 1] == '6'
&& temp[0] == '1' ))
return false ;
for ( int i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false ;
return true ;
}
static bool ispalindrome( int n)
{
string temp = Convert.ToString(n);
string revtemp = Reverse(Convert.ToString(n));
return (temp.Equals(revtemp));
}
static bool isBelphegor( int n)
{
return ispalindrome(n) && isinform(n);
}
public static void Main( string [] args)
{
int n = 100666001;
if (isBelphegor(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "Yes" );
}
}
|
Javascript
function isinform(n)
{
let temp = (n).toString()
let Len = (temp).length
if (!temp.includes( "666" ) ||
Len<5 || Len % 2 == 0)
return false
let mid = Math.floor(Len/2)
if (!(temp[mid-1] == temp[mid]
&& temp[mid-1] == '6' &&
temp[0] == '1' ))
return false
for ( var i = mid + 2; i < Len - 1; i++)
if (temp[i] != '0' )
return false
return true
}
function ispalindrome(n)
{
let temp = (n).toString()
return (temp == temp.split( "" ).reverse().join( "" ))
}
function isBelphegor(n)
{
return ispalindrome(n) && isinform(n)
}
let n = 100666001;
if (isBelphegor(n))
console.log( "Yes" )
else
console.log( "No" )
|
Last Updated :
29 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...