Showing posts with label Kaprekar Number. Show all posts
Showing posts with label Kaprekar Number. Show all posts

Kaprekar Number

 //Wap to accept a number and check it is a Kaprekar number or not.

//A number is said to be a Kaprekar, when a number whose square divide into two parts and the sum 

//of the parts is equal to the original number then it is called Kaprekar.

//ex- 45 --> 45^2 =2025 -->20+25=45

import java.util.*;

class kaprikar

{//class kaprikar opens

    public static void main(String[]args)

    {//main metthod opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no//45

        int sq=n*n;//variable sq to store the square of the no//45x45=2025

        int c=0;//variable c to store the no of digits

        int copy=sq;//variable copy to store the square of the no//2025

        while(sq>0)//2025>0 t//202>0 t//20>0 t//2>0 t//0>0 false

        {

            c=c+1;//0+1=1//1+1=2//2+1=3//3+1=4

            sq=sq/10;//2025/10=202//202/10=20//20/10=2//2/10=0

        }

        int mid=c/2;//variable mid to half the no of digits//4/2=2

        int k=(int)Math.pow(10,mid);//varible k to store the divisor//100

        int f=copy%k;//variable f to store the last part of the square//2025%100=25

        int l=copy/k;//variable l to store the first part of the square//2025/100=20 

        int sum=f+l;//variable sum to store the sum //25+20=45

        if(sum==n)//45==45 true

        System.out.println("It is a kaprikar no");

        else

        System.out.println("It is not a kaprikar no");

    }//main method closes

}//class closes


Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...