Archive

Archive for January 1, 2009

Java Program to find factorial of a given number without recursion – Q2

January 1, 2009 Leave a comment

Q2: Java Program to find factorial of a given number without recursion

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

For example:
5! =     5*4*3*2*1 = 120
6! = 6*5*4*3*2*1 = 720

class fact{
	public static void main(String args[]){
		int num;
		long fac = 1;
		num = Integer.parseInt(args[0]);
		for(int i=1; i<=num; i++){
			fac *= i;
		}
		System.out.println("Factorial of " + num + " is: " + fac);
	}
}

… from College notes (BCA/MCA assignments):


Advertisement

Java Program to find max of three numbers – Q1

January 1, 2009 Leave a comment

Q1: Java Program to find max of three numbers

class MaxThree{
	public static void main(String args[]){
		int a = Integer.parseInt(args[0]);
		int b = Integer.parseInt(args[1]);
		int c = Integer.parseInt(args[2]);
		int x;
		x = ((a>b) && (a>c)) ? a : ( (b > a) && (b > c) ) ? b : c;
		System.out.println(x + " is largest.");
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: