on Leave a Comment

Java Program to Check Whether a Number is Palindrome or Not

This Java program checks whether a number is palindrome or not. A palindrome number is a number that is same after reverse.

Step to check whether a number is palindrome or not

- Get the number to check whether it is palindrome or not.
- Store that number in temporary variable.
- Reverse the number.
- Compare reversed number with temporary number.
- If both are same then print number is palindrome
- Else print number is not palindrome.

Java Program to Check Palindrome Number using for loop


class CheckPalindrome
{
 public static void main(String[] args)
 {
  int n = 151, rev = 0, rem;
  int orig = n;
                 for( ; n != 0; n = n/10)
  {
   rem = n % 10;
                        rev = rev * 10 + rem;
  } 
  if(orig == rev)
   System.out.println(orig+" is palindrome number");
  else
   System.out.println(orig+"is not palindrome number");
 }
}

Output:


151 is palindrome number


Java Program to Check Palindrome Number using while loop


class CheckPalindrome
{
 public static void main(String[] args)
 {
  int n = 282, rev = 0, rem;
  int orig = n;
                while(n != 0)
  {
   rem = n % 10;
                        rev = rev * 10 + rem;
   n = n/10;
  } 
  if(orig == rev)
   System.out.println(orig+" is palindrome number");
  else
   System.out.println(orig+"is not palindrome number");
 }
}

Output:


282 is palindrome number

on Leave a Comment

Java Program to Print Fibonacci Series

In Fibonacci series, next term is sum of previous two terms. Fibonacci series is followed by 0 and 1.

Example of Fibonacci series is 0  1  1  2  3  5  8  13  21  34.....

Java program to print Fibonacci series without recursion


public class FibonacciSeries
{
	public static void main(String[] args)
	{
	    int a1 = 0, a2 = 1, n = 10;
	    System.out.println("Fibonacci series of first "+n+"th terms:");
	    for(int i = 1; i<=10; i++)
	    {
		System.out.print(a1 + "  ");
                int sum = a1 + a2;
                a1 = a2;
                a2 = sum;
	     }
	}
}

Output:


Fibonacci series of first 10th terms:
0  1  1  2  3  5  8  13  21  34


Java program to print Fibonacci series using recursion


public class FibonacciSeries
{
	static int a1 = 0, a2 = 1, n = 10, sum = 0;
        static void showFibonacci(int count)
	{    
            if(count>0)
	    {    
               sum = a1 + a2;    
               a1 = a2;    
               a2 = sum;    
               System.out.print(" "+sum);   
               showFibonacci(count-1);    
            }    
        } 
	public static void main(String[] args)
	{
	      System.out.println("Fibonacci series of first "+n+"th terms:");
	      System.out.print(a1+" "+a2);
	      showFibonacci(n-2);
	}
}

Output:


Fibonacci series of first 10th terms:
0  1  1  2  3  5  8  13  21  34

on Leave a Comment

Command Line Arguments in Java Programming

Command line arguments are the arguments that are passed to the program at the time of invoking it for execution. These java command line arguments are stored in String array (args) of main() method. Command line arguments are stored as strings. We can pass any number of arguments. 

Example of Command Line Arguments in Java

public class cmdarg
{
	public static void main(String[] args)
	{
		System.out.println("You have entered following strings:");
		for(int i = 0; i<(args.length); i++)
			System.out.println(args[i]);
	}
}

OUTPUT:


D:\programs>javac cmdarg.java
D:\programs>java cmdarg "alpha" "beta"
You have entered following strings:
alpha
beta
on Leave a Comment

Java Program to check Even or Odd Number

This java program checks whether a number even or odd. Even number is a number that is completely divided by 2 and odd number cannot completely divide by 2.

Java Program to check Even or Odd Number

import java.util.Scanner;
public class EvenOdd
{
        public static void main(String[] args)
        {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter a number");
                int x = sc.nextInt();
                if(x%2 == 0)
                        System.out.println(x+" is a even number");
                else
                        System.out.println(x+" is a odd number");
        }
}

OUTPUT 1:

Enter a number
59
59 is a odd number

OUTPUT 2:

Enter a number
86
86 is a even number

on Leave a Comment

Static Members in Java Language

Static members are class level members. The class members which are defined with static keyword is called static members. There is only one copy of static member for a class, so static members do not depend on objects. Static members are mainly used for memory management. 

Java Static Members

(1) Static Variable
(2) Static Method
(3) Static Block
(4) Static Nested Class

Static Variable

The member variables which are defined with static keyword are called static variables. Static member variables are defined inside a class and outside all instance member methods, constructors, or any block. A static variable cannot be local variable, if we do so, we will get compile time error "illegal start of expression". 

Static variables get space in memory when class loading into JVM and is available till the class removed from JVM.

Static variables can be accessed with class name 

Syntax:

<class-name>.<variable-name>;

Example of static variable in java

