Skip to main content

Posts

Showing posts from January, 2018

Java Program To Check Whether a Number is Palindrome or Not

Welcome back to my blog! In this post, you will learn how to check whether a number is a palindrome or not in Java. A number is called palindrome if reads same when reversed. For example: 121, 111, 98789, etc. So let's get started! Algorithm: Enter the number which has to be checked. Assign the number to a temporary variable. Reverse the number. Compare the temporary number with the reversed number.  If both are same, print a message "The number is palindrome!" Else print "The number is not palindrome." Given below is the code to check whether the number is palindrome or not: class Palindrome { public static void main(String[] args) {      int rev, temp, sum = 0 ;      int n = 121 ; // The number to be checked whether it is palindrome or not.      temp = n;                  while (n > 0 ) {                     r = n% 10 ; // Getting the remainder                     sum = (sum * 10 ) + r;                       n = n/ 10 ;          

Fibonacci Series in Java

W elcome back to my blog! Today I will show you how to print a Fibonacci series in Java. Fibonacci series is a series of numbers where an addition of two numbers produces the next number. That next number is added to its previous number and again produces a next number and so on... The first two numbers of Fibonacci series are 0 and 1. Now let's see how to write the code for this: public class Series { public static void main(String args [])  {    int n1  = 0,  n2  = 1,  n3 ,  i ,  count  = 10;    System. out .println(n1+ " " +n2);      //printing 0 and 1       for(i=2; i<count; i++)      //loop starts from 2 because 0 and 1 are already printed     {      n3 =  n1 +  n2 ;      System. out .println( " " + n3 );      n1  =  n2 ;      n2  =  n3 ;    }    }         } It will produce the following output: 0 1 1 2 3 5 8 13 21 34 T hank you for coming to my blog. If you find this post useful, please share it wi

Inheritance in Java

W elcome back to my blog! In this post, you will learn about Inheritance in Java. Basically, Inheritance in Java is like a parent-child relationship. The child class acquires all the properties of the parent class. You can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. You can also add new methods and fields. Why Inheritance is used:  For method over-riding,  To reuse code. Syntax: class subclass-name extends superclass-name { //methods and fields } Here, the keyword "extends" is used to derive all the properties from the superclass, i.e, the parent class. Types of Inheritance in Java: 1. Single Inheritance, 2. Multilevel Inheritance, 3. Hierarchical Inheritance, 4. Multiple Inheritance, 5. Hybrid Inheritance. 1. Single Inheritance:     In single inheritance, a child class extends its parent class.

Multi-threading using thread in Java

W elcome back to my blog! In my last post, you learned about control statements in Java. In this post, you will learn about multi-threading using thread in Java. Just follow the steps given below: So let's get started! Step #1: Open Eclipse and select the directory where you want to save your work. Step #2: Go to File>New>Project. Step #3: Select Java Project, then click Next.. Step #4: Give the name of your project and click on Finish. Step #5: You will see a source folder named "src". Add a new class by right-clicking on the source folder>New>Class. Step #6: You will see a screen like this: Write the following code: class Multi_thread extends Thread { public void run() { System.out.println("My thread is running...");    public static void main(String args[]) { Multi_thread obj = new Multi_thread(); obj.start(); }     } } Step #7: Click on the Run button to run your program. C ongratulations!!! You have learned multi-threading using thread in