## Java Program to check Kaprekar number

Question. Write a Java Program to check Kaprekar number.
Kaprekar number are those number whose square, if split into parts and then add it, will be the same as the original number. It is named after Dattaraya Ramchandra Kaprekar was an Indian.
Say 45, If we square it we get 2025 that is 452 = 2025, and split it as 20 and 25 then add it, will get 45.

452 = 2025 then 20 + 25 = 45.

Java program as follows

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class KaprekarNum{

// main method begins execution of this class
public static void main(String args[]) throws IOException
{

int square, temp, counter = 0, reminder, quotient;
int n;   // The number to find Kaprekar Number

InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
System.out.print("Enter a number to check Kaprekar number: ");
// Waiting for the user input
n = Integer.parseInt(br.readLine()); // say 45
temp = n;            // Store n into temp variable
square = n * n;    // Square of 45 is 2025
// find total number of digits
while (n != 0) {
counter++;
n = n / 10;
}
// reminder for 2025 % 100 will be 25
reminder = square % ((int) Math.pow(10, counter));
// quotient for 2025 / 100 will be 20
quotient = square / ((int) Math.pow(10, counter));
/* checking the Kaprekar number after adding of reminder and quotient
with the original number */
if ((reminder + quotient) == temp)
{
System.out.println(temp + " is a kaprekar number ");
} else
{
System.out.println(temp + " is not a kaprekar number ");
}

} // end method main
} // end class

Output
Enter a number to check Kaprekar number: 45
45 is a kaprekar number

Enter a number to check Kaprekar number: 44
44 is not a kaprekar number

## Basic Features of java Basic Features  of  java

What is Java?
Java is an object-oriented programming language built by James Gosling and his colleagues at Sun MicroSystems. Now Java is maintained by Oracle Corporation. Oak was the first name of Java.  It was designed to have the "look and feel" of the C++ language, but it is simpler to use than C++ and enforces an object-oriented programming model.
What are the Basic Features of java?
Java is an object-oriented language.
Java is Easy to write and more readable and eye-catching.
Java program cannot harm other systems thus making it secure.
Java programs can be run on any platform (Linux, Window, Mac)
Java program both compiled and interpreted
Java provides integrated support for multithreaded programming.It can handle many tasks simultaneously.
Java was designed with a distributed environment.
Java has a strong memory allocation and automatic garbage collection mechanism.

Java uses 16-bit Unicode, instead of 8-bit ASCII code.

What are the advantages of OOP?
Easy to relate to the real world.
Data is protected from misuse.
All data is hidden only the method is visible.
We can reuse the code with the help of the Inheritance concept.
Secured
Easy to maintain

What are the Basic components of OOP?
1.    Class—It is a blueprint of an object.
2.    Object—An instance of a class or It is a template of a class.
3.    Encapsulation—The wrapping up of data and method into a single unit called Encapsulation.
4.    Abstraction—It is the act of representing essential features without including the background details.
5.   Polymorphism -- polymorphism refers to a programming language's ability to process objects differently depending on their data type or class.
6.    Inheritance -- Inheritance is the process by which objects of one class acquire the properties of objects of another class.

What are the types of Java applications?
Basically, Java Applications can be 4 types
1) Standalone application       core java
2) Client-Server application    core java and web technology
3) Web Application                 Servlet, JSP, Struts, Hibernate, etc.
4) Distributed Application       EJB application

What is UNICODE?
It is a two bytes character code that is used for internal representation of characters. It is a 16-bits code. Unicode is maintained by the Unicode® Consortium.

### Simple Java Programs

#### // Summation of two numbers in java

import java.io.*;
public class Summation
{
public static void main()throws IOException
{
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
int a,b,c;
System.out.println("Enter two number:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println(c);
}
}

#### //Addition and Average of student Marks

import java.util.Scanner;
public class Student
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m1,m2,m3,tot,avg;
String name;
System.out.println("Enter three subjects marks:");
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
System.out.println("Enter Student name:");
name=sc.next();
tot=m1+m2+m3;
avg=tot/3;
System.out.println(name+" Total="+tot+" Average="+avg);

}
}

#### //Java Program to Convert Fahrenheit into Celsius

import java.util.*;
public class TemperatureCoversion
{
public static void main(String args[])
{
Scanner a=new Scanner(System.in);
System.out.println("Enter Your Choice");
System.out.println("Enter 1 for conversion to celsius");
System.out.println("Enter 2 for conversion to farenheit");
int b=a.nextInt();
switch(b)
{
case 1:
System.out.println("Enter the temp in farenheit");
double c=a.nextDouble();
double d=5.0/9*(c-32);
System.out.println("The temp in celscius is"+d);
break;
case 2:
System.out.println("Enter the temp in celcius");
double e=a.nextDouble();
double f=1.8*(e+32);
System.out.println("The temp in farenheit is"+f);
break;
default:
System.out.println("Invalid Response,Run again");
}
}
}

More Tutorials

# While Loop in java

The  ‘while loop’ is a second type loop. It is used we are not known how many times the loop will execute. This is an entry-controlled loop. In a while loop if the initial condition is true then the execution will enter into the loop.

Syntax for the while loop

While(condition)

{

expression 1;

expression 2;

expression 3;

---------------;

---------------;

} The while statement is a repetition statement. It is an entry control loop and has the following syntax :
while ( condition ) {
// statements
}
The statements within the while block (between the opening and closing curly braces) are executed as long as the condition is true.
If we have only one statement, then there is no need to use the braces.
First, the condition is check. If the condition is true, then the statements are executed. Otherwise, nothing happens. After executing the statements, the condition is checked. If it is true again, the statement are executed and the condition is checked again and the process continues till the condition becomes false.
Here is an example code which prints numbers from 1 to 10.
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
The statement i = 1 initializes with 1. The condition i <= 10 is true, so we enter the body of the loop. i is printed and incremented to 2. Now again, the condition is checked and is true. i is printed and incremented to 10. In this way, the while loop executes and prints 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. Now when i is 10, i is printed and incremented to 11. i <= 10 is false. So, control comes out of the loop.  Full Example
//To check whether a number is an Armstrong or not

import java.io.*;
public class Armstrong
{
public static void main(String args[])throws IOException
{
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader in =new BufferedReader(read);
int n, num, a, b, c,s; s=0;
System.out.println("Enter your no.");
n= Integer.parseInt(in.readLine());
num=n;
while(n>0)
{
a=n/10;
b=a*10;
c=n-b;
s=s+c*c*c;
n=n/10;
}
if(num==s)
System.out.println("The number " +num+ " is an Armstrong no.");
else
System.out.println("The number " +num+ " is not an Armstrong no.");
}

Learn JAVA Step by Step More Java program

# Loops in Java - Java loop explained with examplesJava for loopArray in JavaTwin prime number in javacircular prime number program in java

## Java method or function

When we define a set of code within a block having a name is called method. It can call or invoke at any time.

We can define the Java method as follows

A simple Java method requires a minimum of three items:
Access Level        : public, private, protected
Return Type         : void, int, double, (etc.)
Name                   : whatever you want to call the method

Simple example of a function

private double simpleInterest(double p, double t, double r)
{
double si=(p*t*r)/100;
return si;
}

Here access specifier is private, return type double and the function name is simpleInterst with three parameters. In Java convention function start with a small character.

## Access Specifier

The access modifier or Access specifier is the access type of the method/Function. It specifies the visibility or accessibility of the method. Java has four types of access specifier or access modifier:

1. Default: Java uses default access specifier by default. It is visible only from the same package only.
2. Public: The method is accessible (visible in) by all classes when we use public specifier in Java application.
3. Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
4. Protected: Protected access specifier when use, the method is accessible within the same package or subclasses in a different package.
A full example of a function
public class Function1
{
int a,b,c;
int Add()  // return method
{
a=10;
b=30;
c=a+b;
return c;
}
public static void main(String smart[])
{
int x;
Function1 ob=new Function1();
x=ob.Add();
System.out.println("Sum="+x);
}
}

#### What is the Actual parameter?

Ans: The actual parameter is a parameter as appear in a method/function. Actual parameters (also known as arguments) are what is passed by the caller.

#### What is a Formal parameter?

Ans: the Identifier used in a method to stand for the value that is passed into the method by a caller. Formal parameters are also known as parameters of the function.

#### Simple Interest in Java

import java.util.*;
public class SimpleInterest
{
double p, t, r, si, amount; // Instance Variable
void inputValue()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Principal, Time and Rate :");
p=sc.nextDouble();
t=sc.nextDouble();
r=sc.nextDouble();
}
void simpleInt()
{
si=(p*t*r)/100.0;
amount=p+si;
}
void display()
{
System.out.println("Simple Interest="+si);
System.out.println("Amount="+amount);
}
public static void main()
{
SimpleInterest ob=new SimpleInterest();
ob.inputValue();
ob.simpleInt();
ob.display();
}
}

