30+ Basic Java Programs

Here I am presenting 30+ Basic Java Programs.

  1. Create a program that accepts exactly two command line arguments. Otherwise, it should print an error message and exit. When the user enters (1) programmingempire and Delhi, the output should be Programmingempire Team Delhi (2) If the inputs are princites and Ajmer, the output should be Princites Team Ajmer.
  2. Similarly, in another program accept exactly one command line parameter and display a welcome message. For example, (1) if the input is Cherry, the output is Welcome Cherry. (2) If the input is Charlie, the output should be Welcome Charlie.
  3. Write another program that accepts exactly two integers as the command line arguments and shows their sum.
  4. Similarly, check whether a user has entered any command line argument or not. When no argument is in input display No Data. Otherwise, display all arguments.
  5. Write a program to accept a number as input and display whether it is positive, negative, or zero.
  6. Find whether the two numbers entered by the user have the same unit’s digit.
  7. Write a program to find whether an input number is odd or even.
  8. Take two characters as input and arrange them in alphabetical order.
  9. Initialize a character variable and display the output as ‘Alphabet’, ‘Digit’, or ‘Special Character’.
  10. Write a program to display the interest percentage on Fixed Deposit by taking the age and gender (‘Male’ or ‘Female’) as input. In order to calculate the interest percentage, follow these rules. When the gender is Female (i) if the age is between 1 and 48, the interest is 8.6% (ii) For age between 49 and 100, interest is 9.6%. Otherwise (gender is ‘Male’), (i) age: 1-48, interest is 7.4% (ii) age: 49-100, interest is 8.7%.
  11. Write a program that takes a character and a string as input and displays that in uppercase, if the input is in lowercase. Otherwise, display the input in lowercase.
  12. Accept as input a character as the color code and display the name of the corresponding color. For instance, if the color code is ‘R’, the color name is ‘Red’. In order to perform it, use a switch-case statement. When the input is not valid, display ‘Invalid Code’.
  13. Write a program to accept a number between 1 and 12 and display the corresponding month name.
  14. Display the integers from 1 to 10 separated by a tab.
  15. Write a program to accept a range and display the even numbers in that range. For instance, the user enters 10 and 20. So, the output should be 10 12 14 16 18 20.
  16. Find whether a number entered as input is prime or not.
  17. Also, display prime numbers in a specific range.
  18. Find and display the sum of digits of a number.
  19. Also, find the reverse of a number and display it.
  20. Further, determine if the given number is a palindrome or not.
  21. Display Floyd’s Triangle using ‘*’.
  22. Initialize an integer array and display the sum and average of its elements.
  23. Find maximum and minimum in the array.
  24. Write a program to search for a value in an integer array. When the value is not present display -1. Otherwise, display its index.
  25. Display the corresponding character values for the ASCII values present in an array.
  26. Find the largest two elements and the smallest two elements in an array.
  27. Write a program to sort an array.
  28. Remove duplicates from an array.
  29. Find the sum of elements of an array. However, the following condition applies. Take two numbers as input. When the input numbers appear in the same order in the array ignore all elements including those two numbers while calculating the sum. For instance, input numbers are 12 and 8. (i) When array is {1, 9, 12, 7, 3, 6, 8, 2, 9}, sum= 21 (1+9+2+9) (ii) array: {1, 8, 1, 6, 12, 5}, sum= 33 (1+8+1+6+12+5) (ii) When array is {2, 7, 12, 4, 8, 5}, sum=14 (2+7+5).
  30. Write a program to remove all occurrences of a given number in an array and move other elements left. Also, place 0 for removed elements. For instance, array={3,2,8,2,2,19,2,4}, input number=2. Hence the output array={3,8,19,4,0,0,0,0}.
  31. Shift all even elements in an array towards the left. Whereas the odd elements should be on the right side. For instance, input array={3,1,8,12, 9,6,5,8,1,17}. So, the output array={8,12,6,8,3,1,9,5,1,17}.
  32. Write a program that accepts two integers as input. Also, initialize an array of integers. When the array contains only the occurrences of input integers, the output should be True. Otherwise the output should be False.
  33. Initialise two integer arrays of three elements each. Further, create a third array of two elements by taking their middle element.
  34. Create a 2D array by taking elements using command line arguments. Find the reverse of the array.
  35. Also, find the largest number from a 3 X 3 integer array.

Further Reading

Java Practice Exercise



You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *