Check if a given mobile number is fancy

Here given code implementation process.

/*
    Java Program for
    Check if a given mobile number is fancy
*/
public class FancyNumber
{
    public boolean condition(String num, int n)
    {
        // Case A
        // Check if three Adjacent Similar
        for (int i = 0; i < n - 2 ; ++i) 
        {
            if(num.charAt(i)==num.charAt(i+1) && 
                num.charAt(i+1) ==num.charAt(i+2))
            {
                return true;
            }
        }
        // Case B
        // Check if three Adjacent digits are increasing or decreasing. 
        for (int i = 0; i < n - 2 ; ++i ) 
        {
            if((num.charAt(i) > num.charAt(i+1) && 
                num.charAt(i+1) > num.charAt(i+2))||
               (num.charAt(i) < num.charAt(i+1) && 
                num.charAt(i+1) < num.charAt(i+2)) )
            {
                return true;
            }
        }

        // Case C
        // Check that if any digits are occur more than 3 times or not.
        int []count = new int[10];
        // Set initial frequency of possible digit
        for (int i = 0; i < 10 ; ++i ) 
        {
            count[i]=0;
        }
        // count frequency of each digit
        for (int i = 0; i < 10 ; ++i ) 
        {
            count[num.charAt(i)- '0']++;

            if(count[num.charAt(i)- '0'] >= 4)
            {
                return true;
            }
        }

        return false;
    }

    public void isFancyNumber(String num)
    {
        int n = num.length();
        // Assume num is valid number
        if(condition(num,n))
        {
            System.out.println(" "+num+" is Fancy Number");
        }
        else
        {
            System.out.println(" "+num+" is not Fancy Number");
        }
    }
   
