Hello world java program
As we have discussed all required detail to develop and execute a java program. Let us start java programming with first simple java program to print “Hello World”.
Create Hello world java program:
HelloWorld.java
/**
* This program is used to print "Hello World".
* @author javatutorialpoint
*/
public class HelloWorld {
public static void main(String args[]){
//This line will print "Hello World".
System.out.println("Hello World.");
}
}
|
Output:
To understand above java program better let us have a brief look on it:
1. class: is a keyword used to declare a class with specific name.
2. public: is an access modifier.
3. static: is a keyword which can be used with class, method, variable/constant , block. If it is used with a method that method is known as static method. For static method invocation no need for object creation , method will be associate with class.
4. void : return type of the method , void means it does not return any value.
5. main : method is invoked by JVM, as it is static no need to create an object. It represent the program’s startup.
6. String args[] : represents the array of String type which is used in command line arguments.
7. System.out.println():
1. System is a final class and its all members are static.
2. out is a public final static member of System class. out is of PrintStream type.
3. println() is the method of PrintStream class.
Program to print alphabets both in small and capital.
/**
* This program is used to print alphabets both in small and capital.
* @author javatutorialpoint
*/
public class Alphabets {
public static void main(String args[]){
char ch;
System.out.println("Small Alphabets: ");
for( ch = 'a' ; ch <= 'z' ; ch++ ){
System.out.println(ch);
}
System.out.println("Capital Alphabets: ");
for( ch = 'A' ; ch <= 'Z' ; ch++ ){
System.out.println(ch);
}
}
}
|
Output:
Small Alphabets:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Capital Alphabets:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
|
Program to perform arithmetic operations.
/**
* This program is used to perform arithmetic operations.
* @author javatutorialpoint
*/
public class ArithmeticOperations {
/**
* This method is used to add two numbers.
* @param num1
* @param num2
*/
static void addition(int num1, int num2){
System.out.print("Addition of given numbers = ");
System.out.println(num1 + num2);
}
/**
* This method is used to subtract two numbers.
* @param num1
* @param num2
*/
static void subtraction(int num1, int num2){
System.out.print("Subtraction of given numbers = ");
System.out.println(num1 - num2);
}
/**
* This method is used to multiply two numbers.
* @param num1
* @param num2
*/
static void multiplication(int num1, int num2){
System.out.print("Multiplication of given no.s = ");
System.out.println(num1 * num2);
}
/**
* This method is used to divide two numbers.
* @param num1
* @param num2
*/
static void division(int num1, int num2){
System.out.print("Division of given given no.s = ");
System.out.println(num1 / num2);
}
/**
* This method is used to modulo divide two numbers.
* @param num1
* @param num2
*/
static void moduloDivision(int num1, int num2){
System.out.print("ModuloDivision of given no.s = ");
System.out.println(num1 % num2);
}
public static void main(String args[]){
//method call from main method
addition(20, 10);
subtraction(40, 30);
multiplication(20, 30);
division(20, 4);
moduloDivision(20, 3);
}
}
|
Output:
Addition of given numbers = 30
Subtraction of given numbers = 10
Multiplication of given numbers = 600
Division of given given numbers = 5
ModuloDivision of given numbers = 2
|
Program to find that given number is Armstrong or not.
/**
* This program is used to find that given number is Armstrong or not.
* @author javatutorialpoint
*/
public class ArmstrongNumber {
/**
*This method is used to find that given number is Armstrong or not
*@param num
*/
static void armstrong(int num){
int newNum = 0, reminder, temp;
temp = num;
//find sum of all digit's cube of the number.
while(temp != 0){
reminder = temp % 10;
newNum = newNum + reminder*reminder*reminder;
temp = temp/10;
}
//Check if sum of all digit's cube of the number is
//equal to the given number or not.
if(newNum == num){
System.out.println(num +" is armstrong.");
}else{
System.out.println(num +" is not armstrong.");
}
}
public static void main(String args[]){
//method call
armstrong(407);
}
}
|
Output:
Program to check that given number is even or odd.
/**
* This program is used to check that given number is even or odd.
* @author javatutorialpoint
*/
public class EvenOrOdd {
/**
* This method is used to check that given no is even or odd.
* @param num
*/
static void evenOdd(int num){
if(num % 2 == 0){
System.out.println("Given number is even.");
}else{
System.out.println("Given number is odd.");
}
}
public static void main(String args[]){
//method call, no need of object.
evenOdd(123);
}
}
|
Output:
Program to calculate Factorial of given number.
/**
* This program is used to calculate Factorial of given number.
* @author javatutorialpoint
*/
public class Factorial {
/**
* This method is used to calculate Factorial of given no.
* @param num
*/
static void factorialNumber(int num){
int fact = 1;
//Factorial of 0 and 1 is 1.
if(num == 0 || num == 1){
System.out.println("factorial of " + num + " is 1.");
}
//for calculating factorial, number should be non negative.
if(num > 0){
//calculate factorial.
for(int i = 2; i <= num; i++){
fact = fact*i;
}
System.out.println("factorial of " + num + " is " +fact);
}else{
System.out.println("no. should be non negative.");
}
}
public static void main(String args[]){
//method call
factorialNumber(5);
}
}
|
Output:
Program to find factorial of given number by recursion.
/**
* This program is used to find factorial of given number by recursion.
* @author javatutorialpoint
*/
public class FactorialByRecursion {
/**
* This method is used to find factorial of given no. by recursion.
* @param num
* @return int
*/
static int fact(int num){
//Factorial of 0 is 1.
if (num == 0)
return 1;
else
return num * fact(num-1);
}
public static void main(String args[]){
int num = 5, factorial;
//method call
if(num > 0){
factorial = fact(num);
System.out.println("Factorial of "+num+" is "+ factorial);
}else{
System.out.println("Number should be non negative.");
}
}
}
|
Output:
Program to print fibonacci series.
/**
* This program is used to print fibonacci series.
* @author javatutorialpoint
*/
public class FibonacciSeries {
/**
* This method is used to print fibonacci series.
* @param num
*/
static void fibonacci(int num){
int f1, f2=0, f3=1;
for(int i=1;i <=num;i++){
System.out.println(f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
public static void main(String args[]){
int num = 6;
//method call
if(num > 0){
fibonacci(num);
}else{
System.out.println("No. should be greater than zero.");
}
}
}
|
Output:
Program to find that given number is Palindrome or not.
/**
* This program is used to find that given number is Palindrome or not.
* @author javatutorialpoint
*/
public class PalindromeNumber {
static void palindrome(int num){
int newNum = 0, reminder, temp;
temp = num;
//find sum of all digit's of the number.
while(temp != 0){
reminder = temp % 10;
newNum = newNum*10 + reminder;
temp = temp/10;
}
//Check if sum of all digit's of the number
//is equal to the given number or not.
if(newNum == num){
System.out.println(num +" is palindrome.");
}else{
System.out.println(num +" is not palindrome.");
}
}
public static void main(String args[]){
//method call
palindrome(12321);
}
}
|
Output:
Program to find that given number is prime or not.
/**
* This program is used to find that given number is prime or not.
* @author javatutorialpoint
*/
public class PrimeNumber {
/**
* This method is used to find that given number is prime or not.
* @param num
*/
static void primeNumber(int num){
int count = 0;
//0 and 1 are not prime numbers.
if(num == 0 || num == 1){
System.out.println(num + " is not prime.");
}else{
for(int i = 1; i <= num/2; i++){
if(num % i == 0){
count++;
}
}
if(count > 1){
System.out.println(num + " is not prime.");
}else{
System.out.println(num + " is prime.");
}
}
}
public static void main(String args[]){
//method call
primeNumber(37);
}
}
|
Output:
Program to swap two numbers without using third or temp variable.
/**
* This program is used to swap two numbers without using third variable.
* @author javatutorialpoint
*/
public class SwapNumbers {
/**
* This method is used to swap no.s without using third variable.
* @param num1
* @param num2
*/
static void swapNumbers(int num1, int num2){
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping: "+ num1 + " and " + num2);
}
public static void main(String args[]){
int num1 = 20;
int num2 = 30;
System.out.println("Before swapping:"+ num1 + " and " + num2);
//method call
swapNumbers(num1, num2);
}
}
|
Output:
Before swapping no.s are : 20 and 30
After swapping no.s are : 30 and 20
|