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;
}
}
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)
Sunday, February 7, 2010
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)