Kaprekar Number

Here given code implementation process.

//C Program
//Check if a given number is kaprekar number or not
#include <stdio.h>
#include <math.h>

//Count the number of digits in number 
int digit_length(int number) 
{ 
  int size = 0; 

  while (number!=0) 
  { 
    number/=10; 

    size++; 
  } 
  return size; 
}

// Check whether given number is kaprekar number or not
int is_kaprekar(int number) 
{ 
  if (number == 1) {
    return 1; 
  }

  // Calculate square of given number
  int square = number * number;

  // Get the length of digits in given number square
  int size = digit_length(square); 

  int sum = 0;

  int parts = 0;

  for (int digit = 1; digit < size; ++digit) 
  { 
    parts = pow(10, digit); 

    if (parts != number) 
    {
      // Combine parts of square numbers
      sum = ( square / parts ) + ( square % parts ); 

      if (sum == number) 
      {
        //When square digit sum is equal to number
        return 1; 
      }
    }
  } 

  return 0; 
} 

void kaprekar(int number) 
{
  if(is_kaprekar(number) == 1)
  {
    //When Yes
    printf("%d Is an kaprekar number\n",number);
  } 
  else
  {
    //When No
    printf("%d Is not a kaprekar number\n",number);
  }
}
int main() {
  //Test Cases
  kaprekar(55);
  kaprekar(95);
  kaprekar(77);
  kaprekar(99);

  return 0;
}

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
 C++ Program
 Check if a given number is kaprekar number or not
*/
#include<iostream>
#include <math.h>
using namespace std;

class MyNumber {
	public:

		//Count the number of digits in number 
		int digit_length(int number) {
			int size = 0;
			while (number != 0) {
				number /= 10;
				size++;
			}
			return size;
		}
	// Check whether given number is kaprekar number or not
	bool is_kaprekar(int number) {
		if (number == 1) {
			return true;
		}
		// Calculate square of given number
		int square = number *number;
		// Get the length of digits in given number square
		int size = this->digit_length(square);
		int sum = 0;
		int parts = 0;
		for (int digit = 1; digit < size; ++digit) {
			parts = int(pow(10, digit));
			if (parts != number) {
				// Combine parts of square numbers
				sum = (square / parts) + (square % parts);
				if (sum == number) {
					return true;
				}
			}
		}
		return false;
	}
	void kaprekar(int number) {
		if (this->is_kaprekar(number) == true) {
			//When Yes

			cout << number << " Is an kaprekar number\n";
		} else {
			//When No

			cout << number << " Is not a kaprekar number\n";
		}
	}
};
int main() {
	MyNumber obj ;
	//Test Cases
	obj.kaprekar(55);
	obj.kaprekar(95);
	obj.kaprekar(77);
	obj.kaprekar(99);
	return 0;
}

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
  Java Program
  Check if a given number is kaprekar number or not
*/

public class MyNumber {

  //Count the number of digits in number 
  public int digit_length(int number) 
  { 
    int size = 0; 

    while (number!=0) 
    { 
      number/=10; 

      size++; 
    } 
    return size; 
  }

  // Check whether given number is kaprekar number or not
  public boolean is_kaprekar(int number) 
  { 
    if (number == 1) {
      return true; 
    }

    // Calculate square of given number
    int square = number * number;

    // Get the length of digits in given number square
    int size = digit_length(square); 

    int sum = 0;

    int parts = 0;

    for (int digit = 1; digit < size; ++digit) 
    { 
      parts = (int)Math.pow(10, digit); 

      if (parts != number) 
      {
        // Combine parts of square numbers
        sum = ( square / parts ) + ( square % parts ); 

        if (sum == number) 
        {
          //When square digit sum is equal to number
          return true; 
        }
      }
    } 

    return false; 
  } 

  public void kaprekar(int number) 
  {
    if(is_kaprekar(number) == true)
    {
      //When Yes
      System.out.print(number+" Is an kaprekar number\n");
    } 
    else
    {
      //When No
      System.out.print(number+" Is not a kaprekar number\n");
    }
  }
 
  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Cases
    obj.kaprekar(55);
    obj.kaprekar(95);
    obj.kaprekar(77);
    obj.kaprekar(99);

  }
}

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
  C# Program
  Check if a given number is kaprekar number or not
*/
using System;
public class MyNumber {

	//Count the number of digits in number 
	public int digit_length(int number) {
		int size = 0;

		while (number != 0) {
			number /= 10;

			size++;
		}
		return size;
	}

	// Check whether given number is kaprekar number or not
	public Boolean is_kaprekar(int number) {
		if (number == 1) {
			return true;
		}

		// Calculate square of given number
		int square = number * number;

		// Get the.Length of digits in given number square
		int size = digit_length(square);

		int sum = 0;

		int parts = 0;

		for (int digit = 1; digit < size; ++digit) {
			parts = (int) Math.Pow(10, digit);

			if (parts != number) {
				// Combine parts of square numbers
				sum = (square / parts) + (square % parts);

				if (sum == number) {
					//When square digit sum is equal to number
					return true;
				}
			}
		}

		return false;
	}

	public void kaprekar(int number) {
		if (is_kaprekar(number) == true) {
			//When Yes
			Console.Write(number + " Is an kaprekar number\n");
		} else {
			//When No
			Console.Write(number + " Is not a kaprekar number\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Cases
		obj.kaprekar(55);
		obj.kaprekar(95);
		obj.kaprekar(77);
		obj.kaprekar(99);

	}
}

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
# Python 3 Program
# Check if a given number is kaprekar number or not
class MyNumber :
	# Count the number of digits in number 
	def digit_length(self, number) :
		size = 0
		while (number != 0) :
			number = (number / 10)
			size += 1
		
		return size
	
	#  Check whether given number is kaprekar number or not
	def is_kaprekar(self, number) :
		if (number == 1) :
			return True
		
		#  Calculate square of given number
		square = number * number
		#  Get the length of digits in given number square
		size = self.digit_length(square)
		sum = 0
		parts = 0
		digit = 1
		while (digit < size) :
			parts = 10**digit
			if (parts != number) :
				#  Combine parts of square numbers
				sum = int(square / parts) + (square % parts)
				if (sum == number) :
					return True
				
			
			digit += 1
		
		return False
	
	def kaprekar(self, number) :
		if (self.is_kaprekar(number) == True) :
			print(number ," Is an kaprekar number")
		else :
			print(number ," Is not a kaprekar number")
		
	

def main() :
	obj = MyNumber()
	obj.kaprekar(55)
	obj.kaprekar(95)
	obj.kaprekar(77)
	obj.kaprekar(99)


if __name__ == "__main__":
	main()

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
# Ruby Program 
# Check if a given number is kaprekar number or not
class MyNumber 
	# Count the number of digits in number 
	def digit_length(number) 
		size = 0
		while (number != 0) 
			number /= 10
			size += 1
		end
		return size
	end
	#  Check whether given number is kaprekar number or not
	def is_kaprekar(number) 
		if (number == 1) 
			return true
		end
		#  Calculate square of given number
		square = number * number
		#  Get the length of digits in given number square
		size = self.digit_length(square)
		sum = 0
		parts = 0
		digit = 1
		while (digit < size) 
			parts = 10**digit
			if (parts != number) 
				#  Combine parts of square numbers
				sum = (square / parts) + (square % parts)
				if (sum == number) 
					return true
				end
			end
			digit += 1
		end
		return false
	end
	def kaprekar(number) 
		if (self.is_kaprekar(number) == true) 
			print(number ," Is an kaprekar number\n")
		else 
			print(number ," Is not a kaprekar number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.kaprekar(55)
	obj.kaprekar(95)
	obj.kaprekar(77)
	obj.kaprekar(99)
end
main()

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
 Scala Program
  Check if a given number is kaprekar number or not
*/
class MyNumber {
	//Count the number of digits in number 
	def digit_length(value : Int): Int = {
		var size: Int = 0;
        var number: Int = value;
		while (number != 0) {
			number = (number / 10).toInt;
			size += 1;
		}
		return size;
	}
	// Check whether given number is kaprekar number or not
	def is_kaprekar(number: Int): Boolean = {
		if (number == 1) {
			return true;
		}
		// Calculate square of given number
		var square: Int = number * number;
		// Get the length of digits in given number square
		var size: Int = this.digit_length(square);
		var sum: Int = 0;
		var parts: Int = 0;
		var digit: Int = 1;
		while (digit < size) {
			parts = (scala.math.pow(10, digit)).toInt
			if (parts != number) {
				// Combine parts of square numbers
				sum = (square / parts).toInt + (square % parts);
				if (sum == number) {
					return true;
				}
			}
			digit += 1;
		}
		return false;
	}
	def kaprekar(number: Int): Unit = {
		if (this.is_kaprekar(number) == true) {
			print(s"$number Is an kaprekar number\n");
		} else {
			print(s"$number Is not a kaprekar number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.kaprekar(55);
        obj.kaprekar(95);
        obj.kaprekar(77);
        obj.kaprekar(99);
	}
}

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
  Swift 4 Program
  Check if a given number is kaprekar number or not
*/
import Foundation
class MyNumber {
	//Count the number of digits in number 
	func digit_length(_ value : Int) -> Int {
		var size: Int = 0;
        var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	// Check whether given number is kaprekar number or not
	func is_kaprekar(_ number: Int) -> Bool {
		if (number == 1) {
			return true;
		}
		// Calculate square of given number
		let square: Int = number * number;
		// Get the length of digits in given number square
		let size: Int = self.digit_length(square);
		var sum: Int = 0;
		var parts: Int = 0;
		var digit: Int = 1;
		while (digit < size) {
			parts = Int(pow(Double(10), Double(digit)));
			if (parts != number) {
				// Combine parts of square numbers
				sum = (square / parts) + (square % parts);
				if (sum == number) {
					return true;
				}
			}
			digit += 1;
		}
		return false;
	}
	func kaprekar(_ number: Int) {
		if (self.is_kaprekar(number) == true) {
			print(number ," Is an kaprekar number");
		} else {
			print(number ," Is not a kaprekar number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.kaprekar(55);
	obj.kaprekar(95);
	obj.kaprekar(77);
	obj.kaprekar(99);
}
main();

Output

55  Is an kaprekar number
95  Is not a kaprekar number
77  Is not a kaprekar number
99  Is an kaprekar number
<?php
/*
  Php Program
  Check if a given number is kaprekar number or not
*/
class MyNumber {
	//Count the number of digits in number 

	public 	function digit_length($number) {
		$size = 0;
		while ($number != 0) {
			$number = intval($number/10);
			$size++;
		}
		return $size;
	}
	// Check whether given number is kaprekar number or not

	public 	function is_kaprekar($number) {
		if ($number == 1) {
			return true;
		}
		// Calculate square of given number
		$square = $number *$number;
		// Get the length of digits in given number square
		$size = $this->digit_length($square);
		$sum = 0;
		$parts = 0;
		for ($digit = 1; $digit < $size; ++$digit) {
			$parts = pow(10, $digit);
			if ($parts != $number) {
				// Combine parts of square numbers
				$sum = intval(($square / $parts)) + ($square % $parts);
				if ($sum == $number) {
					return true;
				}
			}
		}
		return false;
	}
	public 	function kaprekar($number) {
		if ($this->is_kaprekar($number) == true) {
			//When Yes

			echo($number ." Is an kaprekar number\n");
		} else {
			//When No

			echo($number ." Is not a kaprekar number\n");
		}
	}
};

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

	$obj->kaprekar(55);
	$obj->kaprekar(95);
	$obj->kaprekar(77);
	$obj->kaprekar(99);
}
main();

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar number
/*
 Node Js Program
 Check if a given number is kaprekar number or not
*/
class MyNumber {
	//Count the number of digits in number 
	digit_length(number) {
		var size = 0;
		while (number != 0) {
			number = parseInt(number/ 10);
			size++;
		}
		return size;
	}
	// Check whether given number is kaprekar number or not
	is_kaprekar(number) {
		if (number == 1) {
			return true;
		}
		// Calculate square of given number
		var square = number *number;
		// Get the length of digits in given number square
		var size = this.digit_length(square);
		var sum = 0;
		var parts = 0;
		for (var digit = 1; digit < size; ++digit) {
			parts = parseInt(Math.pow(10, digit));
			if (parts != number) {
				// Combine parts of square numbers
				sum = parseInt(square / parts) + (square % parts);
				if (sum == number) {
					return true;
				}
			}
		}
		return false;
	}
	kaprekar(number) {
		if (this.is_kaprekar(number) == true) {
			//When Yes

			process.stdout.write(number + " Is an kaprekar number\n");
		} else {
			//When No

			process.stdout.write(number + " Is not a kaprekar number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Cases
	obj.kaprekar(55);
	obj.kaprekar(95);
	obj.kaprekar(77);
	obj.kaprekar(99)
}
main();

Output

55 Is an kaprekar number
95 Is not a kaprekar number
77 Is not a kaprekar number
99 Is an kaprekar 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