Lychrel number

Here given code implementation process.

//C Program
//Check Lychrel number
#include <stdio.h>

//Reverse the given number
long reverse(long number)
{

  long result = 0;

  while(number>0)
  {
    if(result==0)
    {
      result=number%10;
    }
    else
    {
      result=(result*10)+(number%10);
    }
    number/=10;
  }
  return result;
}


int is_palindrome(long number) 
{ 
  //Check if a reversal form of given number are equal or not ?
  if(number == reverse(number))
  {
    //when yes
    return 1;
  }
  else
  {
    //when no
    return 0;
  }

}
// This function are determine whether given number is lychrel or not
int is_lychrel(long number) 
{ 
  //use to control execution 
  int limit = 25;

  for (int i = 0; i < limit; i++) 
  { 
    number = number + reverse(number); 

    if (is_palindrome(number)==1) 
    { //When result is form of palindrome
      return 0; 
    }
  } 

  return 1; 
} 
// This method are used to handle request of lychrel number
// Basically this are displaying status of lychrel number
void lychrel(long number)
{
  if(is_lychrel(number)==1)
  {
    printf("%ld is lychrel number\n",number );
  }
  else
  {
    printf("%ld is not lychrel number\n", number);
  }
}



int main() {

  // Test Case
  lychrel(196);
  lychrel(57);
  lychrel(788);
  lychrel(56);
  lychrel(887);
  return 0;
}

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
 C++ Program
 Determine whether number is Lychrel number
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Reverse the given number
		long reverse(long number) {
			long result = 0;
			while (number > 0) {
				if (result == 0) {
					result = number % 10;
				} else {
					result = (result *10) + (number % 10);
				}
				number /= 10;
			}
			return result;
		}
	bool is_palindrome(long number) {
		//Check if a reversal form of given number are equal or not ?

		if (number == this->reverse(number)) {
			return true;
		} else {
			return false;
		}
	}
	// This function are determine whether given number is lychrel or not
	bool is_lychrel(long number) {
		//use to control execution 
		int limit = 25;
		for (int i = 0; i < limit; i++) {
			number = number + this->reverse(number);
			if (this->is_palindrome(number) == true) {
				return false;
			}
		}
		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number
	void lychrel(long number) {
		if (this->is_lychrel(number) == true) {
			cout << number << " is lychrel number\n";
		} else {
			cout << number << " is not lychrel number\n";
		}
	}
};
int main() {
	MyNumber obj ;
	// Test Case
	obj.lychrel(196);
	obj.lychrel(57);
	obj.lychrel(788);
	obj.lychrel(56);
	obj.lychrel(887);
	return 0;
}

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
  Java Program
  Determine whether number is Lychrel number
*/

public class MyNumber {

  //Reverse the given number
  public long reverse(long number)
  {

    long result = 0;

    while(number>0)
    {
      if(result==0)
      {
        result=number%10;
      }
      else
      {
        result=(result*10)+(number%10);
      }
      number/=10;
    }
    return result;
  }


  public boolean is_palindrome(long number) 
  { 
    //Check if a reversal form of given number are equal or not ?
    if(number == reverse(number))
    {
      //when yes
      return true;
    }
    else
    {
      //when no
      return false;
    }

  }
  // This function are determine whether given number is lychrel or not
  public boolean is_lychrel(long number) 
  { 
    //use to control execution 
    int limit = 25;

    for (int i = 0; i < limit; i++) 
    { 
      number = number + reverse(number); 

      if (is_palindrome(number) == true) 
      { //When result is form of palindrome
        return false; 
      }
    } 

    return true; 
  } 
  // This method are used to handle request of lychrel number
  // Basically this are displaying status of lychrel number
  public void lychrel(long number)
  {
    if(is_lychrel(number)==true)
    {
      System.out.print(number+" is lychrel number\n" );
    }
    else
    {
      System.out.print(number+" is not lychrel number\n" );
    }
  }



  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
      // Test Case
      obj.lychrel(196);
      obj.lychrel(57);
      obj.lychrel(788);
      obj.lychrel(56);
      obj.lychrel(887);
  }
}

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
  C# Program
  Determine whether number is Lychrel number
*/
using System;
public class MyNumber {

	//Reverse the given number
	public long reverse(long number) {

		long result = 0;

		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result * 10) + (number % 10);
			}
			number /= 10;
		}
		return result;
	}


	public Boolean is_palindrome(long number) {
		//Check if a reversal form of given number are equal or not ?
		if (number == reverse(number)) {
			//when yes
			return true;
		} else {
			//when no
			return false;
		}

	}
	// This function are determine whether given number is lychrel or not
	public Boolean is_lychrel(long number) {
		//use to control execution 
		int limit = 25;

		for (int i = 0; i < limit; i++) {
			number = number + reverse(number);

			if (is_palindrome(number) == true) { //When result is form of palindrome
				return false;
			}
		}

		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number
	public void lychrel(long number) {
		if (is_lychrel(number) == true) {
			Console.Write(number + " is lychrel number\n");
		} else {
			Console.Write(number + " is not lychrel number\n");
		}
	}



	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		// Test Case
		obj.lychrel(196);
		obj.lychrel(57);
		obj.lychrel(788);
		obj.lychrel(56);
		obj.lychrel(887);
	}
}

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
# Python 3 Program
# Determine whether number is Lychrel number
class MyNumber :
	#Reverse the 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
	
	def is_palindrome(self, number) :
		#Check if a reversal form of given number are equal or not ?

		if (number == self.reverse(number)) :
			return True
		else :
			return False
		
	
	# This function are determine whether given number is lychrel or not
	def is_lychrel(self, number) :
		limit = 25
		i = 0
		while (i < limit) :
			number = number + self.reverse(number)
			if (self.is_palindrome(number) == True) :
				return False
			
			i += 1
		
		return True
	
	# Basically this are displaying status of lychrel number

	# This method are used to handle request of lychrel number
	def lychrel(self, number) :
		if (self.is_lychrel(number) == True) :
			print(number ," is lychrel number")
		else :
			print(number ," is not lychrel number")
		
	

def main() :
	obj = MyNumber()
	#Test Case
	obj.lychrel(196)
	obj.lychrel(57)
	obj.lychrel(788)
	obj.lychrel(56)
	obj.lychrel(887)


if __name__ == "__main__":
	main()

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
# Ruby Program
# Determine whether number is Lychrel number
class MyNumber 
	#Reverse the given number
	def reverse(number) 
		result = 0
		while (number > 0) 
			if (result == 0) 
				result = number % 10
			else 
				result = (result * 10) + (number % 10)
			end
			number = (number / 10).to_i 
		end
		return result
	end
	def is_palindrome(number) 
		#Check if a reversal form of given number are equal or not ?

		if (number == self.reverse(number)) 
			return true
		else 
			return false
		end
	end
	# This function are determine whether given number is lychrel or not
	def is_lychrel(number) 
		limit = 25
		i = 0
		while (i < limit) 
			number = number + self.reverse(number)
			if (self.is_palindrome(number) == true) 
				return false
			end
			i += 1
		end
		return true
	end
	# Basically this are displaying status of lychrel number

	# This method are used to handle request of lychrel number
	def lychrel(number) 
		if (self.is_lychrel(number) == true) 
			print(number ," is lychrel number\n")
		else 
			print(number ," is not lychrel number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	#Test Case
	obj.lychrel(196)
	obj.lychrel(57)
	obj.lychrel(788)
	obj.lychrel(56)
	obj.lychrel(887)
end
main()

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
 Scala Program
 Determine whether number is Lychrel number
*/
class MyNumber {
	//Reverse the given number
	def reverse(num : Long): Long = {
		var result: Long = 0;
      	var number: Long = num;
		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result * 10) + (number % 10);
			}
			number = number/10;
		}
		return result;
	}
	def is_palindrome(number: Long): Boolean = {
		//Check if a reversal form of given number are equal or not?

		if (number == this.reverse(number)) {
			return true;
		} else {
			return false;
		}
	}
	// This function are determine whether given number is lychrel or not
	def is_lychrel(num : Long): Boolean = {
		var limit: Int = 25;
		var i: Int = 0;
     	var number: Long = num;
		while (i < limit) {
			number = number + this.reverse(number);
			if (this.is_palindrome(number) == true) {
				return false;
			}
			i += 1;
		}
		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number
	def lychrel(number: Long): Unit = {
		if (this.is_lychrel(number) == true) {
			print( s"$number is lychrel number\n");
		} else {
			print( s"$number is not lychrel number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.lychrel(196);
        obj.lychrel(57);
        obj.lychrel(788);
        obj.lychrel(56);
        obj.lychrel(887);
	}
}

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
  Swift 4 Program
  Determine whether number is Lychrel number
*/
class MyNumber {
	//Reverse the given number
	func reverse(_ num: Int) -> Int {
		var result: Int = 0;
       var number : Int = num;
		while (number > 0) {
			if (result == 0) {
				result = number % 10;
			} else {
				result = (result * 10) + (number % 10);
			}
			number /= 10;
		}
		return result;
	}
	func is_palindrome(_ number: Int) -> Bool {
		//Check if a reversal form of given number are equal or not?

		if (number == self.reverse(number)) {
			return true;
		} else {
			return false;
		}
	}
	// This function are determine whether given number is lychrel or not
	func is_lychrel(_ num: Int) -> Bool {
		let limit : Int = 25;
        var number : Int = num;
		var i: Int = 0;
		while (i < limit) {
			number = number + self.reverse(number);
			if (self.is_palindrome(number) == true) {
				return false;
			}
			i += 1;
		}
		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number
	func lychrel(_ number: Int) {
		if (self.is_lychrel(number) == true) {
			print(number ," is lychrel number");
		} else {
			print(number ," is not lychrel number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.lychrel(196);
	obj.lychrel(57);
	obj.lychrel(788);
	obj.lychrel(56);
	obj.lychrel(887);
}
main();

Output

196  is lychrel number
57  is not lychrel number
788  is lychrel number
56  is not lychrel number
887  is lychrel number
<?php
/*
  Php Program
  Determine whether number is Lychrel number
*/
class MyNumber {
	//Reverse the 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;
	}
	public 	function is_palindrome($number) {
		//Check if a reversal form of given number are equal or not ?

		if ($number == $this->reverse($number)) {
			return true;
		} else {
			return false;
		}
	}
	// This function are determine whether given number is lychrel or not

	public 	function is_lychrel($number) {
		//use to control execution 
		$limit = 25;
		for ($i = 0; $i < $limit; $i++) {
			$number = $number + $this->reverse($number);
			if ($this->is_palindrome($number) == true) {
				return false;
			}
		}
		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number

	public 	function lychrel($number) {
		if ($this->is_lychrel($number) == true) {
			echo($number ." is lychrel number\n");
		} else {
			echo($number ." is not lychrel number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	// Test Case

	$obj->lychrel(196);
	$obj->lychrel(57);
	$obj->lychrel(788);
	$obj->lychrel(56);
	$obj->lychrel(887);
}
main();

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel number
/*
 Node Js Program
 Determine whether number is Lychrel number
*/
class MyNumber {
	//Reverse the 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;
	}
	is_palindrome(number) {
		//Check if a reversal form of given number are equal or not ?

		if (number == this.reverse(number)) {
			return true;
		} else {
			return false;
		}
	}
	// This function are determine whether given number is lychrel or not
	is_lychrel(number) {
		//use to control execution 
		var limit = 25;
		for (var i = 0; i < limit; i++) {
			number = number + this.reverse(number);
			if (this.is_palindrome(number) == true) {
				return false;
			}
		}
		return true;
	}
	// This method are used to handle request of lychrel number
	// Basically this are displaying status of lychrel number
	lychrel(number) {
		if (this.is_lychrel(number) == true) {
			process.stdout.write(number + " is lychrel number\n");
		} else {
			process.stdout.write(number + " is not lychrel number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	// Test Case
	obj.lychrel(196);
	obj.lychrel(57);
	obj.lychrel(788);
	obj.lychrel(56);
	obj.lychrel(887)
}
main();

Output

196 is lychrel number
57 is not lychrel number
788 is lychrel number
56 is not lychrel number
887 is lychrel 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