import java.io.*;
public class NumberNames
{
static String A[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
static String B[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
static String ConvertToName(int number, int divisionR, int divisionC)
{
if(number==0)return "";
String div[][]={{"","thousand","lakh","crore","arab","hundred arab"},{"","thousand","million","billion","trillion","quadrillion"}};
String s;
if(number< 20)
return removeXtraSpc(A[number]+" "+div[divisionR][divisionC]+" ");
s=Integer.toString(number);
while(s.length()< 3)
s="0"+s;
int x=s.charAt(2)-48;
int y=s.charAt(1)-48;
int z=s.charAt(0)-48;
if(y> 1) s=B[y]+" "+A[x]+" ";
else s=A[10*y+x]+" ";
if(z!=0)
s=A[z]+" Hundred "+((x!=0||y!=0)?" And ":"")+s;
return removeXtraSpc(s+" "+div[divisionR][divisionC]);
}
public static String removeXtraSpc(String s)
{
int a=0;
while(a< s.length()-1)
{
if(s.charAt(a)==s.charAt(a+1)&&s.charAt(a)==' ')
{
String S1=s.substring(0,a);
String S2=s.substring(a+1,s.length());
s=S1+S2;
continue;
}
a++;
}
return s;
}
public static void main(String aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=null;
int x=0;
do{
try{
System.out.println("Enter a valid Positive Integer: ");
s=br.readLine();
if(s.length()>18) continue;
x=Integer.parseInt(s);
}catch(Exception e)
{
s=null;
}
}while(s==null||x< 0);
if(x==0)
s="Zero";
else if(x< 1000)
s=ConvertToName(x,0,0);
else
{
char c='.';
do{
System.out.println("Enter a choice: ");
System.out.println("1: For Name in Indian System");
System.out.println("2: For Name in International System");
s=br.readLine();
if(s.length()!=1) continue;
c=s.charAt(0);
}while(c< '1'||c> '2');
s=Integer.toString(x);
c=(char)(c-49);
int a=(c==0?2:3);
int b=1;
String S=s.substring(s.length()-3,s.length());
String t=ConvertToName(Integer.parseInt(S),0,0);
if(Integer.parseInt(S)< 100) t=" and "+t;
c=(char)(s.length()-3);
while(c-a> 0)
{
S=s.substring(c-a,c);
t=ConvertToName(Integer.parseInt(S),a-2,b++)+" "+t;
c=(char)(c-a);
}
if(c-a<=0)
{
S=s.substring(0,c);
t=ConvertToName(Integer.parseInt(S),a-2,b++)+" "+t;
}
s=t;
}//else closes here
s=removeXtraSpc(s);
System.out.println(s);
}//main() closes here
}//class closes here
This site has got programs, that u may need as an ISC student or a beginner. These are simple console based programs that can be run and understood by anyone having the idea of basic, core Java concepts. (java programs for beginners)
Thursday, January 12, 2012
Sunday, February 7, 2010
Shortest Function to find if a number is prime
class prime
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}
java program To find all the sets of consecutive natural numbers which add up to give the given number
import java.io.*;
class combi
{
private static int N;
private static int sum;
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String S;
System.out.print("Enter an integer: ");
S=br.readLine();
int i=Integer.parseInt(S);
combi A= new combi(i, 0);
A.calc();
}
public combi(int a, int b)
{
N=a;
sum=b;
}
public void calc()throws IOException
{
for(int i=1; i<=N/2;i++)
{
for(int j=i; j<=(N+1)/2; j++)
{
sum+=j;
if(sum==N)
{
for(int a=i; a<=j; a++)
{
System.out.print(a);
System.out.print(a
}
System.out.println("");
System.out.println("");
sum=0;
break;
}
}
sum=0;
}
}
}
class combi
{
private static int N;
private static int sum;
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String S;
System.out.print("Enter an integer: ");
S=br.readLine();
int i=Integer.parseInt(S);
combi A= new combi(i, 0);
A.calc();
}
public combi(int a, int b)
{
N=a;
sum=b;
}
public void calc()throws IOException
{
for(int i=1; i<=N/2;i++)
{
for(int j=i; j<=(N+1)/2; j++)
{
sum+=j;
if(sum==N)
{
for(int a=i; a<=j; a++)
{
System.out.print(a);
System.out.print(a
System.out.println("");
System.out.println("");
sum=0;
break;
}
}
sum=0;
}
}
}
java program To find whether two strings are anagrams
import java.io.*;
class anagrams
{
public void main(String args[])throws IOException
{
String s1,s2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Anagram Tester! Enter the strings:");
System.out.print("String 1: ");
s1=br.readLine();
System.out.print("String 2: ");
s2=br.readLine();
s1=removewhitespace(s1);
s2=removewhitespace(s2);
s1=s1.toLowerCase();
s2=s2.toLowerCase();
if(s1.length()!=s2.length())System.out.print("Not anagrams(no. of letters not same)!");
else
{
for(int i=0;i
{
for(int j=0;j
{
if(s1.charAt(i)==s2.charAt(j)){s2=delete(s2,j);break;}
}
}
if(s2.equals("")) System.out.print("The strings are anagrams!");
else System.out.print("Not anagrams "+s2);
}
}
public String removewhitespace(String s)
{
for(int i=0;i
{
if(s.charAt(i)==' ')s=delete(s,i);
}
return s;
}
public String delete(String s,int ind)
{
String str=s.substring(ind+1,s.length());
s=s.substring(0,ind);
s=s+str;
return s;
}
}
class anagrams
{
public void main(String args[])throws IOException
{
String s1,s2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Anagram Tester! Enter the strings:");
System.out.print("String 1: ");
s1=br.readLine();
System.out.print("String 2: ");
s2=br.readLine();
s1=removewhitespace(s1);
s2=removewhitespace(s2);
s1=s1.toLowerCase();
s2=s2.toLowerCase();
if(s1.length()!=s2.length())System.out.print("Not anagrams(no. of letters not same)!");
else
{
for(int i=0;i
for(int j=0;j
if(s1.charAt(i)==s2.charAt(j)){s2=delete(s2,j);break;}
}
}
if(s2.equals("")) System.out.print("The strings are anagrams!");
else System.out.print("Not anagrams "+s2);
}
}
public String removewhitespace(String s)
{
for(int i=0;i
if(s.charAt(i)==' ')s=delete(s,i);
}
return s;
}
public String delete(String s,int ind)
{
String str=s.substring(ind+1,s.length());
s=s.substring(0,ind);
s=s+str;
return s;
}
}
java program on Amicable numbers
class amicable
{
public static void main(String args[])
{
for(int i=2; i<=1000; i++)
{
for(int j=i+1; j<=1000; j++)
{
if(facSum(i)==j&&facSum(j)==i)
{ System.out.println(i+" and "+j+" are Amicable!");
}
}
}
}
public static int facSum(int a)
{
int c=0;
for(int s=1; s<=a/2; s++)
{
if(a%s==0)
c+=s;
}
return c;
}
}
Subscribe to:
Posts (Atom)