    public static void main(String[] args)
    {
        FancyNumber task = new FancyNumber();
        // Test A
        task.isFancyNumber("8785917233");
        // Test B
        task.isFancyNumber("9795017233");
    }
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Check if a given mobile number is fancy
*/
class FancyNumber
{
	public: bool condition(string num, int n)
	{
		// Case A
		// Check if three Adjacent Similar
		for (int i = 0; i < n - 2; ++i)
		{
			if (num[i] == num[i + 1] && 
                num[i + 1] == num[i + 2])
			{
				return true;
			}
		}
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		for (int i = 0; i < n - 2; ++i)
		{
			if ((num[i] > num[i + 1] && 
                 num[i + 1] > num[i + 2]) || 
                (num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
			{
				return true;
			}
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		int *count = new int[10];
		// Set initial frequency of possible digit
		for (int i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// count frequency of each digit
		for (int i = 0; i < 10; ++i)
		{
			count[num[i] - '0']++;
			if (count[num[i] - '0'] >= 4)
			{
				return true;
			}
		}
		return false;
	}
	void isFancyNumber(string num)
	{
		int n = num.length();
		// Assume num is valid number
		if (this->condition(num, n))
		{
			cout << " " << num << " is Fancy Number" << endl;
		}
		else
		{
			cout << " " << num << " is not Fancy Number" << endl;
		}
	}
};
int main()
{
	FancyNumber *task = new FancyNumber();
	// Test A
	task->isFancyNumber("8785917233");
	// Test B
	task->isFancyNumber("9795017233");
	return 0;
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
// Include namespace system
using System;
/*
    Csharp Program for
    Check if a given mobile number is fancy
*/
public class FancyNumber
{
	public Boolean condition(String num, int n)
	{
		// Case A
		// Check if three Adjacent Similar
		for (int i = 0; i < n - 2; ++i)
		{
			if (num[i] == num[i + 1] && 
                num[i + 1] == num[i + 2])
			{
				return true;
			}
		}
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		for (int i = 0; i < n - 2; ++i)
		{
			if ((num[i] > num[i + 1] && 
                 num[i + 1] > num[i + 2]) || 
                (num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
			{
				return true;
			}
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		int[] count = new int[10];
		// Set initial frequency of possible digit
		for (int i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// count frequency of each digit
		for (int i = 0; i < 10; ++i)
		{
			count[num[i] - '0']++;
			if (count[num[i] - '0'] >= 4)
			{
				return true;
			}
		}
		return false;
	}
	public void isFancyNumber(String num)
	{
		int n = num.Length;
		// Assume num is valid number
		if (this.condition(num, n))
		{
			Console.WriteLine(" " + num + " is Fancy Number");
		}
		else
		{
			Console.WriteLine(" " + num + " is not Fancy Number");
		}
	}
	public static void Main(String[] args)
	{
		FancyNumber task = new FancyNumber();
		// Test A
		task.isFancyNumber("8785917233");
		// Test B
		task.isFancyNumber("9795017233");
	}
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
package main

import "fmt"
/*
    Go Program for
    Check if a given mobile number is fancy
*/

func condition(num string, n int) bool {
	// Case A
	// Check if three Adjacent Similar
	for i := 0 ; i < n - 2 ; i++ {
		if num[i] == num[i + 1] && num[i + 1] == num[i + 2] {
			return true
		}
	}
	// Case B
	// Check if three Adjacent digits are increasing or decreasing. 
	for i := 0 ; i < n - 2 ; i++ {
		if (num[i] > num[i + 1] && num[i + 1] > num[i + 2]) || 
		(num[i] < num[i + 1] && num[i + 1] < num[i + 2]) {
			return true
		}
	}
	// Case C
	// Check that if any digits are occur more than 3 times or not.
	// Set initial frequency of possible digit
	var count = make([] int, 10)
	// count frequency of each digit
	for i := 0 ; i < 10 ; i++ {
		count[int(num[i]) - 48]++
		if count[int(num[i]) - 48] >= 4 {
			return true
		}
	}
	return false
}
func isFancyNumber(num string) {
	var n int = len(num)
	// Assume num is valid number
	if condition(num, n) {
		fmt.Println(" ", num, " is Fancy Number")
	} else {
		fmt.Println(" ", num, " is not Fancy Number")
	}
}
func main() {

	// Test A
	isFancyNumber("8785917233")
	// Test B
	isFancyNumber("9795017233")
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
<?php
/*
    Php Program for
    Check if a given mobile number is fancy
*/
class FancyNumber
{
	public	function condition($num, $n)
	{
		// Case A
		// Check if three Adjacent Similar
		for ($i = 0; $i < $n - 2; ++$i)
		{
			if ($num[$i] == $num[$i + 1] && 
                $num[$i + 1] == $num[$i + 2])
			{
				return true;
			}
		}
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		for ($i = 0; $i < $n - 2; ++$i)
		{
			if (($num[$i] > $num[$i + 1] && 
                 $num[$i + 1] > $num[$i + 2]) || 
                ($num[$i] < $num[$i + 1] && 
                 $num[$i + 1] < $num[$i + 2]))
			{
				return true;
			}
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		$count = array_fill(0, 10, 0);
		// Set initial frequency of possible digit
		for ($i = 0; $i < 10; ++$i)
		{
			$count[$i] = 0;
		}
		// count frequency of each digit
		for ($i = 0; $i < 10; ++$i)
		{
			$count[ord($num[$i]) - ord('0')]++;
			if ($count[ord($num[$i]) - ord('0')] >= 4)
			{
				return true;
			}
		}
		return false;
	}
	public	function isFancyNumber($num)
	{
		$n = strlen($num);
		// Assume num is valid number
		if ($this->condition($num, $n))
		{
			echo(" ".$num." is Fancy Number\n");
		}
		else
		{
			echo(" ".$num." is not Fancy Number\n");
		}
	}
}

function main()
{
	$task = new FancyNumber();
	// Test A
	$task->isFancyNumber("8785917233");
	// Test B
	$task->isFancyNumber("9795017233");
}
main();

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
/*
    Node JS Program for
    Check if a given mobile number is fancy
*/
class FancyNumber
{
	condition(num, n)
	{
		// Case A
		// Check if three Adjacent Similar
		for (var i = 0; i < n - 2; ++i)
		{
			if (num.charAt(i) == 
                num.charAt(i + 1) 
                && num.charAt(i + 1) == num.charAt(i + 2))
			{
				return true;
			}
		}
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		for (var i = 0; i < n - 2; ++i)
		{
			if ((num.charAt(i) > num.charAt(i + 1) && 
                 num.charAt(i + 1) > num.charAt(i + 2)) || 
                (num.charAt(i) < num.charAt(i + 1) && 
                 num.charAt(i + 1) < num.charAt(i + 2)))
			{
				return true;
			}
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		var count = Array(10).fill(0);
		// Set initial frequency of possible digit
		for (var i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// count frequency of each digit
		for (var i = 0; i < 10; ++i)
		{
			count[num.charCodeAt(i) - 48]++;
			if (count[num.charCodeAt(i)- 48] >= 4)
			{
				return true;
			}
		}
		return false;
	}
	isFancyNumber(num)
	{
		var n = num.length;
		// Assume num is valid number
		if (this.condition(num, n))
		{
			console.log(" " + num + " is Fancy Number");
		}
		else
		{
			console.log(" " + num + " is not Fancy Number");
		}
	}
}

function main()
{
	var task = new FancyNumber();
	// Test A
	task.isFancyNumber("8785917233");
	// Test B
	task.isFancyNumber("9795017233");
}
main();

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
#    Python 3 Program for
#    Check if a given mobile number is fancy
class FancyNumber :
	def condition(self, num, n) :
		i = 0
		#  Case A
		#  Check if three Adjacent Similar
		while (i < n - 2) :
			if (num[i] == num[i + 1] and num[i + 1] == num[i + 2]) :
				return True
			
			i += 1
		
		i = 0
		#  Case B
		#  Check if three Adjacent digits are increasing or decreasing. 
		while (i < n - 2) :
			if ((num[i] > num[i + 1] and 
                 num[i + 1] > num[i + 2]) or
            (num[i] < num[i + 1] and num[i + 1] < num[i + 2])) :
				return True
			
			i += 1
		
		#  Case C
		#  Check that if any digits are occur more than 3 times or not.
		#  Set initial frequency of possible digit
		count = [0] * (10)
		i = 0
		#  count frequency of each digit
		while (i < 10) :
			count[ord(num[i]) - ord('0')] += 1
			if (count[ord(num[i]) - ord('0')] >= 4) :
				return True
			
			i += 1
		
		return False
	
	def isFancyNumber(self, num) :
		n = len(num)
		#  Assume num is valid number
		if (self.condition(num, n)) :
			print(" ", num ," is Fancy Number")
		else :
			print(" ", num ," is not Fancy Number")
		
	

def main() :
	task = FancyNumber()
	#  Test A
	task.isFancyNumber("8785917233")
	#  Test B
	task.isFancyNumber("9795017233")

if __name__ == "__main__": main()

Output

  8785917233  is not Fancy Number
  9795017233  is Fancy Number
#    Ruby Program for
#    Check if a given mobile number is fancy
class FancyNumber 
	def condition(num, n) 
		i = 0
		#  Case A
		#  Check if three Adjacent Similar
		while (i < n - 2) 
			if (num[i] == num[i + 1] && num[i + 1] == num[i + 2]) 
				return true
			end

			i += 1
		end

		i = 0
		#  Case B
		#  Check if three Adjacent digits are increasing or decreasing. 
		while (i < n - 2) 
			if ((num[i] > num[i + 1] && 
                 num[i + 1] > num[i + 2]) || 
                (num[i] < num[i + 1] && num[i + 1] < num[i + 2])) 
				return true
			end

			i += 1
		end

		#  Case C
		#  Check that if any digits are occur more than 3 times or not.
		#  Set initial frequency of possible digit
		count = Array.new(10) {0}
		i = 0
		#  count frequency of each digit
		while (i < 10) 
			count[num[i].ord - '0'.ord] += 1
			if (count[num[i].ord - '0'.ord] >= 4) 
				return true
			end

			i += 1
		end

		return false
	end

	def isFancyNumber(num) 
		n = num.length
		#  Assume num is valid number
		if (self.condition(num, n)) 
			print(" ", num ," is Fancy Number", "\n")
		else
 
			print(" ", num ," is not Fancy Number", "\n")
		end

	end

end

def main() 
	task = FancyNumber.new()
	#  Test A
	task.isFancyNumber("8785917233")
	#  Test B
	task.isFancyNumber("9795017233")
end

main()

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
/*
    Scala Program for
    Check if a given mobile number is fancy
*/
class FancyNumber()
{
	def condition(num: String, n: Int): Boolean = {
		var i: Int = 0;
		// Case A
		// Check if three Adjacent Similar
		while (i < n - 2)
		{
			if (num.charAt(i) == num.charAt(i + 1) && 
                num.charAt(i + 1) == num.charAt(i + 2))
			{
				return true;
			}
			i += 1;
		}
		i = 0;
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		while (i < n - 2)
		{
			if ((num.charAt(i) > num.charAt(i + 1) && 
                 num.charAt(i + 1) > num.charAt(i + 2)) || 
                (num.charAt(i) < num.charAt(i + 1) && 
                 num.charAt(i + 1) < num.charAt(i + 2)))
			{
				return true;
			}
			i += 1;
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		// Set initial frequency of possible digit
		var count: Array[Int] = Array.fill[Int](10)(0);
		i = 0;
		// count frequency of each digit
		while (i < 10)
		{
			count(num.charAt(i).toInt - '0'.toInt) += 1;
			if (count(num.charAt(i).toInt - '0'.toInt) >= 4)
			{
				return true;
			}
			i += 1;
		}
		return false;
	}
	def isFancyNumber(num: String): Unit = {
		var n: Int = num.length();
		// Assume num is valid number
		if (condition(num, n))
		{
			println(" " + num + " is Fancy Number");
		}
		else
		{
			println(" " + num + " is not Fancy Number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: FancyNumber = new FancyNumber();
		// Test A
		task.isFancyNumber("8785917233");
		// Test B
		task.isFancyNumber("9795017233");
	}
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number
import Foundation;
/*
    Swift 4 Program for
    Check if a given mobile number is fancy
*/
class FancyNumber
{
	func condition(_ text: String, _ n: Int) -> Bool
	{
       	var num = Array(text);
		var i: Int = 0;
		// Case A
		// Check if three Adjacent Similar
		while (i < n - 2)
		{
			if (num[i] == num[i + 1] && num[i + 1] == num[i + 2])
			{
				return true;
			}
			i += 1;
		}
		i = 0;
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		while (i < n - 2)
		{
			if ((num[i] > num[i + 1] && 
                 num[i + 1] > num[i + 2]) || 
                (num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
			{
				return true;
			}
			i += 1;
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		// Set initial frequency of possible digit
		var count: [Int] = Array(repeating: 0, count: 10);
		i = 0;
		// count frequency of each digit
		while (i < 10)
		{
			count[Int(UnicodeScalar(String(num[i]))!.value) - 48] += 1;
			if (count[Int(UnicodeScalar(String(num[i]))!.value) - 48] >= 4)
			{
				return true;
			}
			i += 1;
		}
		return false;
	}
	func isFancyNumber(_ num: String)
	{
		let n: Int = num.count;
		// Assume num is valid number
		if (self.condition(num, n))
		{
			print(" ", num ," is Fancy Number");
		}
		else
		{
			print(" ", num ," is not Fancy Number");
		}
	}
}
func main()
{
	let task: FancyNumber = FancyNumber();
	// Test A
	task.isFancyNumber("8785917233");
	// Test B
	task.isFancyNumber("9795017233");
}
main();

Output

  8785917233  is not Fancy Number
  9795017233  is Fancy Number
/*
    Kotlin Program for
    Check if a given mobile number is fancy
*/
class FancyNumber
{
	fun condition(num: String, n: Int): Boolean
	{
		var i: Int = 0;
		// Case A
		// Check if three Adjacent Similar
		while (i < n - 2)
		{
			if (num.get(i) == num.get(i + 1) && 
                num.get(i + 1) == num.get(i + 2))
			{
				return true;
			}
			i += 1;
		}
		i = 0;
		// Case B
		// Check if three Adjacent digits are increasing or decreasing. 
		while (i < n - 2)
		{
			if ((num.get(i) > num.get(i + 1) && 
                 num.get(i + 1) > num.get(i + 2)) || 
                (num.get(i) < num.get(i + 1) && 
                 num.get(i + 1) < num.get(i + 2)))
			{
				return true;
			}
			i += 1;
		}
		// Case C
		// Check that if any digits are occur more than 3 times or not.
		// Set initial frequency of possible digit
		val count: Array < Int > = Array(10)
		{
			0
		};
		i = 0;
		// count frequency of each digit
		while (i < 10)
		{
			count[num.get(i).toInt() - '0'.toInt()] += 1;
			if (count[num.get(i).toInt() - '0'.toInt()] >= 4)
			{
				return true;
			}
			i += 1;
		}
		return false;
	}
	fun isFancyNumber(num: String): Unit
	{
		val n: Int = num.length;
		// Assume num is valid number
		if (this.condition(num, n))
		{
			println(" " + num + " is Fancy Number");
		}
		else
		{
			println(" " + num + " is not Fancy Number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: FancyNumber = FancyNumber();
	// Test A
	task.isFancyNumber("8785917233");
	// Test B
	task.isFancyNumber("9795017233");
}

Output

 8785917233 is not Fancy Number
 9795017233 is Fancy Number


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved