Advertisement

Responsive Ads Here

Friday, December 5, 2025

Top 11--20 Java programming Interview questions Part 2


11-To check the given number is Armstrong number

public class CourseDetails {

 

               public static void main(String[] args) {

 

                              int num = 153;

 

                              int n = num;

 

                              int res = 0;

 

                              int rem = 0;

 

                              while (num > 0) {

 

                                             rem = num % 10;

 

                                             res = res + (rem * rem * rem);

 

                                             num = num / 10;

 

                              }

 

                              if (n == res) {

 

                                             System.out.println("Armstrong number");

 

                              } else {

 

                                             System.out.println("Not an Armstrong number");

 

                              }

 

               }

 

}


12.To find the sum of the digits in a given number

public class CourseDetails {

 

               public static void main(String[] args) {

 

                              int num = 153;

 

                              int res = 0;

 

                              int rem = 0;

 

                              while (num > 0) {

 

                                             rem = num % 10;

 

                                             res = res + rem;

 

                                             num = num / 10;

 

                              }

 

                              System.out.println("Sum of digits: "+res);

 

               }

 

}

13.To find the product of the digits in a given number:

public class CourseDetails {

 

               public static void main(String[] args) {

 

                              int num = 153;

 

                              int res = 1;

 

                              int rem = 0;

 

                              while (num > 0) {

 

                                             rem = num % 10;

 

                                             res = res * rem;

 

                                             num = num / 10;

 

                              }

 

                              System.out.println("Product of digits: "+res);

 

               }

 

}

 

14.To find the count of the digits in a given number:

public class CourseDetails {

 

               public static void main(String[] args) {

 

                              int num = 15345;

 

                              int count = 0;

 

                              while (num > 0) {

 

                                             count++;

 

                                             num = num / 10;

 

                                             

 

                              }

 

                              System.out.println("Count of digits: " + count);

 

               }

 

}

 

15.To remove the duplicates and to sort elements from Array:

public class A {

               public static void main(String[] args) throws IOException {

                              int[] a = { 55, 39, 26, 78, 55, 99, 30 };

                              TreeSet<Integer> t = new TreeSet<>();

                              for (int i = 0; i < a.length; i++) {

                                             t.add(a[i]);

                              }

                              System.out.println(t);

               }

}

16.To sort numbers   in ascending order:

public class CourseDetails {

               public static void main(String[] args) {

                              int[] a = { 55, 20, 60, 90, 15, 35 };

                              

                              System.out.println("Before sorting");

                              for (int i : a) {

                                             System.out.println(i);

                              }

                              

                              for (int i = 0; i < a.length; i++) {

                                             for (int j = i + 1; j < a.length; j++) {

                                                            // Ascending order

                                                            if (a[i] > a[j]) {

                                                                           int temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }

                                             }

                              }

                              System.out.println("After sorting in ascending order");

                              for (int i : a) {

                                             System.out.println(i);

                              }

               }

}

17.To sort elements in descending order:

public class CourseDetails {

               public static void main(String[] args) {

                              int[] a = { 55, 20, 60, 90, 15, 35 };

                              

                              System.out.println("Before sorting");

                              for (int i : a) {

                                             System.out.println(i);

                              }

                              

                              for (int i = 0; i < a.length; i++) {

                                             for (int j = i + 1; j < a.length; j++) {

                                                            // Descending order

                                                            if (a[i] < a[j]) {

                                                                           int temp = a[i];

                                                                           a[i] = a[j];

                                                                           a[j] = temp;

                                                            }

                                             }

                              }

                              System.out.println("After sorting in descending order");

                              for (int i : a) {

                                             System.out.println(i);

                              }

               }

}

18.To find count of each character in string(Count of duplicates of each character):

public class CourseDetails {

               public static void main(String[] args) {

                              String s = "raining here...";

                              char[] c = s.toCharArray();

                              LinkedHashMap<Character, Integer> mp = new LinkedHashMap<>();

                              for (char d : c) {

                                             if (mp.containsKey(d)) {

                                                            Integer i = mp.get(d);

                                                            mp.put(d, i + 1);

                                             }

                                             

                                             else {

                                                            mp.put(d, 1);

                                             }

                              }

                              

                              System.out.println(mp);

               }

}

19.To add each odd and even pair in an array:

public class A {

               public static void main(String[] args) throws IOException {

                              

                              int[] a= {10,20,30,40,50};

                              

                              try {

                                             for (int i = 0; i < a.length; i=i+2) {

                                                            System.out.println(a[i]+a[i+1]);

                                             }

                              }

                              catch (Exception e) {

                                             System.out.println(a[a.length-1]);

                              }                             

               }

}

20.To find count of each word in string(Count of duplicates of each word):

public class A {

               public static void main(String[] args) throws IOException {

                              String s = "java sql java python ";

                              String[] sp = s.split(" ");

                              LinkedHashMap<String, Integer> mp = new LinkedHashMap<>();

                              for (String st : sp) {

                                             if (mp.containsKey(st)) {

                                                            Integer i = mp.get(st);

                                                            mp.put(st, i + 1);

                                             } else {

                                                            mp.put(st, 1);

                                             }

                              }

                              System.out.println(mp);

               }

}


No comments:

Post a Comment