public class StaticVariable
{
        static String course = "JAVA";
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course);
        }
        public static void main(String[] args)
        {
                StaticVariable s = new StaticVariable("John", 32);
                s.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Static variables can be of any access modifier. 

Static Method

A static method always defined with static keyword. A static method belongs to class, not to objects. There is only one copy of static method for a class.  

Static methods cannot access instance member variables, they can only access static variables.  

Static methods get space in memory when class loading into JVM and is available till the class removed from JVM.

Static methods can be accessed with class name.

Example of static variable in java

public class StaticVariable
{
        static String course = "JAVA";
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
        }
       
        static void changeCourse()
        {
                course = "C#";
        }
        public static void main(String[] args)
        {
                StaticVariable s1 = new StaticVariable("John", 32);
                s1.showDetails();
               
                StaticVariable.changeCourse();
                StaticVariable s2 = new StaticVariable("Andrew", 10);
                s2.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

Static Block

Static block is defined with static keyword. Static block is used to initialize static members. Static block executes before main() function, when class is loading into JVM. 

Example of static variable in java

public class StaticVariable
{
        static String course;
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
        }
       
        static void changeCourse()
        {
                course = "C#";
        }
        public static void main(String[] args)
        {
                StaticVariable s1 = new StaticVariable("John", 32);
                s1.showDetails();
               
                StaticVariable.changeCourse();
                StaticVariable s2 = new StaticVariable("Andrew", 10);
                s2.showDetails();
        }
        static
        {
                course = "JAVA";
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

Static Nested Class

A nested class can be defined with static keyword. 

Example of static nested class in java

public class StaticVariable
{
        static class mainClass
        {
                static String course = "JAVA";
            private int roll_no;
            private String name;
            mainClass(String n, int r)
            {
                name = n;
                roll_no = r;
        } 
            public void showDetails()
            {
                   System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
            }
       
            static void changeCourse()
            {
                course = "C#";
            }
        }
        public static void main(String[] args)
        {
                StaticVariable.mainClass s1 = new StaticVariable.mainClass("John", 32);
s1.showDetails();
               
                StaticVariable.mainClass.changeCourse();
                StaticVariable.mainClass s2 = new StaticVariable.mainClass("Andrew", 10);
                s2.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

on Leave a Comment

What is Router in Networking?

Router is a hardware device that connects multiple devices through wired or wireless media. A router transfers packets from source to destination devices after analyzing the best path. Router is a layer 3 (network layer) device in OSI model. 

When a client sends packets to destination device, now firstly destination IP address is compared with client network. If destination IP address is in different network, it sends these packets to the default gateway device. And router is located in gateway in most cases. A gateway is where many networks meet each other.

A router has its own memory and operating system is embedded in routers memory. Examples of router operating systems are Cisco (IOS), DD-WRT etc. A router also contains a processor and several I/O devices.

How Does a Router Work?

A router keeps a routing table to find the best path to transfer packets. Routers use some algorithms for transferring packets and it also depends on distance and cost. A routing table also contains a list of all routers connected to that router. 

A packets travels through multiple routers to reaching its destination. Suppose router have multiple paths to transfer packets to its destination, but router always select one path for transferring packets. This selection process depends on distance and cost.

on Leave a Comment

Operators in Java

Java has rich set of operator to manipulate variables. To perform mathematical operation on data, operators are necessary. 

Java operators are classified into following categories:

(1) Arithmetic operator
(2) Relational operator
(3) Logical operator
(4) Bitwise operator
(5) Conditional operator
(6) Assignment operator
(7) Misc operator

Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. 

Here is a list of arithmetic operators, assume A = 3, B = 2.

Arithmetic operators in java


Relational Operators

Relational operators are mainly used in decision making statements.

This table shows relational operators,


Relational operators in java

Logical Operators

This table shows list of logical operators,


Logical operators in java


Bitwise Operators

This table shows the list of bitwise operators,



Assignment Operators

This table shows the list of assignment operators supported by java.


Assignment operators in java

Miscellaneous Operators

There are some other operators supported by java.

Conditional Operators

Conditional operator is also known as ternary operator. 

Syntax:

expression1 ? expression2 expression3

If expression1 is true, then expression2 otherwise expression3.

instanceOf Variable

This operator is used for object reference variables. The operator checks whether the object is of class type or interface type.


on Leave a Comment

Java Program to Check Whether a number is Prime or Not

In this java program, we check whether a number is prime or not. A prime number is only divisible by 1 and itself.

Java Program to Check Whether a number is Prime or Not

public class PrimeNumber
{
 public static void main(String[] args)
 {
  int n = 83;
  boolean flag = false;
  for(int i = 2; i &lt;= n/2; ++i)
  {            
     if(n % i == 0)
     {
       flag = true;
       break;
     }
  }
  if (!flag)
      System.out.println(n + " is a prime number.");
  else
      System.out.println(n + " is not a prime number.");
 }
}

OUTPUT:

83 is a prime number.

Java Program to Check Whether a number is Prime or Not Using Scanner

import java.util.*;
public class PrimeNumber
{
 public static void main(String[] args)
 {
  Scanner s = new Scanner(System.in);
  System.out.println("Enter a number ");
  int n = s.nextInt();
  boolean flag = false;
  for(int i = 2; i &lt;= n/2; ++i)
  {
     if(n % i == 0)
     {
       flag = true;
       break;
     }
  }
  if (!flag)
     System.out.println(n + " is a prime number.");
  else
  System.out.println(n + " is not a prime number.");
 }
}

OUTPUT:

Enter a number

97

97 is a prime number.