To calculate the repeated subtraction of two numbers, the code is as follows −Example Live DemoOutputThe repeated subtraction results in 18A function named ‘repeated_sub’ is defined that checks to see if two values divide each other completely, and if this is true, it divides the numbers and gives the floor value of the quotient. Otherwise, it gives the floor value of quotient and the value computed by calling the ‘repeated_sub’ function on the second value, and the remainder when the values are divided.Outside the function, values are given to both the variables and the function is called by passing these values ... Read More
To find the sum of odd numbers within a given range, the code is as follows −Example Live DemoOutputThe sum of odd natural numbers between the numbers 3 and 23 is 141.75A function named ‘odd_num_sum’ is defined that computes the sum of odd numbers within a specific range of numbers. The function ‘num_in_range’ gives the range of values between two numbers that are passed as parameters to this function. Outside both the function, the range values are defined and this function ‘num_in_range’ is called by passing the low and high values as parameters. Relevant output is displayed on the console.Read More
To find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’, the code is as follows −Example Live DemoOutputThe sum of first 11 natural numbers divisible by 2 or 5 is 15A function named ‘sum_of_nums’ is defined that computes three values by checking if they can be divided by two specific values or not. Outside the function, the number and the two specific values are defined, and the function is called by passing these values as parameters. Relevant output is displayed on the console
This example demonstrates how do I show soft keyboard based on Android EditText is focused.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javaimport android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText ... Read More
To find the numbers within a given array that are missing, the code is as follows −Example Live DemoOutputThe missing numbers in the array is 1 2 3 4 5A function named ‘missing_nums’ is defined that checks to see if a number is missing from an array of continuous numbers. It iterates through the array and checks to see the count and the current_num that is being iterated over. If two values can’t be found when 1 is added to the previous number, it is considered to be missing.Outside the function, the array is defined, its length is assigned to a ... Read More
To find the first natural number whose factorial can be divided by a number ‘x’, the code is as follows −Example Live DemoOutputThe first natural number whose factorial can be divided by 16 is 4A function named ‘factorial_num’ computes factorial of a number and checks to see if it is divisible by 16, and if yes, returns that number as output. Outside the function, a number is defined and it is passed as parameter to the function. Relevant output is displayed on the console.
To find if a number is present in a given sequence of numbers, the code is as follows −Example Live DemoOutputIs the number present in the sequence? Yes, it is present in the sequenceA function named ‘contains_in_sequence’ checks to see if two values are same, and if they are equal, the function returns true. If the difference between the two values multiplied by a third value is greater than 0 and the difference between the values divided by the third value gives a reminder as 0, the function returns true, otherwise, returns false. Values are assigned to the three variables and ... Read More
To find the sum of cubes of the first n natural numbers, the code is as follows −Example Live DemoOutputThe sum of cubes of first 8 natural numbers is 1296A function named ‘sum_of_cubes’ is defined that initializes a sum to 0. Every natural number is multiplied by itself thrice (cube) and added to the initial sum. The limit is the value that is passed as a parameter to the function. The limit is defined outside the function and the function is called. Relevant output is displayed on the console.
To find the sum of the first n natural numbers who are not powers of a specific number ‘k’, the code is as follows −Example Live DemoOutputThe sum of fist 20 natural numbers that are not powers of 3 is 198A function named ‘sum_of_nums’ is defined and it calculates the sum of natural numbers that are not powers of a certain value. The number and the non-power number are passed as parameters to this function. Outside the function, a value each for n and k is defined and the function is called on these values. Relevant output is displayed on the ... Read More
A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame { private JButton button; public RollOverButtonTest() { setTitle("RollOverButton Test"); button = new JButton("Rollover Button"); button.addMouseListener(new MouseAdapter() { ... Read More