Practice Loop
Practice Loop
01: Write a Java program that reads ten integers from the console and determines how many positive and negative values
have been read and computes the total and average of the input values (not counting zeros). Your program ends with the
02: Write a Java program that prompts the user to enter two positive integers and displays the GCD. The greatest
common divisor (GCD) of two integers’ n1 and n2 can be found in the following way: First find d to be the minimum of
n1 and n2, and then check whether d, d-1, d-2, 2, or 1 is a divisor for both n1 and n2 in this order. The first such common
divisor is the greatest common divisor for n1 and n2. Use appropriate JOptionPane dialog boxes to read inputs and write
outputs.
03: Write a Java program that reads student scores, gets the best score, and then assigns grades based on the following
scheme:
Grade is F otherwise.
The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores and
04: Write a Java program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input
RADAR is a palindrome, but ROVER is not. Write a Java program that prompts the user to enter a string and displays
whether the string is a palindrome or not. You may not use any built-in Java methods to accomplish that.
06: Write a Java program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers
separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number
and store it in an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the
distinct numbers. Write a method Isdistinct() to check whether a number exists multiple times or not.
07: Write a Java program that randomly generates an integer array/ArrayList, numbers, of size 100. Then, find the
value and index (position) of the second highest and the second smallest element. Use separate methods for
08: Write a program that prompts the user to enter the number of students, the students’ names, and their scores, and
09: You are given n lines. In each line there are zero or more integers. You need to answer a few queries where you need
to tell the number located in yth position of xth line. Take your input from System.in.
Input Format
The first line has an integer n. In each of the next n lines there will be an integer d denoting number of integers on that
line and then there will be d space-separated integers. In the next line there will be an integer q denoting number of
Output Format
In each line, output the number located in yth position of xth line. If there is no such position, just print "ERROR!"
10: Write a function that will return an array/ArrayList consisting of the first n term of the Fibonacci sequence.
Example 1:
Input: 5
Output: [0, 1, 1, 2, 3]
Example 2:
Input: 8
Example 1:
Output: “Programming”
Example 2:
Output: “Engineering”
12: Given an integer array nums, return true if any value appears at least twice in the array, and return false if every
element is distinct.
Example 1:
Output: true
Example 2:
Output: false
Example 3:
Output: true
13: Given an array of integers and an integer target, return indices of the two numbers such that they add up to the target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example 1:
Output: [0,1]
Example 2:
Output: [1,2]
Example 3:
14: Given an integer array, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Output: [1,3,12,0,0]
Example 2:
Output: [0]
15: Given an integer array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Output: [5,6,7,1,2,3,4]
Explanation:
Example 2:
Output: [3,99,-1,-100]
Explanation:
16: Write a Java program to delete all the duplicates from an array/ArrayList. Assume the elements of the array
Example 1:
Output: [1,2,3]
Example 2:
Output: [1,2,3,4]
Example 3:
Output: [1,3,4,2]