Thursday, 27 February 2014

Very large numbers

//Multiplication of large numbers in c
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char b[MAX];
    char *c;
    int la,lb;
    int i;
    printf("Enter the first number : ");
    scanf("%s",a);
    printf("Enter the second number : ");
    scanf("%s",b);
    printf("Multiplication of two numbers : ");
    c = multiply(a,b);
    printf("%s",c);
    return 0;
}
char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
    la=strlen(a)-1;
        lb=strlen(b)-1;
  
        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }
        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }
    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }
  
    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}
Sample output of above code:
Enter the first number: 55555555
Enter the second number: 3333333333
Multiplication of two numbers:
185185183314814815
//Division of large numbers in c
#include<stdio.h>
#include<string.h>
#define MAX 10000
int validate(char []);
char * division(char[],long);
long int remainder;
int main(){
    char dividend[MAX],*quotient;
    long int divisor;
    printf("Enter dividend: ");
    scanf("%s",dividend);
    if(validate(dividend))
         return 0;
    printf("Enter divisor: ");
    scanf("%ld",&divisor);
    quotient = division(dividend,divisor);
    while(*quotient)
         if(*quotient ==48)
             quotient++;
         else
             break;
    printf("Quotient: %s / %ld  =  %s",dividend,divisor,quotient);
    printf ("\nRemainder: %ld",remainder);
    return 0;
}
int validate(char num[]){
    int i=0;
    while(num[i]){
         if(num[i] < 48 || num[i]> 57){
             printf("Invalid positive integer: %s",num);
             return 1;
         }
         i++;
    }
    return 0;
}
char * division(char dividend[],long divisor){
  
    static char quotient[MAX];
    long temp=0;
    int i=0,j=0;
    while(dividend[i]){
       
         temp = temp*10 + (dividend[i] -48);
         if(temp<divisor){
           
             quotient[j++] = 48;
           
         }
         else{
             quotient[j++] = (temp / divisor) + 48;;
             temp = temp % divisor;
         }
         i++;
    }
    quotient[j] = '\0';
    remainder = temp;
    return quotient;
}
Sample output:
Enter dividend: 543109237823560187
Enter divisor: 456
Quotient: 543109237823560187 / 456 = 1191029030314824
Remainder: 443
Other program (source code):
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
char * division(char [],unsigned long);
int main(){
    char a[MAX];
    unsigned long b;
    char *c;
    printf("Enter the divdend : ");
    scanf("%s",a);
    printf("Enter the divisor : ");
    scanf("%lu",&b);
    c = division(a,b);
    printf("\nQuotient of the division : ");
    printf("%s",c);
    return 0;
}
char * division(char a[],unsigned long b){
    static char c[MAX];
    int la;
    int i,k=0,flag=0;
    unsigned long temp=1,reminder;
    la=strlen(a);
    for(i=0;i<=la;i++){
         a[i] = a[i] - 48;
    }
    temp = a[0];
    reminder = a[0];
    for(i=1;i<=la;i++){
         if(b<=temp){
             c[k++] = temp/b;
             temp = temp % b;
             reminder = temp;
             temp =temp*10 + a[i];
             flag=1;
         }
         else{
             reminder = temp;
             temp =temp*10 + a[i];
             if(flag==1)
                 c[k++] = 0;
         }
    }
    for(i=0;i<k;i++){
         c[i]=c[i]+48;
    }
    c[i]= '\0';
    printf("Reminder of division:  %lu  ",reminder);
    return c;
}
Sample output:
Enter the divdend:
55555555555555555555555555555555555555555
Enter the divisor: 5
Reminder of division:  0
Quotient of the division:
11111111111111111111111111111111111111111
//C program for modular division of large number
#include<stdio.h>
#include<string.h>
#define MAX 10000
int validate(char []);
int modulerDivision(char[],unsigned long);
int main(){
    char dividend[MAX];
    unsigned long int divisor,remainder;
    printf("Enter dividend: ");
    scanf("%s",dividend);
    if(validate(dividend))
         return 0;
    printf("Enter divisor: ");
    scanf("%lu",&divisor);
    remainder = modulerDivision(dividend,divisor);
    printf("Modular division: %s %% %lu  =  %lu",dividend,divisor,remainder);
    return 0;
}
int validate(char num[]){
    int i=0;
    while(num[i]){
         if(num[i] < 48 || num[i]> 57){
             printf("Invalid positive integer: %s",num);
             return 1;
         }
         i++;
    }
    return 0;
}
int modulerDivision(char dividend[],unsigned long divisor){
  
    unsigned long temp=0;
    int i=0;
    while(dividend[i]){
       
         temp = temp*10 + (dividend[i] -48);
         if(temp>=divisor){
             temp = temp % divisor;
         }
  
         i++;
    }
    return temp;
}
Sample output:
Enter dividend: 123456789
Enter divisor: 56
Modular division: 123456789 % 56 = 29
//C program to find power of a large number
#include<stdio.h>
#include<string.h>
#define MAX 10000
char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char *c;
    int i,n;
    printf("Enter the base number: ");
    scanf("%s",a);
    printf("Enter the power: ");
    scanf("%d",&n);
    printf("Power of the %s^%d: ",a,n);
    c = multiply(a,"1");
    for(i=0;i<n-1;i++)
         c = multiply(a,c);
    while(*c)
    if(*c =='0')
         c++;
    else
         break;
    printf("%s",c);
    return 0;
}
char * multiply(char num1[],char num2[]){
    static char mul[MAX];
    char a[MAX];
    char b[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i=0,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
  
    while(num1[i]){
         a[i] = num1[i];
         i++;
    }
    a[i]= '\0';
    i=0;
    while(num2[i]){
         b[i] = num2[i];
         i++;
    }
    b[i]= '\0';
    la=strlen(a)-1;
        lb=strlen(b)-1;

        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }
        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }
    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }

    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}
Sample output:
Enter the base number: 5
Enter the power: 100
Power of the 5^100: 78886090522101180541172856528278622
96732064351090230047702789306640625

No comments:

Post a Comment