Friday, August 27, 2010

JAVA PROGRAM WITHOUT USING BIG INTEGER


/* PROGRAM TO CALCULATE THE POWER(2,5-DIGIT NUMBER) WITHOUT USING BIG INTEGER AND MATH FUNCTIONS*/
/*This works for the highest 5 digit number 99999 also*/
/* Hope you make the best of it*/

import java.lang.*;
import java.io.*;


class compute
{
public static void main(String args[])

{

int i,rem=0,inc=1,inc1=0,j,value,carry=0,k,flag=0;;
int num[]=new int[60000];
int oldvalue[]=new int[60000];

try{
    DataInputStream in=new DataInputStream(System.in);
    System.out.print("Enter the number:");
    int n=Integer.parseInt(in.readLine());
    num[0]=1;
        for(i=1;i<=n;i++)
        {
        for(j=0;j<inc;j++)
        {
        num[j]=num[j]*2;

    if(num[j]>9)
        { value=num[j]%10;
        oldvalue[inc1]=value+carry;

         carry=1;
         inc1++;

        if(inc==inc1)
        {
        oldvalue[inc1++]=carry;
        carry=0;

        }

        }
    else
        {

        oldvalue[inc1]=num[j]+carry;
        inc1++;
        carry=0;
        }



        }

for(k=0;k<inc1;k++)
{
num[k]=oldvalue[k];
}
inc=inc1;

carry=0;
inc1=0;
}
System.out.print("\n power(2,"+n+")=");
for(k=inc-1;k>=0;k--)
{
System.out.print(num[k]);
}
}/*try block*/

catch(IOException e){}


}
}

No comments:

Post a Comment

Previous Post Home