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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support