Programming in java for Interview
Write a Program to find M to the power N ?
Here M to the power N means if M=2 and N=3 then
2 to the power 3 is : 2*2*2 = 8
5 to the power 2 is : 5*5 = 25
MToPowerN.java
package com.omsingh;
import java.util.Scanner;
public class MToPowerN {
public static void main(String[]args)
{
System.out.println("Enter the number and raised power");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int pow=sc.nextInt();
int result=power(num,pow);
System.out.println(num+" to the power "+pow+ " is: "+result);
}
static int power(int a,int b)
{
int pow=1;
if(b==0)
return 1;
else
{
for(int i=1;i<=b;i++)
{
pow=pow*a;
}
return pow;
}
}
}
OUTPUT:
Here M to the power N means if M=2 and N=3 then
2 to the power 3 is : 2*2*2 = 8
5 to the power 2 is : 5*5 = 25
MToPowerN.java
package com.omsingh;
import java.util.Scanner;
public class MToPowerN {
public static void main(String[]args)
{
System.out.println("Enter the number and raised power");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int pow=sc.nextInt();
int result=power(num,pow);
System.out.println(num+" to the power "+pow+ " is: "+result);
}
static int power(int a,int b)
{
int pow=1;
if(b==0)
return 1;
else
{
for(int i=1;i<=b;i++)
{
pow=pow*a;
}
return pow;
}
}
}
OUTPUT:
Write a Program to find Factorial of a Number ?
Suppose a Number is 4 then factorial of 4 = 4*3*2*1 = 24
In general if Number is N then Factorial of N = N*(N-1)*(N-2)*(N-3)*..........*1
a) Program with Iterative method
FectorialIterative.java
package com.omsingh;
import java.util.Scanner;
public class FectorialIterative {
public static void main(String[]args)
{
int fect=1;
System.out.println("Enter any Number to find Factorial..");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
for(int i=1;i<=num;i++)
{
fect=fect*i;
}
System.out.println("Factorial of the Entered Number "+num+" is : "+fect);
}
}
OUTPUT:
b) Program with Recursion method
FectorialRecursion.java
package com.omsingh;
import java.util.Scanner;
public class FectorialRecursion {
public static void main(String[]args)
{
System.out.println("Enter any Number to find Factorial..");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int result=fectorial(num);
System.out.println("Factorial of the Entered Number "+num+" is : "+result);
}
static int fectorial(int n)
{
if(n==1) {
return 1;
}
else {
return n*fectorial(n-1);
}
}
}
OUTPUT:
package com.omsingh;
import java.util.Scanner;
public class FectorialRecursion {
public static void main(String[]args)
{
System.out.println("Enter any Number to find Factorial..");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int result=fectorial(num);
System.out.println("Factorial of the Entered Number "+num+" is : "+result);
}
static int fectorial(int n)
{
if(n==1) {
return 1;
}
else {
return n*fectorial(n-1);
}
}
}
OUTPUT:
Write a Program to check an Year is Leap Year or Not ?
Leap Year: An year which is divisible by 4 or 400 except that year is not a multiplier of 100 is a Leap Year.
i.e 4, 8, 400, 800 etc is a Leap Year but 100, 200, 300, 500 is not a Leap Year
CheckLeapYear.java
package com.omsingh;
import java.util.Scanner;
public class CheckLeapYear {
public static void main(String[]args)
{
System.out.println("Enter any year to check leap year or not :-) ");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
if(year%4==0 && year%400==0)
{
System.out.println("Entered Year "+year+" is a Leap Year");
}
else if(year%100==0 )
{
System.out.println("Entered Year "+year+" is not a Leap Year");
}
else
System.out.println("Entered Year "+year+" is a Leap Year");
}
}
OUTPUT:
i.e 4, 8, 400, 800 etc is a Leap Year but 100, 200, 300, 500 is not a Leap Year
CheckLeapYear.java
package com.omsingh;
import java.util.Scanner;
public class CheckLeapYear {
public static void main(String[]args)
{
System.out.println("Enter any year to check leap year or not :-) ");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
if(year%4==0 && year%400==0)
{
System.out.println("Entered Year "+year+" is a Leap Year");
}
else if(year%100==0 )
{
System.out.println("Entered Year "+year+" is not a Leap Year");
}
else
System.out.println("Entered Year "+year+" is a Leap Year");
}
}
OUTPUT:
Write a Program to check Prime Number ?
A number which is divisible by one(1) and itself only is called a Prime Number.
i.e. 2,3,5,7,11,13,17,19,23,29 etc.
Exception case: 1 is not a Prime Number.
NOTE:- 2 is the only even Prime Number.
CheckPrimeNumber.java
package com.omsingh;
import java.util.Scanner;
public class CheckPrimeNumber {
public static void main(String[]args)
{
System.out.println("Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// Here I am typcasting to int because sqrt() return double value.
int limit=(int) Math.sqrt(n);
//int limit=n/2;
int flag=0;
if(n==1)
System.out.println("Entered Number "+n+" is not a Prime Number");
else
{
for(int i=2;i<=limit;i++)
{
if(n%i==0) {
flag=1;
System.out.println("Entered Number "+n+" is not a Prime Number");
break;
}
}
if(flag==0)
System.out.println("Entered Number "+n+" is a Prime Number");
}
}
}
OUTPUT:
i.e. 2,3,5,7,11,13,17,19,23,29 etc.
Exception case: 1 is not a Prime Number.
NOTE:- 2 is the only even Prime Number.
CheckPrimeNumber.java
package com.omsingh;
import java.util.Scanner;
public class CheckPrimeNumber {
public static void main(String[]args)
{
System.out.println("Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// Here I am typcasting to int because sqrt() return double value.
int limit=(int) Math.sqrt(n);
//int limit=n/2;
int flag=0;
if(n==1)
System.out.println("Entered Number "+n+" is not a Prime Number");
else
{
for(int i=2;i<=limit;i++)
{
if(n%i==0) {
flag=1;
System.out.println("Entered Number "+n+" is not a Prime Number");
break;
}
}
if(flag==0)
System.out.println("Entered Number "+n+" is a Prime Number");
}
}
}
OUTPUT:
Note: If your program has a package then to compile and run, command changed slightly.
To Compile: javac -d . className(CheckPrimeNumber).java // javac -d(f in my case but it still working)(sapace).(space)className.java
Here -d is used to tell JVM to switched compiled .class file to a directory.
Here .(dot) means current working directory(jvm will create the com/testapp folder at the current directory.
To Run: java package.subpackage.ClassName (here java com.omsingh.CheckPrimeNumber)
Here com is package and omsingh is subpackage.
To run this program you have to write full path of java file (here com.omsingh.CheckPrimeNumber)
Explanation of program:
Here first we get the limit of for loop
int limit=(int) Math.sqrt(n); (oplimized code)
int limit=n/2;
int limit=n;
Note: If a number is divided(means remainder is 0) by any of the number between 1 to square root(sqrt) of n(that number). then that number is not a Prime Number.
That is the reason, I am here running for loop from 1 to sqrt(n).But if you are not comfortable with sqrt() function then you can use 1 to n/2 or 1 to n but this is not a optimized code.It will take more time as compare to fist one.
Scanner: Here Scanner class is used to take input from the user(console).It is defined under java.util package.
nextInt(): It is method of Scanner class to read int value from user(console).
Math.sqrt(n): Here Math is class defined in java.lang package and sqrt() is a static method defined in it.
flag: Here flag is variable initialized to 0 to check further its value changed or not on this value we are checking n is Prime or not.
To Compile: javac -d . className(CheckPrimeNumber).java // javac -d(f in my case but it still working)(sapace).(space)className.java
Here -d is used to tell JVM to switched compiled .class file to a directory.
Here .(dot) means current working directory(jvm will create the com/testapp folder at the current directory.
To Run: java package.subpackage.ClassName (here java com.omsingh.CheckPrimeNumber)
Here com is package and omsingh is subpackage.
To run this program you have to write full path of java file (here com.omsingh.CheckPrimeNumber)
Explanation of program:
Here first we get the limit of for loop
int limit=(int) Math.sqrt(n); (oplimized code)
int limit=n/2;
int limit=n;
Note: If a number is divided(means remainder is 0) by any of the number between 1 to square root(sqrt) of n(that number). then that number is not a Prime Number.
That is the reason, I am here running for loop from 1 to sqrt(n).But if you are not comfortable with sqrt() function then you can use 1 to n/2 or 1 to n but this is not a optimized code.It will take more time as compare to fist one.
Scanner: Here Scanner class is used to take input from the user(console).It is defined under java.util package.
nextInt(): It is method of Scanner class to read int value from user(console).
Math.sqrt(n): Here Math is class defined in java.lang package and sqrt() is a static method defined in it.
flag: Here flag is variable initialized to 0 to check further its value changed or not on this value we are checking n is Prime or not.