Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday, 13 April 2017

Basic Programs in Java

Now here we are going to discuss some basic programs of java which clear your logical concept and it is very important for interview also.


Basic Programs in Java


(1) Java Hello World Program

This is first program in java. In this java program, we will  print "Hello World" as output. It is very simple and first program in java.

For example :

class HelloWorldExample
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

output : Hello World


(2) Sum of Two Numbers in Java

In this java program we are going to use three variables , two variables are initialized i.e contains integer values and the addition or sum of these two numbers store in third variable.

For example :

class SumOfTwoNumberExample
{
public static void main(String args[])
{
int a = 20;
int b = 30;
int c = a + b;
System.out.println("Sum is : "+c);
}
}

output : Sum is : 50


(3) Java Program to Print Even and Odd Numbers

In this program we will find the even or odd numbers. 

Even number is a number which is divisible by 2 and even numbers start with 2,4,6,8...etc . Odd number is a number which is not divisible by 2 and odd numbers is start with 1,3,5,7,9....etc.

For example : 

class EvenOddExample
{
public static void main(String args[])
{
//Let's create an array of 10 numbers to check number is even or odd
int a[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i<a.length; i++)
{
if(a[i]%2==0)//Use modulus(%) operator to check even or odd
{
System.out.println(a[i]+"is even number");
}
else
{
System.out.println(a[i]+"is odd number");
}
}
}
}

output : 1 is odd number
              2 is even number
              3 is odd number
              4 is even number
              5 is odd number
              6 is even number
              7 is odd number
              8 is even number
              9 is odd number
              10 is even number


(4) Write a program to swap two numbers

Java swapping is very easy program and java swapping can be done with third variable or temporary variable and without third variable or temporary variable. But in this program, we use third variable or temporary variable.

For example :

class SwappingWithThirdVariable
{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("before swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
System.out.println();

int temp = a;
a = b;
b = temp;

System.out.println("after swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
}
}

output : before swapping
              value of a 10
              value of b 20
                                  
              after swapping
              value of a 20
              value of a 10



 (5) Program to swap two number without using third variable 

In this program we can swap two number without using third variable or temporary variable. For examp 

class SwappingWithoutThirdVariable
{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("before swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
System.out.println();

a = a + b;
b = a - b;
a = a - b;

System.out.println("after swapping");
System.out.println("value of a "+a);
System.out.println("value of b "+b);
}
}

output : before swapping
              value of a 10
              value of b 20
                                  
              after swapping
              value of a 20
              value of a 10



(6) Palindrome in Java

Palindrome number is a number that is remains the same after reverse. for example : 121, 747, 212, 545, 64646, etc. 

For example : 

class PalindromeExample
{
public static void main(String args[])
{
int r, temp, sum=0;
int n = 747;//number to be checked for palindrome 
temp = n;//temporary variable hold the number
while(n>0)
{
r = n%10;
sum = (sum*10)+r;
n = n/10;
}

if(temp==sum)
{
System.out.println("Number is palindrome");
}
else
{
System.out.println("Number is not palindrome");
}
}
}

output : Number is palindrome


(7) Armstrong Number in Java

An Armstrong number is a number that is equal to the sum of cubes of its digits for example :

153 is an Armstrong number because, 

1^3+5^3+3^3 = 153 i.e 

1+125+27 = 153.

For example :

class ArmstrongNumberExample
{
public static void main(String args[])
{
int a, temp, b = 0;
int c = 153;//Number to be checked for Armstrong 

temp = c;
while(c>0)
{
a = c%10;
c = c/10;
b = b+(a*a*a);
}
if(temp==b)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}

output : Armstrong Number


(8) Java Program for Prime Number

Prime number is number which is divisible by 1 and itself. Prime number cannot be divisible by other number except 1 or itself. 

Prime number start from 2, 3, 5, 7, 11.....etc.

0 and 1 are not prime number.

For example :

class PrimeNumberExample
{
public static void main(String args[])
{
//check prime number from 2 to 15 number
for(int i =2; i<=15; i++)
{
for(int j = 2; j<=i; j++)
{
if(j==i)
{
System.out.println(i);
}
if(i%j==0)
{
break;
}
}
}
}


output: 2
              3
              5
              7
             11
             13


(9) Factorial Program in Java

Factorial of any number is the product of an integer.

e.g  factorial of 4 and 5

4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120

For example :

class FactorialExample
{
public static void main(String args[])
{
int i, fact = 1;
int n = 6;//factorial of 6!
for(i =1; i<=n; i++)
{
fact = fact*i;
}
System.out.println("factorial of " +n+" is " + fact);
}
}

output : factorial of 6 is 720

Share:

1 comment:

  1. Nice article. thanku for sharing good post.Top 50 Core Java Coding / Programming Questions And Answers visit Java Coding Examples

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate