Computer Science, asked by sushmita1233, 11 months ago

Wap to enter a number and check if it is a kaprekar number or not

Using java and function parameter



Urgent​


Anonymous: hiii
rakeshchennupati143: there you go i wrote the program using function parameter
rakeshchennupati143: i gave you the best program i wrote and you chose the answer of some lunatic's program. and that says you are lunatic too, that's it i'm not gonna answer your questions any more ask your lunatic to answer he uses hi lunatic brain to answer,
rakeshchennupati143: a program without spaces without neat editing,such a lunatic persons you two are

Answers

Answered by rakeshchennupati143
3

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:

  1. i took number into int num variable and passed it to function check()
  2. As you said i used a function with a parameter of given number
  3. first i squared the number and stored it in int temp variable
  4. 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
  5. in for loop first half of the number is stored in h1 and other half of the number is stored in h2
  6. 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
  7. so i returned that value to the main function
  8. in main i took the returned value and checked if the given number is equal to the returned value
  9. if true the given number is a kaprekar number
  10. 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   :)


amannishad0512p5zxh6: You don't know
amannishad0512p5zxh6: when to use
Answered by amannishad0512p5zxh6
2

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.

Have a great day !


amannishad0512p5zxh6: See my code carefully then say
amannishad0512p5zxh6: check-up your eyes to a doctor
amannishad0512p5zxh6: Aur haa my code is absolutely right
amannishad0512p5zxh6: And you are frustrated as my friend
amannishad0512p5zxh6: Mark me
amannishad0512p5zxh6: brainly batch
amannishad0512p5zxh6: Be cool ,next time I request to my friend
amannishad0512p5zxh6: To mark give you
amannishad0512p5zxh6: Brainly batch
amannishad0512p5zxh6: Have a good day,friend
Similar questions