Wednesday, January 16, 2008

Request a Program Here

Here u can Ask a question for a Program and i'll make that program and publish it here

HCF and LCM of 2 numbers

import java.io.*;
class lowhigh
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a,b,c,i,j,t,k,p,lcm,hcf=1;
System.out.println(" Enter any 3 number ");
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
c = Integer.parseInt(br.readLine());
p = a*b*c;
for(i=1;i<=p;i++)
{
j = a%i;
t = b%i;
k = c%i;
if(j==0&&t==0&&k==0)
{
hcf = i;
}
}
lcm = p/hcf;
System.out.println(" the lcm is " + lcm);
System.out.println(" the hcf is " + hcf);
}
}

Find the Longest Word in the String

import java.io.*;
class LongestWord
{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String");
String s=in.readLine();
int l=s.length();
String wo="",w="";
s=s+" ";
int max=0,x;
for(int i=0;i {
char ch=s.charAt(i);
if(ch!=' ')
{
wo = wo+ch;
}
else
{
x=wo.length();
if(x>max)
{
max=x;
w=wo;
}
wo="";
}
}
System.out.println("Longes Word is " + wo);
}
}

Count the number of spaces in a String

This Program count the number of spaces in a String

import java.io.*;
class spaces
{
public static void main(String args[])throws IOException
{
int c=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter a String ");
String s = br.readLine();
int l= s.length();
for(int i=0;i{
char ch=s.charAt(i);
if(ch==' ')
c=c+1;
}
System.out.println("Number of spaces " + c);
}
}

Palindrom String

Check a String is palindrom or not

import java.io.*;
class palindrom
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter a String ");
String s = br.readLine();
int l=s.length();
String x = "";
int ch;
ch=0;
for(int i=l-1;i>=0;i--)
{
x=x+s.charAt(i);
}
if(x.compareTo(s)==0)
System.out.println(" its A palindrom");
else
System.out.println(" its not a palindrom ");
}
}

Pattern Alphabetic Diamond

A

A B

A B C

A B C D

A B C

A B

A

This is the output

class ABCD
{
public static void main(String a[])
{
char c;
for(int i=1;i<=7;i++)
{
int x = 65;
if(i<5)
{
for(int j = 4 ; j>=i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
c=(char)x;
System.out.print(c + " " );
x=x+1;
}
}
else
{
for(int j=4;j<=i;j++)
{
System.out.print(" ");
}
for(int j=7;j>=i;j--)
{
c=(char)x;
System.out.print(c + " ");
x = x + 1;
}
}
System.out.println();
}
}
}

Pattern Inverted Triangle

+++++

++++

+++

++

+

this is the output of this program

class LeftTriangle
{
public static void main()
{
for(int i=0;i<5;i++)
{
for(int j=5;j>i;j--)
{
System.out.print("+");
}
System.out.println();
}
}
}