102k views
5 votes
USE C porgramming for this problem.

In this section you will enter the code for cases 'R' and 'r', which correspond to reversing the digits of a number (any positive integer). Use following code snippet:
int reversed = 0;
while(num > 0)
{
reversed = reversed * 10 + num % 10;
num = num / 10;
}
return reversed;
Put this code in the definition of the function reverse_number() within the lab2support.c file and call this function at the appropriate place within the lab2main.c file. Print the returned value.
Compile and run the program using the following commands:
$gcc -Wall lab2support.c lab2main.c -o lab2
$./lab2
Your output should look similar to the following screenshot:
Test your program and enter the following two numbers:
12345 (YOUR OUTPUT SHOULD BE: 54321)
54321 (YOUR OUTPUT SHOULD BE: 12345)
Get a screenshot

1 Answer

2 votes

Below is the C porgramming to reversing the digits of a number (any positive integer)

// lab2support.c

int reverse_number(int num)

{

int reversed = 0;

while (num > 0)

{

reversed = reversed * 10 + num % 10;

num = num / 10;

}

return reversed;

}

// lab2main.c

#include <stdio.h>

#include "lab2support.c"

int main()

{

int number;

printf("Enter a number: ");

scanf("%d", &number);

int reversed_number = reverse_number(number);

printf("Reversed number: %d\\", reversed_number);

return 0;

}

The above code is one that helps defines a function reverse_number() in lab2support.c that reverses the digits of an integer number.

Therefore, the main() function in lab2main.c is one that prompts the user to enter an integer, calls the reverse_number() function to reverse the digits, and prints the reversed number.

see text below

USE C porgramming for this problem.

In this section you will enter the code for cases 'R' and 'r', which correspond to reversing the digits of a number (any positive integer). Use following code snippet:

int reversed = 0;

while(num > 0)

{

reversed = reversed * 10 + num % 10;

num = num / 10;

}

return reversed;

Put this code in the definition of the function reverse_number() within the lab2support.c file and call this function at the appropriate place within the lab2main.c file. Print the returned value.

Compile and run the program using the following commands:

$gcc -Wall lab2support.c lab2main.c -o lab2

$./lab2

Your output should look similar to the following screenshot:

Test your program and enter the following two numbers:

12345 (YOUR OUTPUT SHOULD BE: 54321)

54321 (YOUR OUTPUT SHOULD BE: 12345)

Get a screenshot

User Saurabh Hirani
by
8.3k points

No related questions found