Tuesday, August 4, 2009

Simple Programs - Java Code

//FACTORIAL OF A NUMBER:
import java.io.*;
class Factorial1
{
public static void main(String[] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
double fact= 1;
System.out.println("Factorial of " +a+ ":");
for (int i= 1; i<=a; i++)
{
fact=fact*i;
}
System.out.println(fact);
}
catch (Exception e)
{
System.out.println("Array out of bounds exaception");
}
}
}
//PALINDROME NUMBER:
import java.io.*;
public class Palindrome
{
public static void main(String [] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num)
for (int i=0; i<=num; i++)
{
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " "); System.out.println(" "+ rev);
if(n == rev)
{
System.out.print("Number is palindrome!");
}
else
{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e)
{
System.out.println("Out of range!");
}
}
}
//AREA OF A RECTANGLE:
import java.io.*;
class RecArea
{
public static void main(String[] args)
{
int l=0; int w=0;
try
{
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter length of rectangle : ");
l = Integer.parseInt(br1.readLine());
System.out.println("Enter width of rectangle : ");
w = Integer.parseInt(br1.readLine());
int area = l*w;
System.out.println("Area of Rectangle : "+area);
int perimiter = 2*(l+w);
System.out.println("Perimeter: " + perimiter);
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
//PRINT A TRIANGLE:
import java.io.*;
class triangle
{
public static void main(String[] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
for (int i=1; i{
for (int j=1; j<=i;j++ )
{
System.out.print("*");
}
System.out.println("");
}
}
catch(Exception e)
{}
}
}

No comments:

Post a Comment