Knowee
Questions
Features
Study Tools

Write a program that checks whether a given integer is a Kaprekar number or not. A Kaprekar number is a number whose square when divided into two parts and such that the sum of the parts is equal to the original number and none of the parts has value 0. Your task is to determine if the given input integer is a Kaprekar number.Examples:9 is a Kaprekar number since 92 = 81 and 8+1 = 9.92 is not a Kaprekar number since 922 = 8464 and 84 + 64 = 148.Input format :The input consists of a single integer, n.Output format :The first line of output prints "Sum: " followed by the sum of two parts of the square of n.The second line prints one of the following:If n is a Kaprekar number, print "[n] is a kaprekar number".If n is not a Kaprekar number, print "[n] is not a kaprekar number".Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 105Sample test cases :Input 1 :9Output 1 :Sum: 99 is a kaprekar numberInput 2 :92Output 2 :Sum: 14892 is not a kaprekar numberInput 3 :55Output 3 :Sum: 5555 is a kaprekar number

Question

Write a program that checks whether a given integer is a Kaprekar number or not. A Kaprekar number is a number whose square when divided into two parts and such that the sum of the parts is equal to the original number and none of the parts has value 0. Your task is to determine if the given input integer is a Kaprekar number.Examples:9 is a Kaprekar number since 92 = 81 and 8+1 = 9.92 is not a Kaprekar number since 922 = 8464 and 84 + 64 = 148.Input format :The input consists of a single integer, n.Output format :The first line of output prints "Sum: " followed by the sum of two parts of the square of n.The second line prints one of the following:If n is a Kaprekar number, print "[n] is a kaprekar number".If n is not a Kaprekar number, print "[n] is not a kaprekar number".Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 105Sample test cases :Input 1 :9Output 1 :Sum: 99 is a kaprekar numberInput 2 :92Output 2 :Sum: 14892 is not a kaprekar numberInput 3 :55Output 3 :Sum: 5555 is a kaprekar number

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that checks whether a given integer is a Kaprekar number or not:

def is_kaprekar(n):
    # Calculate the square of n
    sq_n = n * n
    # Convert the square to string for easy splitting
    str_sq_n = str(sq_n)
    # Calculate the length of the string
    len_str = len(str_sq_n)
    # Split the string into two parts
    for i in range(1, len_str):
        # Get the first and second parts
        first_part = int(str_sq_n[:i])
        second_part = int(str_sq_n[i:])
        # Check if the sum of the parts is equal to n and none of the parts has value 0
        if first_part > 0 and second_part > 0 and first_part + second_part == n:
            return True, first_part + second_part
    return False, None

# Read the input number
n = int(input())
# Check if the number is a Kaprekar number
is_kap, sum_parts = is_kaprekar(n)
# Print the result
if is_kap:
    print("Sum: ", sum_parts)
    print(n, "is a kaprekar number")
else:
    print("Sum: ", sum_parts)
    print(n, "is not a kaprekar number")

This program works by first calculating the square of the input number n. It then converts this square into a string and splits it into two parts. It checks if the sum of these two parts is equal to n and if none of the parts has value 0. If these conditions are met, it prints that n is a Kaprekar number. Otherwise, it prints that n is not a Kaprekar number.

This problem has been solved

Similar Questions

Problem StatementMisha wants to check and print whether the given number N is a neon number or not. Write a program that uses a function and helps her to complete this task.A neon number is a number where the sum of digits of the square of the number is equal to the number. Function Specifications: int neon(int n)Example 1Input:9Output:9 is a neon numberExplanation:92 = 81, and the sum of its digits (8+1) is equal to the original number (9).Example 2Input:12Output:12 is not a neon numberExplanation:(12)2=144, and the sum of its digits (1+4+4) is not equal to the original number (12).Note: This question helps in clearing technical coding tests for companies like Capgemini.Input format :The input consists of an integer, N.Output format :If N is a neon number, print "[N] is a neon number"Otherwise, print "[N] is not a neon number"

Problem Statement:Given a number, check whether it is a perfect square or not.Input Format:Given an integer n.Output Format:Print Yes if it is a perfect square or No if it is not.Constraints1 <= n <= 10^6Sample Input 1:100Sample Output 1:YesSample Input 2:34Sample Output 2:No

Write a program to check the given number is perfect or not ?

Help Alex, a new programmer, create a straightforward program. Ask for a positive number, use a function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the first digit), and print the result in the main function.This tool aids Alex in spotting duck numbers, enhancing their programming journey.Note: This question helps in clearing technical coding tests for companies like Amazon and Adobe.Input format :The input consists of an integer n, representing the number to be checked.Output format :The output prints "The number is a duck number." if the entered number is a duck number or "The number is NOT a duck number." otherwise.

The provided code stub reads and integer, , from STDIN. For all non-negative integers , print .ExampleThe list of non-negative integers that are less than is . Print the square of each number on a separate line.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.