Archives for 

Basic Java Example

Even/Odd program in Java

/* Even Odd Number Example */   import java.io.*; public class EvenOdd { public static void main(String[] args) { //create an array of 10 numbers int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10}; for(int i=0; i < numbers.length; i++){ /* * use modulus operator to check if the number is even or odd. * If we divide any […] Continue reading →

Factorial in Java

/* Java Factorial Example */ import java.io.*; class fact { public static void main(String[] args) { int number = 5; /* * Factorial of any number is !n. * For example, factorial of 4 is 4*3*2*1. */ int factorial = number; for(int i =(number – 1); i > 1; i–) { factorial = factorial * […] Continue reading →

Calculate Rectangle Area in Java

/* Calculate Rectangle Area using Java Example */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;   public class RectArea { public static void main(String[] args) { int width = 0; int length = 0; try { //read the length from console BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Please enter length of a rectangle”); length = Integer.parseInt(br.readLine()); […] Continue reading →

Calculate Circle Perimeter in Java

/* Calculate Circle Perimeter using Java Example */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class CirPeri { public static void main(String[] args) { int radius = 0; System.out.println(“Please enter radius of a circle”); try { //get the radius from console BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); radius = Integer.parseInt(br.readLine()); } //if invalid value was entered […] Continue reading →

Area of a Circle in Java

/*Calculate  Area Of a Circle in Java Example*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class cir{ public static void main(String[] args) { int radius = 0; System.out.println(“Please enter radius of a circle”); try { //get the radius from console BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); radius = Integer.parseInt(br.readLine()); } //if invalid value was entered catch(NumberFormatException […] Continue reading →

Swap number in Java

/* Swap Numbers Java Example*/ import java.io.*; class sw{ public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println(“Before Swapping”); System.out.println(“Value of num1 is :” + num1); System.out.println(“Value of num2 is :” +num2); //swap the value swap(num1, num2); } private static void swap(int num1, int num2) { int temp = […] Continue reading →

Reverse digits in Java

/* Reverse Number using Java*/   import java.io.*; public class reverse { public static void main(String[] args) { //original number int number = 3456; int reversedNumber = 0; int temp = 0; while(number > 0)    {        //use modulus operator to strip off the last digit temp = number%10;    //create the reversed number […] Continue reading →

Finding HCF and LCM of Two Numbers In Java

Write a program to LCM and HCF of two integers.   import java.io.*; class HCM { int a,b,num,hcd,hcm; void lcm() { a=Integer.parseInt(System.console().readLine(“enter the first no.=”)); b=Integer.parseInt(System.console().readLine(“enter the 2nd   no.=”)); int m=a*b; if(a>b) { num=a; } else { num=b; } for (int i = 1; i <= num; i++) { if ((a%i==0) && (b%i==0)) { hcd=i; […] Continue reading →