Big Number Division and Remainder

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000
/*******************************************************************/
int call_div(char *number,long div,char *result){
 int len=strlen(number);
 int now;
 long extra;
 char Res[MAX];
 for(now=0,extra=0;now<len;now++){
 extra=extra*10 + (number[now]-'0');
 Res[now]=extra / div +'0';
 extra%=div;
 }
 Res[now]='';
 for(now=0;Res[now]=='0';now++);
 strcpy(result, &Res[now]);
 if(strlen(result)==0)
 strcpy(result, "0");
 return extra;
}
/*******************************************************************/
int main(){
 char fir[MAX],res[MAX];
 long sec,remainder;
 while(scanf("%s%ld",&fir,&sec)==2){
 if(sec==0) printf("Divide by 0 error\n");
 else{
 remainder=call_div(fir,sec,res);
 int len=strlen(res);
 for(int i=0;i<len;i++) printf("%c",res[i]);
 printf("\t%ld",remainder);
 printf("\n");
 }
 }
 return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s