Java Exercise Programs Topic-wise

Basic Exercise

  1. Java Program to print an output.
    Hey there,
    I am Programmer!
    Solution
  2. Write a program to print
    *
    **
    ***
    ****
    on screen.
    Solution
  3. Print the following pattern on the screen
    *****
     *** 
      *  
     *** 
    *****
    Solution
  4. Write a program to print the sum of the numbers 2, 4 and 5.
  5. Write a program to print the difference and product of the numbers 45 and 32.

Java String Practice Set

  1. Write a program to print a string entered by user.
  2. Write a program to input and display the sentence I love candies.
  3. Write a program to find the length of the string “refrigerator”.
  4. Write a program to check if the letter ‘e’ is present in the word ‘Umbrella’.
  5. Write a program to check if the word ‘orange’ is present in the “This is orange juice”.
  6. Write a program to find the first and the last occurrence of the letter ‘o’ and character ‘,’ in “Hello, World”.
  7. Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R. B. Roser.
  8. Write a program to find the number of vowels, consonants, digits and white space characters in a string.
  9. Write a program to delete all consonants from the string “Hello, have a good day”.
  10. Input a string of alphabets. Find out the number of occurrences of all alphabets in that string. Find out the alphabet with maximum occurrence.

Data Types

Java For Loop Exercise

  1. Write a program in Java to print the first 10 natural numbers.
    Sample output:
    The natural numbers are:
    1 2 3 4 5 6 7 8 9 10
  2. Write a program in Java to print the sum of the first 10 natural numbers.
    Sample Output:
    Find the first 10 natural numbers:
    —————————————
    The natural numbers are:
    1 2 3 4 5 6 7 8 9 10
    The sum of first 10 natural numbers: 55
  3. Write a program in Java to display n terms of natural numbers and their sum.
    Sample Output:
    Input a number of terms: 7
    The natural numbers upto 7th terms are:
    1 2 3 4 5 6 7
    The sum of the natural numbers is: 28
  4. Write a program in Java to check whether a number is prime or not.
    Sample Output:
    Input a number to check prime or not: 13
    The entered number is a prime number.
  5. Write a program in Java to find a prime number within a range.
    Input number for starting range: 1
    Input number for ending range: 100
    The prime numbers between 1 and 100 are:
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
    The total number of prime numbers between 1 to 100 is: 25
  6.  Write a program in Java to find the factorial of a number.
    Sample output:
    Input a number to find the factorial: 5
    The factorial of the given number is: 120
  7. Java Program to check prime Number of a given number
  8. Java Program to Calculate Factorial of given Number
  9. Java Program to print Fibonacci numbers using for loop.

Java while Loop Exercise

  1. Print all numbers from 1 to 10 using a while loop.
  2. Print all even numbers from 2 to 20 using a while loop.
  3. Print all odd numbers from 1 to 19 using a while loop.
  4. Print all numbers from 10 to 1 in descending order using a while loop.
  5. Print the multiplication table of 5 using a while loop.
  6. Print the sum of all numbers from 1 to 100 using a while loop.
  7. Print the factorial of a given number using a while loop.
  8. Print all the prime numbers between 1 and 50 using a while loop.
  9. Print the Fibonacci sequence up to 100 using a while loop.
  10. Print the reverse of a given number using a while loop.
  11. Print the largest number in a given array using a while loop.
  12. Print the smallest number in a given array using a while loop.
  13. Print the sum of all numbers in a given array using a while loop.
  14. Print the product of all numbers in a given array using a while loop.
  15. Print the average of all numbers in a given array using a while loop.
  16. Print the number of positive, negative and zero elements in a given array using a while loop.
  17. Print the multiplication table of a given number up to 10 using a while loop.
  18. Print the sum of all odd numbers from 1 to a given number using a while loop.
  19. Print the sum of all even numbers from 1 to a given number using a while loop.
  20. Print the sum of the digits of a given number using a while loop.

Java Array Exercise

  1. Write a Java program to sort a numeric array and a string array.
  2. Write a Java program to sum values of an array.
  3. Write a Java program to calculate the average value of array elements.
  4. Write a Java program to test if an array contains a specific value.
  5. Write a Java program to find the index of an array element.
  6. Write a Java program to remove a specific element from an array.
  7. Write a Java program to copy an array by iterating the array.
  8. Write a Java program to insert an element (specific position) into an array.
  9. Write a Java program to find the maximum and minimum value of an array.
  10. Write a Java program to reverse an array of integer values.
  11. Write a Java program to find the duplicate values of an array of integer values.
  12. Write a Java program to find the duplicate values of an array of string values.
  13. Write a Java program to find the common elements between two arrays (string values). 
  14. Write a Java program to find the common elements between two arrays of integers.
  15. Write a Java program to remove duplicate elements from an array.
  16. Write a Java program to find the second largest element in an array.
  17. Write a Java program to find the second smallest element in an array. 
  18. Write a Java program to add two matrices of the same size. 
  19. Write a Java program to convert an array to ArrayList.
  20. Write a Java program to convert an ArrayList to an array.
  21. Write a Java program to find all pairs of elements in an array whose sum is equal to a specified number.
  22. Write a Java program to test the equality of two arrays.
  23. Write a Java program to find a missing number in an array.
  24. Write a Java program to find common elements from three sorted (in non-decreasing order) arrays.
  25. Write a Java program to move all 0’s to the end of an array. Maintain the relative order of the other (non-zero) array elements.
  26. Write a Java program to find the number of even and odd integers in a given array of integers.
  27. Write a Java program to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above.
  28. Write a Java program to compute the average value of an array of integers except the largest and smallest values.
  29. Write a Java program to check if an array of integers without 0 and -1
  30. Write a Java program to check if the sum of all the 10’s in the array is exactly 30. Return false if the condition does not satisfy, otherwise true.
  31. Write a Java program to check if an array of integers contains two specified elements 65 and 77.
  32. Write a Java program to remove the duplicate elements of a given array and return the new length of the array.
    Sample array: [20, 20, 30, 40, 50, 50, 50]
    After removing the duplicate elements the program should return 4 as the new length of the array.
  33. Write a Java program to find the length of the longest consecutive elements sequence from a given unsorted array of integers.
    Sample array: [49, 1, 3, 200, 2, 4, 70, 5]
    The longest consecutive elements sequence is [1, 2, 3, 4, 5], therefore the program will return its length 5.
  34. Write a Java program to find the sum of the two elements of a given array which is equal to a given integer.
    Sample array: [1,2,4,5,6]
    Target value: 6.