Sunday, October 18, 2015

C Program to perform arithmatic operation of two large integers on 32 bit machine.

/* ================================================================================
 *  * *
 *  * *       Filename:  op_largenumber.c
 *  * *        Created:  Tuesday 17 February 2015
 *  * *       Compiler:  gcc
 *  * *         Author:  Manoj Kumar Patra,manojpatra.sit@gmail.com
 *  * *   Organization:  SCIS,University of Hyderabad
 *  * *
 *  ===============================================================================*/

#include <stdio.h>
#include <string.h>
#define MAX 100
void sub(char num1[], char num2[], int n1, int n2);
void add(char num1[], char num2[], int n1, int n2);
char num1[MAX],num2[MAX],res[MAX+1];
void main()
{
char temp[2]="0",temp2[MAX];
int n1,n2,i,j,temp1,c=0,t1,t2,ch;
printf("===================================================================");
printf("\nDecide what do you wanna perform\n");
printf("1.Addition\t2.Subtraction");
printf("\nEnter your choice:");
scanf("%d",&ch);
printf("===================================================================\n");
switch(ch)
{
case 1 :
printf("Enter the first number:");
scanf("%s",num1);
printf("Enter the second number:");
scanf("%s",num2);
n1 = strlen(num1);
n2 = strlen(num2);
add(num1,num2,n1,n2);
break;
case 2 :
printf("Enter the larger number first!");
printf("\nEnter the first number:");
scanf("%s",num1);
printf("Enter the second number:");
scanf("%s",num2);
n1 = strlen(num1);
n2 = strlen(num2);
sub(num1,num2,n1,n2);
break;
default :
printf("Wrong input!!\n");
}
printf("===================================================================");
printf("\n");
}

void add(char num1[], char num2[], int n1,int n2)
{
char temp[2]="0",temp2[MAX];
int i,j,temp1,c=0,t1,t2;

if(n1>n2)
{
for(i=0;i<(n1-n2);i++)
temp2[i]='0';
for(j=i;j<n1;j++)
{
temp2[j]=num2[c];
c++;
}
for(i=0;i<n1;i++)
num2[i]=temp2[i];
n2=n1;
}
else if(n2>n1)
{
for(i=0;i<(n2-n1);i++)
temp2[i]='0';
for(j=i;j<n2;j++)
{
temp2[j]=num1[c];
c++;
}
for(i=0;i<n2;i++)
num1[i]=temp2[i];
n1=n2;
}
for(i=n1-1;i>=0;i--)
{
temp1 = ( num1[i] - 48 + num2[i] - 48 + temp[0] - 48 );
sprintf(temp, "%d", temp1);
if(strlen(temp)==1)
{
res[i+1]=temp[0];
temp[0]='0';
}
else
res[i+1]=temp[1];
}
if(strlen(temp)==2)
res[0] = temp[0];
else
res[0] = '0';

printf("\nThe sum of the above two number is: %s\n",res);
}

void sub(char num1[], char num2[], int n1,int n2)
{
char temp[2]="0",temp2[MAX];
int i,j,temp1,c=0,t1,t2;
if(n1>n2)
{
for(i=0;i<(n1-n2);i++)
temp2[i]='0';
for(j=i;j<n1;j++)
{
temp2[j]=num2[c];
c++;
}
for(i=0;i<n1;i++)
num2[i]=temp2[i];
n2=n1;
}
else if(n2>n1)
{
for(i=0;i<(n2-n1);i++)
temp2[i]='0';
for(j=i;j<n2;j++)
{
temp2[j]=num1[c];
c++;
}
for(i=0;i<n2;i++)
num1[i]=temp2[i];
n1=n2;
}
c = 0;
for(i=n1-1;i>=0;i--)
{
if(c==1)
{
if(num1[i]!='0')
t1 = num1[i] - 48 - 1;
else
t1 = 9;
}
else
{
t1 = num1[i] - 48;
}
t2 = num2[i] - 48;
if((t1 < t2) && i != 0)
{
t1 += 10;
c = 1;
}
else if(t1 > t2)
c = 0;
else if((t1 < t2) && i == 0)
{
t1 = t1 + t2;
t2 = t1 - t2;
t1 = t1 - t2;
c = 1;
}
temp1 = t1 - t2 ;
sprintf(temp, "%d", temp1);
res[i+1] = temp[0];
}
if(c==1)
res[0] = '-';
else
res[0] = '+';

printf("\nThe substraction of the above two number is: %s\n",res);
}

OutPut:

No comments:

Post a Comment