Archives for 

Java Programming

Write a program to display Pascal’s Triangle in Java

//To display a Pascal’s Triangle   import java.util.Scanner; public class Pascaltriangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter no of rows in Pascal’s triangle : “); int rows = scanner.nextInt(); int nextNumber; System.out.println(” “); for (int a = 0; a < rows; a++) { nextNumber = 1; for(int b=0; […] Continue reading →

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 →