Question

C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...

C - Language Create a recursive function to print a multi-digit number vertically.

For example, 2378 should be printed as

2

3

7

8

Be sure to test your program with numbers of different length.

The recursive function should return an int and take an int as a parameter.

The function should have a base case where the parameter is 0 and this should return 0

Define a temp variable using the remainder operator where temp is equal to the number passed to the function % 10 this will give you the last digit of the number

Call the recursive function again with the parameter/10

print the number from temp.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<stdio.h>
int main()
{
int num;
printf("Enter a Number:");
scanf("%d",&num);
  
multi_digit(num); //calling multi_digit function
  
return 0;
}


// Function to print multi digit number vertically
int multi_digit(int n)
{
int temp;
if (n == 0) //Base case if n == 0 returns 0
{
return 0;
}   
else
{
temp = n%10; //gives last digit of a number
multi_digit(n/10); //calling function recursively with quotient
printf("%d\n", temp); //prints digits
}
  
return 0;
}

Snapshot of Code:

Output:

Add a comment
Know the answer?
Add Answer to:
C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...
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++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void...

    C++ Recursion Code a function which displays the first n prime numbers. The prototype is: void prime (int) The number of primes to display is passed as the parameter. The function prime itself is not recursive. However, it should call a separate recursive helper function which determines if a given number is prime #include <iostream> using namespace std; void prime(int ) { } int main() {    prime (21);    return 0; }

  • Please write program in C language. 2.Write a recursive int function to return the number of...

    Please write program in C language. 2.Write a recursive int function to return the number of even integers in a linked list. Each node in the list has an integer number (declared as int number) and a pointer to the next node (declared as NODE *next). The function is defined as int count (NODE *); and is called as follows: x = count(head);

  • Last Coding Question! C++ is the programming language used This one is very difficult. My to...

    Last Coding Question! C++ is the programming language used This one is very difficult. My to do list is as follows: // This program uses a function to swap the values in two variables. /*TO DO: change the function swapNums to use two pointer parameters instead of two reference parameters, and then modify the complete program accodingly. */ #include <iostream> using namespace std; // Function prototype void swapNums(int&, int&); /***** main *****/ int main() { int num1 = 5, num2...

  • Language should be java Create a recursive function, int combinations (int n, int k) to calculate...

    Language should be java Create a recursive function, int combinations (int n, int k) to calculate the number of ways to select k items from n items. Use this formula: morfin.. C(n, k) = { 0 if k=0 if n <k otherwise C(n-1, k-1) + C(n-1,k) Running your program should look like this: Enter an Integer: 6 Enter another Integer: 2 combinations (6,2) = 15 Enter an Integer: 8 Enter another Integer: 4 combinations (8,4) = 70

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...

    ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

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