Posted on by Kalkicode
Code Number

# Check if a given number is pure number

A pure number is a number that satisfies the following three conditions:

1. The number has an even number of digits.
2. All its digits are either 4 or 5.
3. The number remains the same when its digits are reversed.

In this article, we will discuss how to determine whether a given number is a pure number or not. We will provide an explanation of the problem statement, along with a suitable example. Then, we will describe an algorithm to solve the problem and present the pseudocode. Finally, we will explain the output of the provided code along with its time complexity.

## Problem Statement

The task is to check whether a given number is a pure number or not. A pure number must have an even number of digits and consist only of the digits 4 and 5. Additionally, the number should remain the same when its digits are reversed.

### Example

Let's consider the number 4554 as an example. It has an even number of digits (4 digits), and all its digits (4, 5, 5, and 4) are either 4 or 5. When we reverse the number, we obtain 4554 again. Therefore, 4554 is a pure number.

## Algorithm

1. Define a function named "reverse" that takes a number as input and returns the reversed number.
1. Initialize a variable named "result" to 0.
2. While the number is greater than 0, perform the following steps:
1. If "result" is equal to 0, assign it the value of the last digit of the number.
2. Otherwise, multiply "result" by 10 and add the last digit of the number.
3. Divide the number by 10 to remove the last digit.
3. Return "result" as the reversed number.
2. Define a function named "check_digit" that takes a number as input and checks whether its digits are only 4 or 5.
1. If the number is less than 0, return 0.
2. Initialize a variable named "status" to 1.
3. While the number is not equal to 0, perform the following steps:
1. If the last digit of the number is between 4 and 5 (inclusive), divide the number by 10.
2. Otherwise, set "status" to 0 and break out of the loop.
4. Return "status" as the result of checking the digits.
3. Define a function named "digit_length" that takes a number as input and returns its length (number of digits).
1. If the number is less than 0, return 0.
2. Initialize a variable named "length" to 0.
3. While the number is not equal to 0, perform the following steps:
1. Increment "length" by 1.
2. Divide the number by 10 to remove the last digit.
4. Return "length" as the length of the number.
4. Define a function named "is_pure_no" that takes a number as input and checks whether it is a pure number.
1. Check the following conditions:
1. If the length of the number is even and all its digits are between 4 and 5 (inclusive) and the number is equal to its reverse, print that the number is a pure number.
2. Otherwise, print that the number is not a pure number.
5. In the main function:
1. Call the "is_pure_no" function with different test cases to check if the implementation is correct.

## Pseudocode

``````reverse(number):
result = 0
while number > 0:
if result == 0:
result = number % 10
else:
result = (result * 10) + (number % 10)
number = number / 10
return result

check_digit(number):
if number < 0:
return 0
status = 1
while number != 0:
if 4 <= (number % 10) <= 5:
number = number / 10
else:
status = 0
break
return status

digit_length(number):
if number < 0:
return 0
length = 0
while number != 0:
length = length + 1
number = number / 10
return length

is_pure_no(number):
if digit_length(number) % 2 == 0 and check_digit(number) == 1 and number == reverse(number):
print number, "is a pure number"
else:
print number, "is not a pure number"

main():
is_pure_no(4554)
is_pure_no(444444)
is_pure_no(445454)
is_pure_no(555)
``````

## Code Solution

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``````

## Output Explanation

The provided code tests different numbers to determine if they are pure numbers or not. The output of the code is as follows:

• 4554 is a pure number because it has an even number of digits, all of which are either 4 or 5, and it remains the same when its digits are reversed.
• 444444 is a pure number because it has an even number of digits, all of which are either 4 or 5, and it remains the same when its digits are reversed.
• 445454 is not a pure number because although it has an even number of digits and all its digits are either 4 or 5, it does not remain the same when its digits are reversed.
• 555 is not a pure number because it has an odd number of digits.

## Time Complexity

The time complexity of the code is dependent on the number of digits in the given number. Let's assume the number has n digits.

The "reverse" function iterates through each digit of the number, which takes O(n) time.

The "check_digit" function also iterates through each digit of the number, which takes O(n) time.

## Comment

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.

Categories
Relative Post