Question

Write a program in C++ that implements a function // Precondition: num >= 0 int rotateUp(int...

Write a program in C++ that implements a function

// Precondition: num >= 0
int rotateUp(int num);

which produces a new integer with each digit replaced with (digit+1), and 9 replaced with 0, because we don’t have digit 10:

rotateUp(5) == 6
rotateUp(17) == 28
rotateUp(126) == 237
rotateUp(5091) == 6102
rotateUp(9911) == 22   // technically, more like 0022

The program should have a main function that tests the function.

Example Output:

Enter a number: 901

12

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

//below function will extract each digit from number and then add 1 to it
int rotateUp(int num){
    int base = 1;
    int digit;
    int number = 0;
    while(num){
        digit = num % 10;
        num /= 10;
        digit = (digit + 1) % 10;
        number += digit * base;
        base *= 10;
    }
    return number;
}

int main(){
    int number;
    cout<<"Please enter a number :";
    cin>>number;
    cout<<"Rotated number = "<<rotateUp(number)<<endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that implements a function // Precondition: num >= 0 int rotateUp(int...
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
  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • C++ 9) Write a recursive function printAll that takes an int num argument and prints all...

    C++ 9) Write a recursive function printAll that takes an int num argument and prints all the numbers in the range [num, 0]. The function returns nothing. e.g. printAll(5) prints 5, 4, 3, 2, 1, 0. Call your function in the main

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int...

    C++ C. In the file c_final_practice.cpp: (1) Write a function void replace(vector<int>& v, int old, int new) that replaces all occurrences of the integer old in v with the integer new. For example, if v is the vector {1,2,1,3}, then after calling replace(v, 1, 3) v should be {3,2,3,3}. (2) Write the main-function so that it prompts the user for a list of integers, and then uses your replace function to display the following lists: • The user's list with...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

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
ADVERTISEMENT