Check if a given number is pure number

Here given code implementation process.

//C Program
//Check if a given number is pure number
#include <stdio.h>


//Reverse a given number
int reverse(int number)
{
  int result = 0;

  while(number>0)
  {
    if(result==0)
    {
      result=number%10;
    }
    else
    {
      result=(result*10)+(number%10);
    }
    number/=10;
  }
  return result;
}
//Check given digit is combination of 4 or 5
int check_digit(int number)
{
  if(number<0)
  {
   return 0;
  }
  int status = 1;
  while(number!=0)
  {

    if(number%10>=4 && number%10<=5)
    {
      number/=10;
    }
    else
    {
      status = 0;
      break;
    }
  }
  return status;
}

//Count the length of given number
int digit_length(int number)
{
  if(number<0)
  {
    //When given is a negative number
    return 0;
  }

  //Set the initial value of length is zero
  int length = 0;

  while(number!=0)
  {
    //update length
    length++;
    //Remove last digit
    number/=10;
   
  }
  //return length
  return length;
}

void is_pure_no(int number)
{
  //Test case
  //Given digits length are Even ? 
  //Given number is combination of 4 or 5 ?
  //Number is form of palindrome ?
  if(digit_length(number)%2==0 && 
    check_digit(number)==1 && 
    number == reverse(number))
  {
    printf("%d is pure no\n",number );
  }
  else
  {
    printf("%d is not a pure no\n",number );
  }
}
int main()
{
  //test case
  is_pure_no(4554);
  is_pure_no(444444);
  is_pure_no(445454);
  is_pure_no(555);
  return 0;
}

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
 C++ Program
 Check if a given number is pure number
*/
#include<iostream>

using namespace std;
class MyNumber {
	public:

		//Reverse a given number
		int reverse(int number) {
			int result = 0;
			while (number > 0) {
				if (result == 0) {
					result = number % 10;
				} else {
					result = (result *10) + (number % 10);
				}
				number /= 10;
			}
			return result;
		}
	//Check given digit is combination of 4 and 5
	bool check_digit(int number) {
		if (number < 0) {
			return false;
		}
		bool status = true;
		while (number != 0) {
			//check if a given number is combination of 4 or 5

			if (number % 10 >= 4 && number % 10 <= 5) {
				number /= 10;
			} else {
				//When number is not a combination of 4 or 5
				status = false;
				break;
			}
		}
		return status;
	}
	//Count the length of given number
	int digit_length(int number) {
		if (number < 0) {
			return 0;
		}
		//Set the initial value of length is zero
		int length = 0;
		while (number != 0) {
			//update length
			length++;
			//Remove last digit
			number /= 10;
		}
		return length;
	}
	void is_pure_no(int number) {
		//Test case
		//Given digits length are Even ? 
		//Given number is combination of 4 or 5 ?
		//Number is form of palindrome ?

		if (this->digit_length(number) % 2 == 0 && this->check_digit(number) == true && number == this->reverse(number)) {
			cout << number << " is pure no\n";
		} else {
			cout << number << " is not a pure no\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test case
	obj.is_pure_no(4554);
	obj.is_pure_no(444444);
	obj.is_pure_no(445454);
	obj.is_pure_no(555);
	return 0;
}

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
  Java Program
  Check if a given number is pure number
*/

public class MyNumber {

  //Reverse a given number
  public int reverse(int number) {
    int result = 0;

    while (number > 0) {
      if (result == 0) {
        result = number % 10;
      } else {
        result = (result *10) + (number % 10);
      }
      number /= 10;
    }
    return result;
  }
  //Check given digit is combination of 4 and 5
  public boolean check_digit(int number) {
    if (number < 0) {
      return false;
    }
    boolean status = true;
    while (number != 0) {
      //check if a given number is combination of 4 or 5
      if (number % 10 >= 4 && number % 10 <= 5) {
        number /= 10;
      } else {
        //When number is not a combination of 4 or 5
        status = false;
        break;
      }
    }
    return status;
  }

  //Count the length of given number
  public int digit_length(int number) {
    if (number < 0) {
      //When given is a negative number
      return 0;
    }

    //Set the initial value of length is zero
    int length = 0;

    while (number != 0) {
      //update length
      length++;
      //Remove last digit
      number /= 10;

    }
    //return length
    return length;
  }

  public void is_pure_no(int number) {
    //Test case
    //Given digits length are Even ? 
    //Given number is combination of 4 or 5 ?
    //Number is form of palindrome ?
    if (digit_length(number) % 2 == 0 &&
      check_digit(number) == true &&
      number == reverse(number)) {
      System.out.print(number + " is pure no\n");

    } else {
      System.out.print(number + " is not a pure no\n");
    }
  }
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test case
    obj.is_pure_no(4554);
    obj.is_pure_no(444444);
    obj.is_pure_no(445454);
    obj.is_pure_no(555);

  }


}

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
  C# Program
  Check if a given number is pure number
*/
using System;
public class MyNumber {

	//Reverse a given number
	public int reverse(int number) {
		int result = 0;

		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result * 10) + (number % 10);
			}
			number /= 10;
		}
		return result;
	}
	//Check given digit is combination of 4 and 5
	public Boolean check_digit(int number) {
		if (number < 0) {
			return false;
		}
		Boolean status = true;
		while (number != 0) {
			//check if a given number is combination of 4 or 5
			if (number % 10 >= 4 && number % 10 <= 5) {
				number /= 10;
			} else {
				//When number is not a combination of 4 or 5
				status = false;
				break;
			}
		}
		return status;
	}

	//Count the.Length of given number
	public int digit_length(int number) {
		if (number < 0) {
			//When given is a negative number
			return 0;
		}

		//Set the initial value of.Length is zero
		int length = 0;

		while (number != 0) {
			//update.Length
		     length++;
			//Remove last digit
			number /= 10;

		}
		//return.Length
		return length;
	}

	public void is_pure_no(int number) {
		//Test case
		//Given digits.Length are Even ? 
		//Given number is combination of 4 or 5 ?
		//Number is form of palindrome ?
		if (digit_length(number) % 2 == 0 &&
			check_digit(number) == true &&
			number == reverse(number)) {
			Console.Write(number + " is pure no\n");

		} else {
			Console.Write(number + " is not a pure no\n");
		}
	}
	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.is_pure_no(4554);
		obj.is_pure_no(444444);
		obj.is_pure_no(445454);
		obj.is_pure_no(555);

	}
}

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
# Python 3 Program
# Check if a given number is pure number
class MyNumber :
	#Reverse a given number
	def reverse(self, number) :
		result = 0
		while (number > 0) :
			if (result == 0) :
				result = number % 10
			else :
				result = (result * 10) + (number % 10)
			
			number = int(number / 10)
		
		return result
	
	#Check given digit is combination of 4 and 5
	def check_digit(self, number) :
		if (number < 0) :
			return False
		
		status = True
		while (number != 0) :
			#check if a given number is combination of 4 or 5

			if (number % 10 >= 4 and number % 10 <= 5) :
				number = int(number / 10)
			else :
				#When number is not a combination of 4 or 5
				status = False
				break
			
		
		return status
	
	#Count the length of given number
	def digit_length(self, number) :
		if (number < 0) :
			return 0
		
		#Set the initial value of length is zero
		length = 0
		while (number != 0) :
			#update length
			length += 1
			#Remove last digit
			number = int(number / 10)
		
		return length
	
	def is_pure_no(self, number) :
    
		#Number is form of palindrome ?
		#Given number is combination of 4 or 5 ?
		#Given digits length are Even ? 
		#Test case

		if (self.digit_length(number) % 2 == 0 and self.check_digit(number) == True and number == self.reverse(number)) :
			print(number ," is pure no\n")
		else :
			print(number ," is not a pure no\n")
		
	

def main() :
	obj = MyNumber()
	#Test case
	obj.is_pure_no(4554)
	obj.is_pure_no(444444)
	obj.is_pure_no(445454)
	obj.is_pure_no(555)


if __name__ == "__main__":
	main()

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
# Ruby Program 
# Check if a given number is pure number
class MyNumber 
	#Reverse a given number
	def reverse(number) 
		result = 0
		while (number > 0) 
			if (result == 0) 
				result = number % 10
			else 
				result = (result * 10) + (number % 10)
			end
			number /= 10
		end
		return result
	end
	#Check given digit is combination of 4 and 5
	def check_digit(number) 
		if (number < 0) 
			return false
		end
		status = true
		while (number != 0) 
			#check if a given number is combination of 4 or 5

			if (number % 10 >= 4 and number % 10 <= 5) 
				number /= 10
			else 
				#When number is not a combination of 4 or 5
				status = false
				break
			end
		end
		return status
	end
	#Count the length of given number
	def digit_length(number) 
		if (number < 0) 
			return 0
		end
		#Set the initial value of length is zero
		length = 0
		while (number != 0) 
			#update length
			length += 1
			#Remove last digit
			number /= 10
		end
		return length
	end
	def is_pure_no(number) 
		#Number is form of palindrome ?

		#Given number is combination of 4 or 5 ?

		#Given digits length are Even ? 

		#Test case

		if (self.digit_length(number) % 2 == 0 and self.check_digit(number) == true and number == self.reverse(number)) 
			print(number ," is pure no\n")
		else 
			print(number ," is not a pure no\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	#Test case
	obj.is_pure_no(4554)
	obj.is_pure_no(444444)
	obj.is_pure_no(445454)
	obj.is_pure_no(555)
end
main()

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
 Scala Program
 Check if a given number is pure number
*/
import scala.util.control.Breaks._

class MyNumber {
	//Reverse a given number
	def reverse(value: Int): Int = {
		var result: Int = 0;
		var number: Int = value;
		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result *10) + (number % 10);
			}
			number /= 10;
		}
		return result;
	}
	//Check given digit is combination of 4 and 5
	def check_digit(value: Int): Boolean = {
		if (value < 0) {
			return false;
		}
		var number: Int = value;
		var status: Boolean = true;
		breakable {
			while (number != 0) {
				//check if a given number is combination of 4 or 5

				if (number % 10 >= 4 && number % 10 <= 5) {
					number /= 10;
				} else {
					//When number is not a combination of 4 or 5
					status = false;
					break;
				}
			}
		}

		return status;
	}
	//Count the length of given number
	def digit_length(value: Int): Int = {
		if (value < 0) {
			return 0;
		}
		//Set the initial value of length is zero
		var length: Int = 0;
		var number: Int = value;
		while (number != 0) {
			//update length
			length += 1;
			//Remove last digit
			number /= 10;
		}
		return length;
	}
	def is_pure_no(number: Int): Unit = {
		//Test case
		//Given digits length are Even? 
		//Given number is combination of 4 or 5?
		//Number is form of palindrome?

		if (this.digit_length(number) % 2 == 0 && this.check_digit(number) == true && number == this.reverse(number)) {
			print(s"$number is pure no\n");
		} else {
			print(s"$number is not a pure no\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyNumber = new MyNumber();
		//Test case
		obj.is_pure_no(4554);
		obj.is_pure_no(444444);
		obj.is_pure_no(445454);
		obj.is_pure_no(555);
	}
}

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
  Swift 4 Program

  Check if a given number is pure number
*/
class MyNumber {
	//Reverse a given number
	func reverse(_ value: Int) -> Int {
		var result: Int = 0;
       var number: Int = value;
		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result * 10) + (number % 10);
			}
			number /= 10;
		}
		return result;
	}
	//Check given digit is combination of 4 and 5
	func check_digit(_ value: Int) -> Bool {
		var number: Int = value;
        if (number < 0) {
			return false;
		}
		var status: Bool = true;
		while (number != 0) {
			//check if a given number is combination of 4 or 5

			if (number % 10 >= 4 && number % 10 <= 5) {
				number /= 10;
			} else {
				//When number is not a combination of 4 or 5
				status = false;
				break;
			}
		}
		return status;
	}
	//Count the length of given number
	func digit_length(_ value: Int) -> Int {
        var number: Int = value;
		if (number < 0) {
			return 0;
		}
		//Set the initial value of length is zero
		var length: Int = 0;
		while (number != 0) {
			//update length
			length += 1;
			//Remove last digit
			number /= 10;
		}
		return length;
	}
	func is_pure_no(_ number: Int) {
		//Test case
		//Given digits length are Even? 
		//Given number is combination of 4 or 5?
		//Number is form of palindrome?

		if (self.digit_length(number) % 2 == 0 && self.check_digit(number) == true && number == self.reverse(number)) {
			print(number ," is pure no");
		} else {
			print(number ," is not a pure no");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test case
	obj.is_pure_no(4554);
	obj.is_pure_no(444444);
	obj.is_pure_no(445454);
	obj.is_pure_no(555);
}
main();

Output

4554  is pure no
444444  is pure no
445454  is not a pure no
555  is not a pure no
<?php
/*
  Php Program
  Check if a given number is pure number
*/
class MyNumber {
	//Reverse a given number

	public 	function reverse($number) {
		$result = 0;
		while ($number > 0) {
			if ($result == 0) {
				$result = $number % 10;
			} else {
				$result = ($result *10) + ($number % 10);
			}
			$number = intval($number / 10);
		}
		return $result;
	}
	//Check given digit is combination of 4 and 5

	public 	function check_digit($number) {
		if ($number < 0) {
			return false;
		}
		$status = true;
		while ($number != 0) {
			//check if a given number is combination of 4 or 5

			if ($number % 10 >= 4 && $number % 10 <= 5) {
				$number = intval($number / 10);
			} else {
				//When number is not a combination of 4 or 5
				$status = false;
				break;
			}
		}
		return $status;
	}
	//Count the length of given number

	public 	function digit_length($number) {
		if ($number < 0) {
			return 0;
		}
		//Set the initial value of length is zero
		$length = 0;
		while ($number != 0) {
			//update length
			$length++;
			//Remove last digit
			$number = intval($number / 10);
		}
		return $length;
	}
	public 	function is_pure_no($number) {
		//Test case
		//Given digits length are Even ? 
		//Given number is combination of 4 or 5 ?
		//Number is form of palindrome ?

		if ($this->digit_length($number) % 2 == 0 && $this->check_digit($number) == true && $number == $this->reverse($number)) {
			echo($number ." is pure no\n");
		} else {
			echo($number ." is not a pure no\n");
		}
	}
};
function main() {
	$obj = new MyNumber();
	//Test case

	$obj->is_pure_no(4554);
	$obj->is_pure_no(444444);
	$obj->is_pure_no(445454);
	$obj->is_pure_no(555);
}
main();

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no
/*
 Node Js Program

 Check if a given number is pure number
*/
class MyNumber {
	//Reverse a given number
	reverse(number) {
		var result = 0;
		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result *10) + (number % 10);
			}
			number = parseInt(number / 10);
		}
		return result;
	}
	//Check given digit is combination of 4 and 5
	check_digit(number) {
		if (number < 0) {
			return false;
		}
		var status = true;
		while (number != 0) {
			//check if a given number is combination of 4 or 5

			if (number % 10 >= 4 && number % 10 <= 5) {
				
                number = parseInt(number / 10);
			} else {
				//When number is not a combination of 4 or 5
				status = false;
				break;
			}
		}
		return status;
	}
	//Count the length of given number
	digit_length(number) {
		if (number < 0) {
			return 0;
		}
		//Set the initial value of length is zero
		var length = 0;
		while (number != 0) {
			//update length
			length++;
			//Remove last digit
			number = parseInt(number / 10);
		}
		return length;
	}
	is_pure_no(number) {
		//Test case
		//Given digits length are Even ? 
		//Given number is combination of 4 or 5 ?
		//Number is form of palindrome ?

		if (this.digit_length(number) % 2 == 0 && this.check_digit(number) == true && number == this.reverse(number)) {
			process.stdout.write(number + " is pure no\n");
		} else {
			process.stdout.write(number + " is not a pure no\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test case
	obj.is_pure_no(4554);
	obj.is_pure_no(444444);
	obj.is_pure_no(445454);
	obj.is_pure_no(555)
}
main();

Output

4554 is pure no
444444 is pure no
445454 is not a pure no
555 is not a pure no


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