How to reverse a number in MATLAB?


In MATLAB, there are various methods and functions for reversing a given number. In this tutorial, I will explain all these methods one-by-one with the help of examples.

Reverse a Number Using "fliplr" Function in MATLAB

The "fliplr" is a built-in function in MATLAB that we can use to reverse a given number.

Syntax

reversed_num = fliplr(number);

Here, the "number" must be a string.

Here are the steps involved in reversing a given number using the "fliplr" function in MATLAB.

  • Step (1) − Input the number.

  • Step (2) − Convert the input number to a string. For this use the "num2str" function.

  • Step (3) − Use the "fliplr" function to reverse the characters in the string.

  • Step (4) − Convert the reversed string back to the number. For this, use the "str2double" function.

  • Step (5) − Display the reversed number.

Example

The following MATLAB example demonstrates the implementation of these steps of reversing a number using the "fliplr" function.

% MATLAB code to reverse a number using flipr function
% Input the number
num = input('Please enter your multi-digit number:');

% Convert the input number to string
num_string = num2str(num);

% Reverse the digits in the number
reversed_string = fliplr(num_string);

% Convert the reversed string back to number format
reversed_num = str2double(reversed_string);

% Display the input number and reversed number
disp(['The input number: ' num2str(num)]);
disp(['The reversed number: ' num2str(reversed_num)]);

Output

When your run this code, it will produce the following output

Please enter your multi-digit number:
56789
The input number: 56789
The reversed number: 98765

This MATLAB code shows how we can use the "fliplr" function to reverse a number.

Reverse a Number Using "num2str" and "flip" Functions in MATLAB

The "num2str" and "flip" are the two built-in functions in MATLAB that we can use to reverse a number. The "num2str" function converts the input number to a string and the "flip" function reverse the number.

Syntax

num_string = num2str(number);
reversed_str = flip(num_string);

The steps involved in reversing a number using the "num2str" and "flip" functions in MATLAB are explained below.

  • Step (1) − Input the number.

  • Step (2) − Convert the input number to a string using the "num2str" function.

  • Step (3) − Use the "flip" function to reverse the number/string.

  • Step (4) − Convert the reversed string back to the number format using the "str2num" function.

  • Step (5) − Display the reversed number.

Example

The MATLAB example given below demonstrates the implementation of these steps of reversing a number using the "num2str" and "flip" functions.

% MATLAB code to reverse a number using num2str and flip function
% Input the number
num = input('Please enter your multi-digit number:');

% Convert the input number to string
num_string = num2str(num);

% Reverse the number
reversed_string = flip(num_string);

% Convert the reversed string back to number format
reversed_num = str2num(reversed_string);

% Display the input number and reversed number
disp(['The input number: ' num2str(num)]);
disp(['The reversed number: ' num2str(reversed_num)]);

Output

When your run this code, it will produce the following output

Please enter your multi-digit number:
71468
The input number: 71468
The reversed number: 86417

In this example, we have reversed the input number by using the "num2str" and "flip" functions in MATLAB.

Reverse a Number Using "str2num" and "reverse" Functions in MATLAB

Both "str2num" and "reverse" functions are built-in functions in MATLAB. We can use these functions together to reverse a number.

Here are the steps involved in reversing a number using the "str2num" and "reverse" functions in MATLAB.

  • Step (1) − Input the number.

  • Step (2) − Convert the input number to string using the "num2str" function.

  • Step (3) − Reverse the number string using the "reverse" function.

  • Step (4) − Convert the number string back to the number format using the "str2num" function.

  • Step (5) − Display the reversed number.

Example

Let us take an example to understand these functions to reverse a number using MATLAB.

% MATLAB code to reverse a number using str2num and reverse function
% Input the number
num = input('Please enter your multi-digit number:');

% Convert the input number to string
num_string = num2str(num);

% Reverse the number
reversed_string = reverse(num_string);

% Convert the reversed string back to number format
reversed_num = str2num(reversed_string);

% Display the input number and reversed number
disp(['The input number: ' num2str(num)]);
disp(['The reversed number: ' num2str(reversed_num)]);

Output

When your run this code, it will produce the following output

Please enter your multi-digit number:
698746
The input number: 698746
The reversed number: 647896

Reverse a Number Using "sprintf" and "flip" Functions in MATLAB

In MATLAB, we can also reverse a number using the "sprintf" and "flip" functions.

The step-by-step process of reversing a number using the "sprintf" and "flip" functions is explained below.

  • Step (1) − Input the number.

  • Step (2) − Use the "sprintf" function to convert the number into a string.

  • Step (3) − Use the flip function to reverse the number string.

  • Step (4) − Convert the reversed number string to number format using the "str2double" function.

  • Step (5) − Display the reversed number.

Example

Here is the example demonstrating the MATLAB code implementation to reverse a number using the "sprintf" and "flip" functions in MATLAB.

% MATLAB code to reverse a number using sprintf and flip function
% Input the number
num = input('Please enter your multi-digit number:');

% Convert the input number to string
num_string = sprintf('%d', num);

% Reverse the number
reversed_string = flip(num_string);

% Convert the reversed string back to number format
reversed_num = str2double(reversed_string);

% Display the input number and reversed number
disp(['The input number: ' num2str(num)]);
disp(['The reversed number: ' num2str(reversed_num)]);

Output

When your run this code, it will produce the following output

Please enter your multi-digit number:
68974
The input number: 68974
The reversed number: 47986

In this example, I have demonstrated how to reverse the order of a number using the "sprintf" and "flip" functions in MATLAB.

Conclusion

In conclusion, MATLAB provides various methods to reverse a number. In this tutorial, I explained the commonly used methods to reverse a number using MATLAB. In all these methods, we have used the built-in functions in MATLAB.

In the examples included in this tutorial, I have allowed users to input the number by using the "input" function. Run all these codes in your MATLAB compiler with different input numbers to see the results.

Updated on: 09-Oct-2023

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements