Reverse Integer

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 39 :- Reverse Integer

To reverse an integer, you can write a C++ function that takes an integer as input and returns its reverse. The reverse of an integer is obtained by reversing the order of its digits. For example, the reverse of 123 is 321.

Here's a C++ program to reverse an integer:

cppCopy code

#include <iostream> int reverseInteger(int num) {    int reversed = 0;    while (num != 0) {        int digit = num % 10;        reversed = reversed * 10 + digit;        num /= 10;    }    return reversed; } int main() {    int num;    std::cout << "Enter an integer: ";    std::cin >> num;    int reversedNum = reverseInteger(num);    std::cout << "The reverse of " << num << " is: " << reversedNum << std::endl;    return 0; }

Example output:

vbnetCopy code

Enter an integer: 12345 The reverse of 12345 is: 54321

vbnetCopy code

Enter an integer: -789 The reverse of -789 is: -987

In the program, we define a function reverseInteger that takes an integer as input. Inside the function, we use a while loop to extract each digit of the input number from the least significant digit to the most significant digit. We reverse the digits by building the reversed number from right to left. The loop continues until the input number becomes 0. Note that the function also handles negative numbers correctly.

Feel free to run this program with different integers to reverse them and see the results.

5. Week2 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?