Post Ur Java Programs

Status
Not open for further replies.
Hi friends i have started dis forum regarding interview based......
so post ur programs which u think dat s neccessary in d basis of interview.....

FACTORIAL

import java.math.BigInteger;
public class Factorial {
public static void main(String[] args) {

//-- BigInteger solution.
BigInteger n = BigInteger.ONE;
for (int i=1; i<=20; i++) {
n = n.multiply(BigInteger.valueOf(i));
System.out.println(i + "! = " + n);
}

//-- int solution (BAD IDEA BECAUSE ONLY WORKS TO 12).
int fact = 1;
for (int i=1; i<=20; i++) {
fact = fact * i;
System.out.println(i + "! = " + fact);
}
}
}


Fibonacci

public class Fibonacci {
public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}

}
 
Last edited:

Bandu

Journeyman
Nice thread.

Indentation will work if you enclose it within [ CODE] [ /CODE] tags.

I'll have to search my collection of such snippets. Will post more soon.
 

chandru.in

In the zone
Trust me except for freshers' interviews, algorithms and logic matter very very little in Java interviews. What matters is understanding of OO, design patterns and popular frameworks.
 
Status
Not open for further replies.
Top Bottom