Wap to enter a number and check if it is a kaprekar number or not
Using java and function parameter
Urgent
Answers
Kaprekar Number:
A number is said to be Kaprekar number when the number is having even digits,then sum of even half's of square of the number is equal to that number.
Program:
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String[] args) {
System.out.println("enter a number");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int val = check(num);
if(val == num){
System.out.println(num+" is a Kaprekar number");
}else{
System.out.println(num+" is not a Kaprekar number");
}
}
public static int check(int number){
int i,j,temp = number * number;
String h1 = "" ,h2 = "" ,int_st = Integer.toString(temp);
int len = int_st.length(),half = len/2;
for( i=(half-1),j=(len-1) ; i>=0 && j>=half ; i--,j--){
h1 = (int_st. charAt(i)) + h1;
h2 = (int_st. charAt(j)) + h2;
}
int tot = (Integer.parseInt(h1) + Integer.parseInt(h2));
return tot;
}
}
Output-1:
enter a number : 45
45 is Kaprekar number
Output-2:
enter a number : 46
46 is not a Kaprekar number
Explanation:
- i took number into int num variable and passed it to function check()
- As you said i used a function with a parameter of given number
- first i squared the number and stored it in int temp variable
- so i want to divide the number into 2 half's so i turned the temp number into string and then calculated the string length and then divided the string into 2 half using for loop
- in for loop first half of the number is stored in h1 and other half of the number is stored in h2
- after there are strings so i converted them into integers using java predefined method Integer.parseInt() which takes string as parameter and return the integer value
- so i returned that value to the main function
- in main i took the returned value and checked if the given number is equal to the returned value
- if true the given number is a kaprekar number
- if not then it is not a kaprekar number
----Hope you understood my logic behind the program,if you like mark as brainliest.in future if you want any help regarding programming or coding feel free to approach me, so that i can help you :)
Definition of Kaprekar Number
A number is said to be Kaprekar number if its sum of digits in its square is the number itself,if it's no. of digits of square of number is even then divide it by half,if odd make the right side part greater.
import java.util.Scanner;
class Kaprekar
{
public int kaprekar(int n1)
{
int square,t=0,m=0,l,sum=0;
square=(int)Math.pow(n1,2);
String a,f="",d="";
a=Integer.toString(square);
l=a.length();
f=a.substring(0,l/2);
d=a.substring(l/2);
t=Integer.parseInt(f);
m=Integer.parseInt(d);
sum=t+m;
return(sum);
}
public static void main ( )
{
Scanner sc=new Scanner (System.in);
Kaprekar ob=new Kaprekar ();
int n,check=0;
System.out.println("Enter your number");
n=sc.nextInt();
check=ob.kaprekar(n);
if(check==n)
System.out.println("Kaprekar Number");
else
System.out.println("Not a Kaprekar Number");
}
}
Hope my friend Sushmita, you are satisfied with the answer.
Please Compile the Program .
If any query then submit in Comments,I will surely tell you.
If your program runs smoothly and you get your doubt resolved,then only Mark me Brainlest,if you like.