//Multiplication of large numbers in c
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
#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;
}
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;
}
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;
}
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;
}
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
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
#include<string.h>
#define MAX 10000
int validate(char []);
char * division(char[],long);
long int remainder;
char * division(char[],long);
long int remainder;
int main(){
char dividend[MAX],*quotient;
long int divisor;
long int divisor;
printf("Enter dividend: ");
scanf("%s",dividend);
if(validate(dividend))
return 0;
scanf("%s",dividend);
if(validate(dividend))
return 0;
printf("Enter divisor: ");
scanf("%ld",&divisor);
scanf("%ld",&divisor);
quotient = division(dividend,divisor);
while(*quotient)
if(*quotient ==48)
quotient++;
else
break;
if(*quotient ==48)
quotient++;
else
break;
printf("Quotient: %s / %ld = %s",dividend,divisor,quotient) ;
printf ("\nRemainder: %ld",remainder);
return 0;
}
printf ("\nRemainder: %ld",remainder);
return 0;
}
int validate(char num[]){
int i=0;
int i=0;
while(num[i]){
if(num[i] < 48 || num[i]> 57){
printf("Invalid positive integer: %s",num);
return 1;
}
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;
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;
temp = temp*10 + (dividend[i] -48);
if(temp<divisor){
quotient[j++] = 48;
}
else{
quotient[j++] = (temp / divisor) + 48;;
temp = temp % divisor;
}
i++;
}
i++;
}
quotient[j] = '\0';
remainder = temp;
return quotient;
}
remainder = temp;
return quotient;
}
Sample output:
Enter dividend: 543109237823560187
Enter divisor: 456
Quotient: 543109237823560187 / 456 = 1191029030314824
Remainder: 443
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
#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;
}
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);
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;
}
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;
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;
}
}
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';
c[i]=c[i]+48;
}
c[i]= '\0';
printf("Reminder of division: %lu ",reminder);
return c;
}
return c;
}
Sample output:
Enter the divdend:
555555555555555555555555555555 55555555555
Enter the divisor: 5
Reminder of division: 0
Quotient of the division:
111111111111111111111111111111 11111111111
Enter the divdend:
555555555555555555555555555555
Enter the divisor: 5
Reminder of division: 0
Quotient of the division:
111111111111111111111111111111
//C program for modular division of large number
#include<stdio.h>
#include<string.h>
#define MAX 10000
#include<string.h>
#define MAX 10000
int validate(char []);
int modulerDivision(char[], unsigned long);
int modulerDivision(char[],
int main(){
char dividend[MAX];
unsigned long int divisor,remainder;
unsigned long int divisor,remainder;
printf("Enter dividend: ");
scanf("%s",dividend);
if(validate(dividend))
return 0;
scanf("%s",dividend);
if(validate(dividend))
return 0;
printf("Enter divisor: ");
scanf("%lu",&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;
int i=0;
while(num[i]){
if(num[i] < 48 || num[i]> 57){
printf("Invalid positive integer: %s",num);
return 1;
}
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;
unsigned long temp=0;
int i=0;
while(dividend[i]){
temp = temp*10 + (dividend[i] -48);
if(temp>=divisor){
temp = temp % divisor;
}
i++;
}
temp = temp*10 + (dividend[i] -48);
if(temp>=divisor){
temp = temp % divisor;
}
i++;
}
return temp;
}
}
Sample output:
Enter dividend: 123456789
Enter divisor: 56
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
#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);
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);
for(i=0;i<n-1;i++)
c = multiply(a,c);
while(*c)
if(*c =='0')
c++;
else
break;
if(*c =='0')
c++;
else
break;
printf("%s",c);
return 0;
}
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;
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';
a[i] = num1[i];
i++;
}
a[i]= '\0';
i=0;
while(num2[i]){
b[i] = num2[i];
i++;
}
b[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;
}
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;
}
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;
}
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: 788860905221011805411728565282 78622
967320643510902300477027893066 40625
Enter the power: 100
Power of the 5^100: 788860905221011805411728565282
967320643510902300477027893066
No comments:
Post a Comment