Pages

Monday, May 11, 2020

Top 20 Java Interview Programs

Below programs covered in section:

Fibonacci Series, Armstrong Number, Anagrams Number, Prime Number, Reverse Number / Palindrome Number, Perfect Number, Duplicate Numbers and String/ Word/Characters, Factorial of number,  String Rotation, Binary to Decimal, Largest & Second largest Number, Calculate Sum of All Numbers Contained in String, Find Number of Words in Paragraph, Programs on patterns Of Triangles, Print Pyramid, Number series etc.



1. Fibonacci Series:

public static void main(String[] args) throws Exception {
int ipnum=6, oldVal=1, newVal=1, sum=0;
for(int i=0; i<=ipnum; i++)
{
System.out.print(" "+sum);//0 1 1 2 3 5 8
oldVal=newVal;
newVal=sum;
sum=oldVal + newVal;
}
}

Output:

0 1 1 2 3 5 8


2. Armstrong Number:

int num, rem, sum=0, temp;

System.out.println("Enter any number:");
Scanner sc =new Scanner(System.in);
num=sc.nextInt();
temp=num;

while (num!=0) {
rem = num % 10;
sum = sum+rem*rem*rem
num= num /10;
}
if(sum==temp)
{
System.out.println("Armstrong Number");
}
else
System.err.println("Not Armstrong Number");
}
}

Output:

Enter any number:
153
Armstrong Number



3. Anagrams Number:

public static void main(String[] args) {
System.out.println("Enter String: ");
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
System.out.println("Enter Another String: ");
String str2=sc.nextLine();

//Step 1: Convert to Char Array
char [] c1=str1.toCharArray();
char [] c2=str2.toCharArray();

//Step 2: Sort an Array
Arrays.sort(c1);
Arrays.sort(c2);

//Step 3:Compare by equals()
if (Arrays.equals(c1, c2)) {
System.out.println("****Anagrams****");}
else {
System.err.println("**Not Anagrams**");
}
}

Output:
Enter String: Tony
Enter Another String: Stark
**Not Anagrams**

(Check for Anagrams)

Enter String: Fired
Enter Another String: Fried
****Anagrams****




4. Duplicate Numbers:

public static void main(String a[]) {
int[ ] numbers = { 21, 12, 35, 31, 24, 73, 64, 85, 55, 35, 24};

Arrays.sort(numbers);
for(int i = 1; i < numbers.length; i++) {
if(numbers[i] == numbers[i-1])
{
System.out.println("Duplicate: " + numbers[i]);
}

Output:
Duplicate numbers are:  24 35



5. Find Duplicate String/ Word:

String str=
"Tutorials core Java and advanced Java and Java programming";
List<String> list=Arrays.asList(str.split(" ")); //Split all spaces 
Set<String> set=new HashSet<String>(list); 
for(String s : set)
{
System.out.println(s+" "+Collections.frequency(list, s));
}

Output:
core 1
Java 3
advanced 1
and 2
Tutorials 1
programming 1



5. Find Duplicate Characters:

public static void main(String[] args) {
String str="pursuit of happiness";
Map<Character, Integer> hm = new HashMap<Character, Integer>();
char[] chrs = str.toCharArray();
for(Character ch: chrs){
if(hm.containsKey(ch)){
hm.put(ch, hm.get(ch)+1);
}
else {
hm.put(ch, 1);
}
}
Set<Character> keys = hm.keySet();
for(Character ch:keys){
if(hm.get(ch) > 1)
{
System.out.println(ch+"--->"+hm.get(ch));
}
}

Output:
  --->2  //space
i--->2
p--->3
s--->3
u--->2




6. Find Number of Words in Paragraph:

public static void main(String[] args) {
System.out.println("Enter the string: ");
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
String[] words = str.trim().split(" ");
System.out.println("Number of words= "+words.length);
}

Output:
Enter the string:
The Lord of the King: The Return of the Ring
Number of words= 10



7. Reverse Number / Palindrome Number:

int num, temp, sum=0, rem; 
System.out.println("Enter any number:");
Scanner sc =new Scanner(System.in);
num=sc.nextInt();
temp=num; //store temp value of num to temp variable


while (num>0)
{
rem= num % 10; //3 //2 //1
sum=sum*10 + rem; //3 //32 //321
num= num / 10; //12 //1 //0
}
System.out.println("Reverse Number is:"+sum);
if(sum == temp){
System.out.println("It's Palidrome Number...I");
}
else
System.err.println("Not Palidrome Number...");
}

Output:

Enter any number:
143
Reverse Number is:341
Not Palidrome Number...

Enter any number:
151
Reverse Number is:151
It's Palidrome Number!



8. String Rotation:

String s1 = "nullpointer";
String s2 = "pointernull";
String s3 = s1 + s1;
if(s3.contains(s2)) {
System.out.println("Rotation Version");
}
else{
System.err.println("Not rotation Version");
}
}

Output:
Rotation Version



9. Check Prime Number:

int num, temp, sum=0, rem, den=2; 
System.out.println("Enter any number:");
Scanner sc =new Scanner(System.in);
num=sc.nextInt();
temp=num; //store temp value of num to temp variable

while (den<num) {
if (num % den == 0) {
System.err.println("Not Prime Number");
break;
}
den++;
}
if (den==num) {
System.out.println("Prime Number!");
}

Output:
Enter any number:
47

Prime Number!



10. Find Largest & Second largest Number:

int large= 0;
int secondlarge= 0;
int array[]={1,6,3,140,799,24,55,77};

for(int i=0; i<array.length; i++)
{
//for large
if(array[i]>large)
{
secondlarge = large;
large= array[i];
}
//for secongLarge
else if(array[i] > secondlarge)
{
secondlarge = array[i];
}
}
System.out.println("Large:"+large);
System.out.print("Second Large:"+secondlarge);
}

