Sunday, January 23, 2011

Commonly used Http Codes

[sourcecode language="css"]
200 = OK
204 = No Content
302 = Found
305 = Use Proxy
400 = Bad Request
401 = Unauthorized
402 = Payment Required
403 = Forbidden
404 = Not Found
405 = Method Not Allowed
407 = Proxy Authentication Required
408 = Request Timeout
414 = Request URI Too Large
415 = Unsupported Media Type
500 = Internal Server Error
501 = Not Implemented
502 = Bad Gateway
503 = service Unavailable
504 = Gateway Timeout
505 = Http Version Not Supported


[/sourcecode]

Fibonacci Series

Find the fibonacci series based on the input number. If number is 8 then it should print like 1 1 2 3 5 8 13 21
package myrnd;

public class Fibonacci {

public static void main(String[] args) {

int num = 8;
int a, b = 0, c = 1;
for (int i = 0; i < num; i++) {
System.out.print(c+" ");
a = b;
b = c;
c = a + b;
}
}
}

Reverse of a number

Find the reverse of a number. eg if input number is 786 then output should be 687.
[sourcecode language="java"]
package myrnd;

public class ReverseNo {

// 786 : reverse : 687
public static void main(String[] args) {
int n = 78890;
int result = 0;
int remainder;
while (n > 0) {
remainder = n % 10;
result = result * 10 + remainder;
n = n / 10;
}
System.out.println("Result:" + result);
}
}

[/sourcecode]

Factorial of a number

[sourcecode language="java"]
package myrnd;

public class Factorial {

public static void main(String[] args) {
int a = 5;
int result = 1;
while (a > 0) {
result *= a;
a--;
}
System.out.println("result is:" + result);
// using recursion.
System.out.println("factorial :" + factorial(5));
}

// another way using recursion.
public static int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
}
return n;
}
}

[/sourcecode]

Some Simple java practice code

Find the sum of numbers between 100 and 200 those are divisible by 7.
[sourcecode language="java"]
package myrnd;

public class SpecificSum {

public static void main(String[] args) {
int result = 0;
for (int i = 100; i < 200; i++) {
if (i % 7 == 0) {
result += i;
}
}
System.out.println("Sum:" + result);
}
}

[/sourcecode]