#### Prime Number in Java with Method

import java.util.*;
public class PrimeNumber
{
int n, i;
boolean p;
boolean prime(int x)
{
n=x;
p=true;
for(i=2;i<n;i++)
{
if(n%i==0)
{
p=false;
break;
}
}
return p;
}
public static void main()
{
int n; // Local variable
boolean pr;
Scanner sc=new Scanner(System.in);
PrimeNumber ob=new PrimeNumber();
System.out.println("Enter number:");
n=sc.nextInt();
pr=ob.prime(n);
if(pr==true)
System.out.println("Prime Number");
else
System.out.println("Not Prime Number");
}
}

#### Power and Factorial Method with return and Decimal Format

import java.io.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class SeriesPowFact
{
//private static DecimalFormat df = new DecimalFormat("#.##");
int i, p, f, x;
int pow(int n)
{
x=n;
p=1;
for(i=0; i<x ; i++)
{
p=p*x;
}
return p;
}

int fact(int n)
{
f=1;
x=n;
for(i=1; i<=x; i++)
{
f=f*i;
}
return f;
}

public static void main(String args[])throws IOException
{
int i, po, fa, no;
double s=0.0;
SeriesPowFact ob=new SeriesPowFact();
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(input);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number :");
no=Integer.parseInt(br.readLine());
for(i=1;i<=no;i++)
{
po=ob.pow(i);
fa=ob.fact(i);
s=s + (double)fa/po;
}
System.out.println("Series="+s);
DecimalFormat df = new DecimalFormat("#.###");
String formatted = df.format(s);
System.out.println(formatted); //prints 2.46
}
}

More Articles

# Java program for Pronic Number

## Java Program to check Krishna Murthy or Special number

### Special Number in JAVA

If we add factorial of all the digits of a number is called Krishnamurty Number.
For example 145 is krishnamurthy number
Sum of factorial of each digits  145
= 1! + 4! + 5!
= 1 + 24 + 120
= 145 which is equal the entered number.

import java.io.*;
/**
* class KrishnamurtyNumber
*  @ Khurshid Md Anwar
*/
public class KrishnamurtyNumber
{
private int n;  // instance variables
private int f;
/**
* Constructor  of the class
*/
public KrishnamurtyNumber()
{
// initialise instance variables
n = 0;
f  = 1;
}
// Factorial method
public int fact(int x)
{
f=1;
n=x;
for(int i=1; i<=n; i++)
{
f=f*i;
}
return f;
}
// Start of main method
public static void main(String args[])throws IOException
{
int  a, b, n, m, s;          // Local variable within the main method
s=0;                              // Initialize of s where we will store the summation
KrishnamurtyNumber ob=new KrishnamurtyNumber ();
InputStreamReader in=new InputStreamReader (System.in);
BufferedReader br=new BufferedReader(in);
System.out.print("Enter a number: ");
n = Integer.parseInt(br.readLine());
m=n;                // Store the n value to m
while(n!=0)
{
a = n % 10;
b = ob.fact(a);    // Call the factorialmethod for each digit
s = s + b;
n = n / 10;
}
// Checking the Krishnamurty number
if(m == s)
{
System.out.println ("Krishnamurty number");
}
else
{
System.out.println ("Not Krishnamurty number");
}
}        // Start of main method
} // end of class

Output
Enter a number: 147
Not KrishnamurtyNumber number
Enter a number: 145
KrishnamurtyNumber number

Description of the variables and method used
 Serial No. Variable Name Data Type Purpose 1 n int entered number 2 f int For storage of  factorial of the each digit 3 fact() int Factorial method 4 in InputStreamReader Object of class InputStreamReader 5 br BufferedReader The object of class BufferedReader 6 main() static void Main method 7 a, b, n, m, s int Main method local variable 8 ob object The object of KrishnamurtyNumber class

In the above program, I've calculated factorial and the sum that factorial. After the number becomes zero I've checked the number with sum of the factorial and if it is equal to the original number then that number is called Special or Krishnamurty number.