Concept explainers
Implement a
Enter num1: Enter num2: Input validation MUST be completed input must be all digits input must be a positive number, Integer Overflow for the individual inputs and the sum of the inputs is now VALID and needs to be handled as described below with the ADDITION
The input CANNOT be converted to a true int, unsigned int, long, unsigned long or long long data type
The solution is to use an array to store each individual digit of the number
For example, the inputted number: "1234" could be stored in the array nums backwards, so the ones position is at index 0 of the array.
4 = 10^0 (ones position) so that nums[0] = 4 3 = 10^1 (tens position) so that nums[1] = 3 2 = 10^2 (hundreds position) so that nums[2] = 2 1 = 10^3 (thousands position) so that nums[3] = 1 The program will perform the addition operation by implementing the usual paper-and-pencil addition algorithm (adding the ones digits and if greater than 10, then carry the 1 to the tens position, etc)
Add starting with ones digits and carry the 1 if 10 or over. For the below example the following steps are completed sum[0] = 5+6+0(no carry over) =11 store the 1 and carry the 1 to the next index. sum[1] = 4+7+1(carry over)=12 store the 2 and carry the 1 to the next index. sum[2] = 3+8+1(carry over) = 12 store the 2 and carry the 1 to the next index. sum[3] = 2+9+1(carry over) = 12 store the 2 and carry the 1 to the next index. sum[4] = 1+0+1(carry over) = 2 store the 2 Reverse the sum array to get the answer. The ones place at index 0, should be written as the right most value. Answer: 22221 Outputs the sum of the two positive integers when input is valid as formatted below in the example test runs.
I'm not getting the results needed for test #3-6
Test Run #3
Test Run #4
Enter num1: -378
INVALID RE-Enter num1: abc
INVALID RE-Enter num1: 18446744073709551619
Enter num2: 16abc
INVALID RE-Enter num2: -16
INVALID RE-Enter num2: 1
18446744073709551619
+ 1
-------------------------------------
18446744073709551620
Test Run #6
Below is my C++ code already started
#include <iostream>
#include <bits/stdc++.h>
bool isNumber(std::string s) {
for (int i = 0; i < s.length(); i++)
if(isdigit(s[i]) == false)
return false;
return true;
}
std::string findSum(std::string str1, std::string str2) {
if(str1.length() > str2.length())
swap(str1, str2);
std::string str = "";
int n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i = 0; i < n1; i++) {
int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n1; i < n2; i++) {
int sum = ((str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
if (carry)
str.push_back(carry + '0');
reverse(str.begin(), str.end());
return str;
}
Step by stepSolved in 4 steps with 2 images
- Select the code snippet that would implement the result for the following arithmetic expression: * (-op1 op2) + op3 Be sure to check for overflow. mov eax, op2 neg eax mul op1 jnz Err sub eax, op3 mov eax, op1 neg eax mul op2 jc Err mul eax, op3 jc Err mov eax, op1 neg eax imul op2 jo Err add eax, op3 jo Errarrow_forwardPython questionarrow_forwardWrite a program that computes factorial for a desired number repeatedly until -1 is entered.Expected input and outputPlease enter a positive number for factorial calculation (-1 to end) :4factorial for 4 is 24Please enter a positive number for factorial calculation (-1 to end) :6factorial for 6 is 720Please enter a positive number for factorial calculation (-1 to end) :8factorial for 8 is 40320Please enter number for factorial calculation (-1 to end) :-1Submit 1 run of your program. It should have at least 5 inputs. The first three input as shown above and two other the inputs of your choice.Your program should work for any input. Do not add any logic in your code to limit the input. However for the run, try to limit your input between 1 and 15 just to keep the output simple.arrow_forward
- Problem Ask the user for a positive integer N, then output the number of even and odd digits of N. The program should continuously ask input from the user until the user inputs a nonpositive integer (where the program must exit). Sample Run Enter n: 100 Even digits: 2 Odd digits: 1 Enter n: 31 Even digits: 0 Odd digits: 2 Enter n: 4286 Even digits: 4 Odd digits: 0 Enter n: 1234567890 Even digits: 5 Odd digits: 5 Enter n: -15 Goodbye! NOTE: You are not allowed to use string functions/operations, lists, tuples, dictionaries, or any other non-scalar objects.arrow_forward..5-- I have a python programing question In 1937, a German mathematician named Lothar Collatz formulated an intriguing hypothesis (it still remains unproven) which can be described in the following way: 1. take any non-negative and non-zero integer number and name it c0; 2. if it's even, evaluate a new c0 as c0 ÷ 2; 3. otherwise, if it's odd, evaluate a new c0 as 3 × c0 + 1; 4. if c0 ≠ 1, skip to point 2. The hypothesis says that regardless of the initial value of c0, it will always go to 1. Of course, it's an extremely complex task to use a computer in order to prove the hypothesis for any natural number (it may even need artificial intelligence), but you can use Python to check some individual numbers. Maybe you'll even find the one which would disprove the hypothesis. Write a program which reads one natural number and executes the above steps as long as c0remains different from 1(c0 != 1). Moreover, we'll add another task - we want you to count the steps needed to achieve the goal.…arrow_forwardplease use pythonarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY