# How to reverse a number in MATLAB?

• Last Updated : 05 Aug, 2021

In this article, we will discuss the “Reversing of a number” in MATLAB that can be done using the multiple methods which are illustrated below.

## Using str2num()

The str2num() function is used to convert the specified character array or string to a numeric array.

Syntax:  str2num(chr)

Parameters: This function accepts a parameter .

• char: This is the specified character array or string.

Example:

## Matlab

 % MATLAB code for str2num()% Calling the str2num() functions% over the character 10str2num('10')

Output:

ans =  10

## Using fliplr()

The fliplr() function is used to flip the specified number from left to right.

Syntax: fliplr(num)

Parameters: This function accepts a parameter.

• num: This is the specified number.

Example:

## Matlab

 % MATLAB code for fliplr()% Calling the fliplr() function% over a number string '123'fliplr('123')

Output:

ans = 321

## Using str2num(), fliplr(), and num2str()

The num2str() function is used to convert the specified numbers to a character array.

Syntax: num2str(num)

Parameters: This function accepts a parameter.

• num: This is the specified number.

Example:

## Matlab

 % MATLAB code for reversing a specified number% using the combination of str2num(), fliplr()% and num2str()% Specifying a number to reverseNumber = 2468 % Calling the combination of three functions% str2num(), fliplr(), and num2str()% over the above number to reverseReversed_Number = str2num(fliplr(num2str(Number)))

Output:

Number =  2468
Reversed_Number =  8642

## Using polyval( ) and num2str( )

The polyval(p, x) function is used to return the value of a polynomial of degree n evaluated at x. The input argument p is a vector of length n+1 whose elements are the coefficients in descending powers of the polynomial to be evaluated.

Syntax: polyval(p, x)

Parameters: This function accepts two parameters,

• p: This is the specified input vector of length n+1.
• x: This is the specified matrix or vector or array.

## Matlab

 % MATLAB code for polyval() for reverse a number% Specifying a number to reverseNumber = 3579 % Calling the num2str() over the% above numbers = num2str(Number) - '0'; % Calling the polyval() functionReversed_Number = polyval(s(end:-1:1),10)

Output:

Number =  3579
Reversed_Number =  9753

## Using flip( ) and  sprintf()

The sprintf() function is used to format the data into a string or character vector.

Syntax:  sprintf(formatSpec, A1, …, An)

Here,   sprintf(formatSpec, A1, …, An) is used to format the data in arrays A1,…, An uses the formatting operators specified by formatSpec and returns the resulting text in str.

Example:

## Matlab

 % MATLAB code for reverse a number% using flip() and sprintf()% Calling the flip() function over the% sprintf() function with number% 5678 as its parameter to reverseReversed_Number = flip(sprintf('%d', 5678), 2)

Output:

Reversed_Number = 8765

My Personal Notes arrow_drop_up