Output:
Large:799

Second Large:140



11. Calculate Sum of All Numbers Contained in String:

public static void main(String[] args) {
String str="Marriage on 11th May at 10:45 am and reception on 12th May at 8:30pm";

//1. replace non digit as Space
str=str.replaceAll("[\\D]", " ");

//2. Split into Space
String [] splits=str.split(" ");
int sum=0;

for (int i = 0; i < splits.length; i++) {
try {
sum = sum + Integer.parseInt(splits[i]);
}
catch (Exception e) {
}
}
System.out.println(sum);
}

Output:
Sum of all numbers:116


12. Calculate Factorial / Sum of all numbers:

int num, temp, sum=0, fact=1; 
System.out.println("Enter any number:");
Scanner sc =new Scanner(System.in);
num=sc.nextInt();

for (int i = 1; i <= num; i++) {
fact = fact *i;
sum = sum + i;
}
System.out.println("Factorial is:"+fact);
System.out.println("Sum is:"+sum);

Output:
Enter any number: 5
Factorial is:120

Sum is:15



13. Perfect Number: (Please google about perfect number)

int num, temp, sum=0; 
System.out.println("Enter any number:");
Scanner sc =new Scanner(System.in);
num=sc.nextInt();

for (int i = 1; i <=num/2; i++) {
i
f (num%i==0) {
sum = sum+i;
}
}
if (sum==num) {
System.out.println("Perfect Number: "+num);
}
else{
System.err.println("Not a Perfect Number");
}

Output:
Enter any number: 28
Yeah..It's Perfect Number



14. Binary to Decimal:

int sum=0, rem, i=0;

int num=1111; //Or we can take number from console

while(num>0)
{
rem=num%10;
sum=(int) (sum + rem * Math.pow(2, i));
num=num/10;
i++;
}
System.out.println(sum); 

Output: 
15



15. Decimal to Binary:

int num=15, index =0;
int binary[]=new int[10];
while (num>0)
{
binary [index++]=num % 2;
num=num/2;
}
for (int i = index-1; i >=0; i--) {
System.out.print(binary[i]);
}


Output: 
1111




16. Patterns Of Triangle and Others:

public class Patterns {
public static void main(String[] args) {
/** 1. Ascending Order
*
* for column: for rows:
* 1 2 3 4 5 1 1 1 1 1
1 2 3 4 5 2 2 2 2 2
1 2 3 4 5 3 3 3 3 3
1 2 3 4 5 4 4 4 4 4
1 2 3 4 5 5 5 5 5 5
*/
for (int i=1; i <=5; i++) {
    for (int j=1; j <=5; j++) {
       System.out.print(i+" ");
  }
      System.out.println(" ");
 }


/** 2. Descending Order
*
5 4 3 2 1 5 5 5 5 5
5 4 3 2 1 4 4 4 4 4
5 4 3 2 1 3 3 3 3 3
5 4 3 2 1 2 2 2 2 2
5 4 3 2 1 1 1 1 1 1
*/

for (int i=5; i>=1; i--)  {
     for (int j=5; j>=1; j--) {
         System.out.print(i+" ");
   }
         System.out.println(" ");
}


/**
* 3. Characters in Ascending Orders:
*
A A A A A A B C D E
B B B B B A B C D E
C C C C C A B C D E
D D D D D A B C D E
E E E E E A B C D E
*/
for (char i ='A'; i <='E'; i++) {
     for (char j ='A'; j <='E'; j++) {
         System.out.print(i+" ");
    }
       System.out.println(" ");
}


/**
* 4. Characters in Descending Orders:
*
E D C B A E E E E E
E D C B A D D D D D
E D C B A C C C C C
E D C B A B B B B B
E D C B A A A A A A
*/
for (char i ='E'; i >='A'; i--) {
      for (char j ='E'; j >='A'; j--) { 
        System.out.print(j+" ");
    }
        System.out.println(" ");
   }


/**
* 5. Triangle: (check condition in column loop)
*
* 1 1
* * 1 2 2 2
* * * 1 2 3 3 3 3
* * * * 1 2 3 4 4 4 4 4
* * * * * 1 2 3 4 5 5 5 5 5 5
Do same for Characters and reverse numbers:
*/
for (int i=1; i<=5; i++) {
      for (int j=1; j<=i; j++) {
        System.out.print(i+" ");
      } 
       System.out.println(" ");
   }


/**
6. Triangle as below:
5 5 5 5 5 1 2 3 4 5 * * * * *
4 4 4 4 1 2 3 4 * * * *
3 3 3 1 2 3 * * *
2 2 1 2 * *
1 1 *
*Do same for Characters:
*/
for (int i=5; i>=1; i--) {
for (int j=1; j<=i; j++) {
System.out.print(j+" ");
}
System.out.println(" ");
}
for (char i='A'; i<='E'; i++) {
for (int j='E'; j>=i ;j--) {
System.out.print(i+" ");
}
System.out.println(" ");
}}
}


17. Prog to Print Pyramid
*
***
*****
*******

int i, j, k;
int min = 1, num=4 (4 rows of *);

for (i = 0; i < num; i++) {
      for (j = num - 1; j > i; j--) {
          System.out.print(" "); //here should be a space
   }
     for (k = 0; k < min; k++) {
   System.out.print("*");
}
   System.out.println();
   min = min + 2;
}


18. Program to Print Rectangle:

 *****
*      *
*      *
*      *
 *****

int i,j;
for(i=0; i<=4; i++){
     for(j=0; j<=4; j++){
           if (i==4 ||i==0 || j==4|| j==0) {
             System.out.print("*");
         }
else{
System.out.print(" ");
     }
  }
System.out.println();
}



19. Program to Print Series Of Numbers:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

int i,j;
for(i=1; i<=5; i++){
     for(j=1; j<=5; j++){

int k=i+j-1;
if (k>5) {
System.out.print(k-5+" ");
}
else{
System.out.print(k+" ");
     }
  }
System.out.println();
}




20. Prog to Swap numbers without using 3rd variable:

public static void main(String[] args) {
int x, y;
System.out.print("Enter x and y : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();

System.out.println("Before Swapping: " + " x=" + x + " y=" + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping: " + " x=" + x + " y=" + y);

}




Please Note: Another new advance programs will upload soon on all core Java topics.



No comments:

Post a Comment

**** Please let me know if you have any doubts. ****