DIGIT-ARC read-only archive · snapshot: July 23, 2026

Post Ur Java Programs

4 posts · originally from https://geek.digit.in/community/threads/post-ur-java-programs.101069/

T
thinkdigit123456 Original post #1
Nov 1, 2008 at 6:09 PM
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));
}

}
QwertyManiac
Nov 1, 2008 at 6:19 PM
I don't know about interviews but indenting code is hell necessary otherwise.
B
Bandu #3
Nov 1, 2008 at 10:47 PM
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.
C
Nov 5, 2008 at 8:22 PM
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.