Sunny Number in Java

Sunny Number

A number is called a sunny number if the number next to the given number is a perfect square. In other words, a number N will be a sunny number if N+1 is a perfect square.

Let’s understand it through an example.Suppose, we have to check if 80 is a sunny number or not. Given, N=80 then N+1 will be 80+1=81, which is a perfect square of the number 9. Hence 80 is a sunny number.

Let’s take another number 10.

Given, N=10 then N+1 will be 10+1=11, which is not a perfect square. Hence 10 is not a sunny number.

Steps to Find Sunny Number

The logic is very simple. To find the sunny number, we need only to check whether N+1 is the perfect square or not.

  1. Read or initialize a number (num).
  2. Add 1 to the given number i.e. num+1.
  3. Find the square root of num+1.
  4. If the square root is an integer, the given number is sunny, else not a sunny number.
[mai mult...]

Automorphic Number Program in Java

A number is called an automorphic number if and only if the square of the given number ends with the same number itself. For example, 25, 76 are automorphic numbers because their square is 625 and 5776, respectively and the last two digits of the square represent the number itself. Some other automorphic numbers are 5, 6, 36, 890625, etc.

[mai mult...]

Fascinating Number in Java

Multiplying a number by two and three separately, the number obtained by writing the results obtained with the given number will be called a fascinating number. If the result obtained after concatenation contains all digits from 1 to 9, exactly once.

In other words, we can also say that a number (n) may be a fascinating number if it satisfies the following two conditions:

  • If the given number is a 3 or more than three-digit
  • If the value getting after concatenation contains all digits from 1 to 9, exactly once.

For example, 192, 1920, 2019, 327, etc. Let’s understand the concept of fascinating numbers through an example.

[mai mult...]

Java Program to Calculate Compound Interest

Compound interest is calculated using the following formula:

P (1 + R/n) (nt) - P

Here P is principal amount.
R is the annual interest rate.
t is the time the money is invested or borrowed for.
n is the number of times that interest is compounded per unit t, for example if interest is compounded monthly and t is in years then the value of n would be 12. If interest is compounded quarterly and t is in years then the value of n would be 4.

Before writing the java program let’s take an example to calculate the compound interest. Let’s say an amount of $2,000 is deposited into a bank account as a fixed deposit at an annual interest rate of 8%, compounded monthly, the compound interest after 5 years would be:

P = 2000.
R = 8/100 = 0.08 (decimal).
n = 12.
t = 5.

Let’s put these values in the formula.

Compound Interest = 2000 (1 + 0.08 / 12) (12 * 5) – 2000 = $979.69

So, the compound interest after 5 years is $979.69.

[mai mult...]

Java Program to check Vowel or Consonant using Switch Case

The alphabets A, E, I, O and U (smallcase and uppercase) are known as Vowels and rest of the alphabets are known as consonants. Here we will write a java program that checks whether the input character is vowel or Consonant using switch case in java.

In this program we are not using break statement with cases intentionally, so that if user enters any vowel, the program continues to execute all the subsequent cases until Case 'U' is reached and thats where we are setting up the value of a boolean variable to true. This way we can identify that the alphabet entered by user is vowel or not.

[mai mult...]

Java Program to check Leap Year

Before we see the program, lets see how to determine whether a year is a leap year mathematically:
To determine whether a year is a leap year, follow these steps:
1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4. The year is a leap year (it has 366 days).
5. The year is not a leap year (it has 365 days).

[mai mult...]

How to Read and Print an Integer value in Java

In below program, the syntax and procedures to take the integer as input from the user is shown in Java language. Steps:

  1. The user enters an integer value when asked.
  2. This value is taken from the user with the help of nextInt() method of Scanner Class. The nextInt() method, in Java, reads the next integer value from the console into the specified variable. Syntax:
  3. variableOfIntType = ScannerObject.nextInt();
  4. where variableOfIntType is the variable in which the input value is to be stored. And ScannerObject is the beforehand created object of the Scanner class.
    1. This entered value is now stored in the variableOfIntType.
    2. Now to print this value, System.out.println() or System.out.print() method is used. The System.out.println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console. Whereas System.out.print() method, in Java, prints the value passed as the parameter to it, on the console screen and the cursor remains on the next character of the last printed character on the console.
[mai mult...]

How to Generate Random Number in Java

In Java programming, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many applications use the OTP. The best example of random numbers is dice. Because when we throw it, we get a random number between 1 to 6.

In this section, we will learn what is a random number and how to generate random numbers in java

Random numbers are the numbers that use a large set of numbers and selects a number using the mathematical algorithm. It satisfies the following two conditions:

  • The generated values uniformly distributed over a definite interval.
  • It is impossible to guess the future value based on current and past values.
Generating Random Number in Java

In Java, there is three-way to generate random numbers using the method and classes.

  • Using the random() Method
  • Using the Random Class
  • Using the ThreadLocalRandom Class
  • Using the ints() Method (in Java 8)
Using the Math.random() Method

The Java Math class has many methods for different mathematical operations. One of them is the random() method. It is a static method of the Math class. We can invoke it directly. It generates only double type random number greater than or equal to 0.0 and less than 1.0. Before using the random() method, we must import the java.lang.Math class.

[mai mult...]

How to Compare Two Objects in Java

Java Object class is the super class of all the Java classes. All Java classes implements the Object class by default. The Java Object class provides the two important methods to compare two obiects in java, i.e. equals() and hashCode() method. In this section, we will learn how equals() and hashCode() method works. Along with this, we will also learn how to compare two objects in Java with proper examples.

Java provides the two methods of the Object class to compare the objects are as follows:

  • Java equals() Method
  • Java hashCode() Method
Java equals() Method

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.

Syntax:

  1. public boolean equals(Object obj)

The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.

It is also possible that an object is equal to another given object, then the equals() method follow the equivalence relation to compare the objects.

    • Reflexive: If x is a non-null reference, the calling of x.equals(x) must return true.
    • Symmetric: If the two non-null references are x and y, x.equals(y) will return true if and only if y.equals(x) return true.
    • Transitive: If the three non-null references are x, y, and z, x.equals(z) will also return true if x.equals(y) and y.equals(z) both returns true.
    • Consistent: If the two non-null references are x and y, the multiple calling of x.equals(y) constantly returns either true or false. It does not provide any information used in the comparison.
    • For any non-null reference x, x.equals(null) returns false.

In short, for any non-null reference say x and y, it returns true if and only if both references refer to the same object.Remember: When we override the equals() method, it is necessary to override the hashCode() method. Overriding follow the convention for the hashCode() method that states, the equal object must have equal hash code.

Example of equals() method

In the following example, we have created constructor of the Double and Long class and passes corresponding values, as an argument that stored in their objects, respectively.

After that, in the first println statement, we have invoked equals() method and parse an object y as a parameter that compares the object x and y. It returns false because x holds the double value and y holds the long value that is not equal.

Similarly, in the second println statement, we have invoked equals() method and parse the same value as in the constructor of the Double class. It returns true because the object of double class i.e. x holds the same value as we have passed in the equals() method.

[mai mult...]