Question

Hi, I was wondering if you'd be able to help me figure out how to evaluate...

Hi, I was wondering if you'd be able to help me figure out how to evaluate the sum of the numbers in char data type since built in data type is not applicable (cannot contain 80 digits).(C programming).(the problem is written below). Is it necessary to use multidimensional array ?

A country has a budget of more than 81 trillion yen. We want to process such data, but conventional integer type which uses signed 32 bit can represent up to 2,147,483,647.

Your task is to write a program which reads two integers (more than or equal to zero), and prints a sum of these integers.

If given integers or the sum have more than 80 digits, print "overflow".

Input

Input consists of several datasets. In the first line, the number of datasets N (1 ≤ N ≤ 50) is given. Each dataset consists of 2 lines:

The first integer
The second integer

The integer has at most 100 digits.

Output

For each dataset, print the sum of given integers in a line.

Sample Input

6
1000
800
9999999999999999999999999999999999999999
1
99999999999999999999999999999999999999999999999999999999999999999999999999999999
1
99999999999999999999999999999999999999999999999999999999999999999999999999999999
0
100000000000000000000000000000000000000000000000000000000000000000000000000000000
1
100000000000000000000000000000000000000000000000000000000000000000000000000000000
100000000000000000000000000000000000000000000000000000000000000000000000000000000

Output for the Sample Input

1800
10000000000000000000000000000000000000000
overflow
99999999999999999999999999999999999999999999999999999999999999999999999999999999
overflow
overflow
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for your question. Please do rate the answer if it helped. Thank you.

#include <stdio.h>
#include <string.h>
//adds num1 and num2 and stores result in ans. right most digit is stored in index 0 for all numbers
//returns 0 if overflow and 1 if no overflow

int add(char num1[], char num2[], char ans[]);
void print(char num[]); //displays the number, right most digit is in index 0
void reverse(char s[]); //reverses a string
int main(){
int numTests;
char num1[101], num2[101], ans[101];

printf("Enter number of tests followed by 2 numbers for each test case\n");
scanf("%d", &numTests);

for(int i = 0; i < numTests; i++){
scanf("%s %s", num1, num2);
reverse(num1);
reverse(num2);
if(add(num1, num2, ans))
print(ans);
else
printf("overflow\n");
}
return 0;
}

int add(char num1[], char num2[], char ans[])
{
int d1, d2, sum , carry = 0;
int len1 = strlen(num1), len2 = strlen(num2);
int i = 0, j = 0, k = 0;

if(len1 > 80 || len2 > 80)
return 0; //overflow

while(i < len1 || j < len2){
if(i < len1 )
d1 = num1[i++] - '0'; //get the numeric equivalent of the char
else
d1 = 0;
  
if(j < len2)
d2 = num2[j++] - '0';
else
d2 = 0;

sum = carry + d1 + d2;

if(sum > 9){
sum -= 10;
carry = 1;
}
else
carry = 0;
//printf("d1 = %d, d2 = %d, s = %d,c = %d\n", d1, d2, sum, carry);
ans[k++] = sum + '0'; //store the digit as char
}

if(carry && k >= 80)
return 0; //overflow
else{
if(carry)
ans[k++] = '1';
ans[k] = '\0';
return 1;
}
}

void print(char num[]) //displays the number, right most digit is in index 0
{
int len = strlen(num);
for(int i = len-1; i>= 0; i--)
printf("%c", num[i]);
  
printf("\n");
}


void reverse(char s[]){
int len = strlen(s);
int i = 0, j = len-1;
char temp;
while(i < j){
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}

output
----
Enter number of tests followed by 2 numbers for each test case
6
1000
800
1800
9999999999999999999999999999999999999999
1
10000000000000000000000000000000000000000
99999999999999999999999999999999999999999999999999999999999999999999999999999999
1
overflow
99999999999999999999999999999999999999999999999999999999999999999999999999999999
0
99999999999999999999999999999999999999999999999999999999999999999999999999999999
100000000000000000000000000000000000000000000000000000000000000000000000000000000
1
overflow
100000000000000000000000000000000000000000000000000000000000000000000000000000000
100000000000000000000000000000000000000000000000000000000000000000000000000000000
overflow

Add a comment
Know the answer?
Add Answer to:
Hi, I was wondering if you'd be able to help me figure out how to evaluate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • C++ problem Please help me with it. Thank you. the output would be like this: 10-20...

    C++ problem Please help me with it. Thank you. the output would be like this: 10-20 with sequence 10 - 30 no sequence: this is the background: Project Description / Specification Your program takes as input three integer values (same line, space separated) the first two integers, the beginning and ending value in a range of integers you are going to examine, as space separated integers. They are in that order: the first is the lower range and the second...

  • I want this using while loop This using stringin python Use list or some thing in...

    I want this using while loop This using stringin python Use list or some thing in python Using list in python I want answer as soon as posdible E. Last Number time limit per test: 1 second memory limit per test: 256 megabytes input standard input output standard output You are given a sequence of positive integers aj, , 03, ... Print the last element of the sequence. Input The input consists of multiple lines. The i-th line contains a...

  • Hi can anyone help me with this question? Please use python when you do it. THANKS...

    Hi can anyone help me with this question? Please use python when you do it. THANKS 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are or * The tree represents...

  • #include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For...

    #include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For this compiler: “ << endl; Cout << “integers are: “ << sizeof(int) << “bytes” << endl; Cout << “largest integers is “ <<INT_MAX << endl; Cout << “smallest integers is “ <<INT_MIN << endl; Cout << “Input two integers values “ << endl; Cin >> i >> j; Cout << endl << “You entered the following values: “ << endl; Cout << “integer “...

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

  • Hello, If anyone would be so kind as to help me out with a solution in...

    Hello, If anyone would be so kind as to help me out with a solution in either Java or C++ please and thank you !!! CS 278 Lab3: Quantified statements Write a program that does the following. It should take as a user input ten different integers and store them in a length ten integer array (with no repeated entries). The domain D is the set of entries in this array. Recall that if the domain is a finite set...

  • HI, I am having trouble finsihing this program i was wondering if someone could help me....

    HI, I am having trouble finsihing this program i was wondering if someone could help me. Thanks in adavnce. Here is the code that I have so far..... //Stack.h // implementation file for the stack class #include <iostream> using namespace std; const int stack_size = 100; class stack { private: char data [stack_size]; // elements in the stack int top; // index of the top element of the stack public: stack (); // constructor creates an empty stack void push...

  • Hello I was wondering how could I solve this excel assignment, since I never used Excel,...

    Hello I was wondering how could I solve this excel assignment, since I never used Excel, I have no clue how to begin and how to get the values and charts inputed into excel. Could I see an excel version on how I could do this? Please help, and thank you! Step Instructions Points Possible Use a cell reference or a single formula where appropriate in order to receive full credit. Do not copy and paste values or type values,...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT