Sunday 3 March 2013

CALCULATE MASSIVE NUMBER 2^3000

Hey friends,
Here is a  very interesting program in C that enables you to calculate a massive number like 2^1000 or even 2^3000. This program runs perfectly in turbo cpp.
Many a times we encounter a problem like through programming we have to calculate the sum of all digits of 2^1000. Initially this could seem to you a bit messed up, but once you have understood this program, it becomes very easy.
You might be thinking that this silly job can be done by calculator, but its not that true...just have a look at the output...you will be stunned as this program gives the full required number without truncating the answer...its simply amazing.
Just copy and paste this code on your tcpp and try it out just now.
Actually I encountered this problem on www.projecteuler.net an awesome site that always encouraged and tangled me, my favorite pass-time.
Hope you all like it.



Amazing Output:-

Have a look at it and enjoy...;)



 /****************************************************************************/

//PROGRAM TO CALCULATE POWER OF 2

#include<stdio.h>
#include<conio.h>
#define max 5000


void main()
{
    int arr[max],aarr[max],i,j,flag,lavish,tag,sum=0;
    clrscr();

    printf("program to calculate power of 2\n");
    printf("x^(number)\n");
    printf("enter the number\n");
    scanf("%d",&lavish);

    for(i=0;i<max;i++)
    {
        arr[i]=0;
        aarr[i]=0;
    }

    printf("\n\n");

    arr[max-1]=2;

    flag=1;

    while(1)
    {
        flag++;
        for(i=max-1;i>=0;i--)
        {
            aarr[i]=aarr[i]+((2*arr[i])%10);
            if(arr[i]>=5&&arr[i]<10)
                aarr[i-1]=1;
        }


        for(i=0;i<=max-1;i++)
        {
            arr[i]=aarr[i];
            aarr[i]=0;
        }

        tag=0;

        if(flag==lavish)
        {
            for(i=0;i<max;i++)
            {
                if(arr[i]!=0)
                    tag=1;
                if(tag==1)
                {
                    printf("%d",arr[i]);
                    sum=sum+arr[i];
                }
            }
            break;
        }
    }

    printf("\n\nsum of all digits is:-%d",sum);
    getch();
}



/***************************************************************************/

No comments:

Post a Comment