URI 1212 -- Primary Arithmetic

Problem Link

Problem Name : Primary Arithmetic 
Solution :

Assume that a=99 and b=9999 so how to count carry variable ?
let's see,

n1=a%10 got 9
n2=b%10 got 9 

sum=9+9=18 so carry=1

n1=a%10 got 9
n2=b%10 got 9 

sum=9+9=18 so carry= 1+1 (previous counted 1) and so on...

Another way if , sum is not equal or greater than 10 than carry=0.

// Author : Shohan
// URI oj : 1212
#include<stdio.h>
int long carry_num (int long a,int long b);
int main()
{
int long a,b,sum=0,op=0,d1,d2,carry;
while(scanf("%ld %ld",&a,&b)==2){
if(a==0 && b==0)
break;
op=carry_num(a,b);
if(op==0)
printf ("No carry operation.\n");
else if(op==1)
printf ("1 carry operation.\n");
else
printf("%ld carry operations.\n",op);
}
return 0;
}
int long carry_num(int long a,int long b)
{
int long op,carry,d1,d2,sum;
op=0;
carry=0;
while(1){
d1=a%10;
d2=b%10;
sum=d1+d2+carry;
if(sum>=10){
op++;
carry=1;
}
else {
carry=0;
}
a=a/10;
b=b/10;
if(a==0 && b==0)
break;
}
return op;
}
view raw URI 1212 hosted with ❤ by GitHub

Related Posts: