Thursday, February 23, 2012
Wednesday, February 22, 2012
MOBIUS function
M( N) = 1 if N = 1
= 0 if any prime factor of N is contained in N more than once
= ( -1)^ P if N is a product of p distinct prime factors
Example :
M( 78) = -1 ( for 78 = 2 * 3 * 13 M( 78) = ( -1)^ 3 = -1 )
M( 34) = 1 ( for 34 = 2 * 17 M( 34) = ( -1) ^2 = 1 )
M( 12) = 0 ( for 12 = 2 * 2 * 3 M( 12) = 0 for 2 appears two times)
M( 17) = -1 ( for 17 = 17 M( 17) = ( -1)^ 1 = -1 )
Write a program to input natural number N and output M( N).
import java.io.*;
class MobiusFn
{
BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
int n,a,p,c,q;
public MobiusFn()
{
a=2;
p=0;
c=0;
}
public void input() throws IOException
{
System.out.println("Enter a number ");
n=Integer.parseInt(ob.readLine());
}
public int primefac(int x)
{
if(x==1) return 1;
while(x>1)
{
c=0;
while(x%a==0)
{
c++;
p++;
System.out.println("Prime Fator is "+a);
x=x/a;
}
if(c>1) return 0;
a++;
}
q=(int)Math.pow(-1,p);
return q;
}
public void display()
{
int r=primefac(n);
if(r==0) System.out.println("Prime factor of N is contained in N more than once. Mobius function returns " + r );
else System.out.println("Mobius function returns value " + r);
}
public static void main(String arg[]) throws IOException
{
MobiusFn obj = new MobiusFn();
obj.input();
obj.display();
}
}
Number combination - Expected question 2012
Output : Given number 5
Combinations :
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
1 4
2 3
import java.io.*;
class numcombi
{
String str="";
int a=0;
int b=0;
public void combi() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number : ");
int num = Integer.parseInt(br.readLine());
System.out.println("Combinations :");
for(int i=1;i<=num/2;i++)
{
str="";
extract(num,i);
}
}
public void extract(int n, int m)
{
a=m;
str+=" "+a;
b=n-a;
System.out.println(str+" "+b);
if(b<=m)
{
return;
}
extract(b,m);
}
public static void main(String arg[]) throws IOException
{
numcombi obj= new numcombi();
obj.combi();
}
}
Sunday, February 19, 2012
Sum of two Binary numbers
class BinarySum
{
public int n,m,p,q,r;
public void input()
{
// two binary numbers added by converting into decimal
// and sum then converted into binary
Scanner br = new Scanner(System.in);
System.out.println(" Input a binary number n");
n=br.nextInt();
System.out.println(" Input a binary number m");
m=br.nextInt();
}
public void calculate()
{
p=bintodec(n);
q=bintodec(m);
if(p== -1 || q== -1)
System.out.println("INVALID BINARY VALUE ");
else
{
System.out.println("Binary number " + n + " in decimal is " + p);
System.out.println("Binary number " + m + " in decimal is " + q);
r=sumbin(p+q);
System.out.println("Sum of two binary number " + n + " and " + m + " is " + r);
}
}
public int bintodec(int x)
{
int a,s=0, i=0;
while(x>0)
{
a=x%10;
if(a>1) return -1;
s = s + (int)Math.pow(2,i)*a;
x=x/10;
i++;
}
return s;
}
public int sumbin(int x)
{
int a,s=0, i=0;
while(x>0)
{
a=x%2;
s = s + (int)Math.pow(10,i)*a;
x=x/2;
i++;
}
return s;
}
public static void main(String arg[])
{
BinarySum ob= new BinarySum();
ob.input();
ob.calculate();
}
}
Two decimal numbers converted into binary and sum in binary form
class BinaryAdd
{
public int n,m,p,q,r;
public void input()
{
// two decimal numbers converted into binary
// and sum is also converted into binary
Scanner br = new Scanner(System.in);
System.out.println(" Input a number n");
n=br.nextInt();
System.out.println(" Input a number m");
m=br.nextInt();
}
public void calculate()
{
p=decbin(n);
q=decbin(m);
System.out.println("Binary value for " + n + " is " + p);
System.out.println("Binary value for " + m + " is " + q);
r=sumbin(m+n);
System.out.println("Sum of two binary number " + p + " and " + q + " is " + r);
}
public int decbin(int x)
{
int a,s=0, i=0;
while(x>0)
{
a=x%2;
s = s + (int)Math.pow(10,i)*a;
x=x/2;
i++;
}
return s;
}
public int sumbin(int x)
{
int a,s=0, i=0;
while(x>0)
{
a=x%2;
s = s + (int)Math.pow(10,i)*a;
x=x/2;
i++;
}
return s;
}
public static void main(String arg[])
{
BinaryAdd ob= new BinaryAdd();
ob.input();
ob.calculate();
}
}
Decimal number to binary conversion
class dectobin
{
public int n,m,s,i,a;
public void input()
{
Scanner br = new Scanner(System.in);
System.out.println(" Input anumber n");
n=br.nextInt();
}
public void calculate()
{
s=0;
i=0;
m=n;
while(n>0)
{
a=n%2;
s = s + (int)Math.pow(10,i)*a;
n=n/2;
i++;
}
System.out.println("Binary value for " + m + " is " + s);
}
public static void main(String arg[])
{
dectobin ob= new dectobin();
ob.input();
ob.calculate();
}
}
ISC Practical ( Expected )
1. Binary Conversions
2. Hamming Distance
3. Mobius function
4. Sorting Rows / Columns of 2D Array
5. Finding maximum and minimum value of a 2D Array
6. Reversing words of a sentence
7. Sorting of words of a sentence
Sort words of a sentence
Read a single sentence which terminates with a full stop(.). The words are to be separated with a single blank space and are in lower case. Arrange the words contained in the sentence according to the length of the words in ascending order. If two words are of the same length then the word occurring first in the input sentence should come first. For both, input and output the sentence must begin in upper case.
Test your program for the following data and some random data.
INPUT : The lines are printed in reverse order.
OUTPUT : In the are lines order printed reverse.
INPUT : Print the sentence in ascending order.
OUTPUT : In the print order sentence ascending.
import java.io.*;
import java.util.*;
class sortlength
{
public static void main(String args[])throws IOException
{
String t,a;
int i, k,j=0, x=0, l, l1, l2, l3;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence");
a=ob.readLine();
a=a.toLowerCase();
a=a.substring(0,a.length()-1);
StringTokenizer str=new StringTokenizer(a);
l=str.countTokens();
String z[ ]=new String[l];
for(i=0;i
z[j++]=str.nextToken( );
for(i=0;i
{
for(k=0;k<(l-1);k++)
{ l2=z[k].length();
l3=z[k+1].length();
if(l2>l3)
{ t=z[k];
z[k]=z[k+1];
z[k+1]=t;
}
}
}
System.out.print((char)(z[0].charAt(0)-32)+z[0].substring(1)+" ");
for(i=1;i
System.out.print(z[i]+" ");
System.out.print(z[j-1]+".");
}
}
Friday, February 17, 2012
Program to convert a number in to words
Output - Five Hundred Sixty Eight
import java.util.*;
class numtoword
{
public static void main(String arg[])
{
String a[]={"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"};
String b[]={"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty"};
Scanner ob=new Scanner(System.in);
int n,m,p,q,r;
System.out.println("Enter a number less than 1000");
n=ob.nextInt();
if(n<1000)
{
m=n/100;
p=n%100;
if(m>0) System.out.print(a[m] + " Hundred ");
else System.out.print("");
if(p<20) System.out.println(a[p]);
else
{
q=p%10;
r=p/10;
System.out.println(b[r] + " " + a[q]);
}
}
}
}