# UVA Problem 10035 – Primary Arithmetic Solution

UVA Problem 10035 – Primary Arithmetic Solution:

Solving Technique:

For this problem two integers less than 10 digits are given. The task is to find how many carry operations occur when adding.

This problem may be solved using strings since input integer is less than 10 digits. It can be solved with out using array. My implementation is using character array to add and count carry operations.

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer. Please compile with c++ compiler as some of my codes are in c and some in c++.

More Inputs of This Problem on uDebug.

Input:

```123 456
555 555
123 594
0 0
```

Output:

```No carry operation.
3 carry operations.
1 carry operation.
```

### Code:

```/**
* Author:    Asif Ahmed
* Site:      https://quickgrid.wordpress.com
* Problem:   UVA 10035 - Primary Arithmetic
*            Making two strings of same length,
*/

#include<stdio.h>
#include<string.h>

#define N 128

static char s[N];
static char t[N];

static char output[N];

int main(){

while( scanf("%s", s) && scanf("%s", t) ){

// Although a major flaw input beginning with 0 and
// two strings matching. But for this problem it doesn't matter.
if( strcmp(s,t) == 0 && s == '0' )
break;

int lens = strlen(s);
int lent = strlen(t);

// Shift each characters in string right by padding length.
if( lens > lent ){

int padding = lens - lent;

for(int i = lent - 1; i >= 0; --i)

for(int i = 0; i < padding; ++i)
t[i] = '0';
}

else if( lens < lent ){

int padding = lent - lens;

for(int i = lens - 1; i >= 0; --i)

for(int i = 0; i < padding; ++i)
s[i] = '0';
}

int maxlen;
if(lens > lent)
maxlen = lens;
else maxlen = lent;

int carry = 0;
int c = 0;
int sum = 0;

// Add two Strings, if a carry operation occurs
// then add that to the count.
for(int i = maxlen - 1; i >= 0; --i){

sum += s[i] - '0' + t[i] - '0';

sum = sum + carry;

carry = sum / 10;

if(carry)
++c;

sum = 0;

}

if(!c)
printf("No carry operation.\n");
else if( c > 1 )
printf("%d carry operations.\n", c);
else
printf("%d carry operation.\n", c);

}

return 0;
}
```