• Post author:
  • Reading time:3 mins read
  • Post category:Programming

Logic for reverse a number

This approach is completely based on mathematical calculations. First check for if number is negative or positive. If that number is negative convert that number to positive number by multiplying with -1. Then initialize reverse number variable with zero. Until the number is not zero perform these operations.

  • Find remainder by dividing that number by 10.
  • Multiply reverse number variable with 10 and add remainder to reverse number and update reverse number with new value.
  • Divide that number with 10.

Pre-requisite Programming Concepts for reverse a number

  • Input output operations in C | C++ and Java.
  • Data types in C | C++ and Java.
  • Modulus (%) and Division (/) operators in C | C++ and Java.
  • Assignment operator in C | C++ and Java
  • While loop in C | C++ and Java

Reverse a number in c

#include<stdio.h>
int main(int argc, char const *argv[])
{
	//input the number
	int number;
	printf("Reverse a number in c \nEnter a number to Reverse");
	scanf("%d",&number);
	int remainder=0;
	int temp=number;
	int reverse_number=0;
	//check for negative number and convert that to positive first
	int negative_flag=0;
	if( number < 0 )
	{
		negative_flag = 1;
		temp = temp * (-1);
	}
	while( temp>0 )
	{
		remainder = temp % 10;
		reverse_number = ( 10*reverse_number) + remainder ;
		temp=temp/10; 
	}  
	if( negative_flag )
	{
		reverse_number = (-1)*reverse_number;
		
	}
	printf( "Reverse of %d is %d\n",number,reverse_number );

	return 0;
}
Reverse a  number in c
Reverse a number in c

Reverse a number in c++

#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
	//input the number
	int number;
	cout<<"Reverse a number in c++ \nEnter a number to Reverse\n";
	cin>>number;
	int remainder=0;
	int temp=number;
	int reverse_number=0;
	//check for negative number and convert that to positive first
	int negative_flag=0;
	if( number < 0 )
	{
		negative_flag = 1;
		temp = temp * (-1);
	}
	while( temp>0 )
	{
		remainder = temp % 10;
		reverse_number = ( 10*reverse_number) + remainder ;
		temp=temp/10; 
	}  
	if( negative_flag )
	{
		reverse_number = (-1)*reverse_number;
		
	}
	cout<<"Reverse of "<<number<<" is "<<reverse_number<<"\n";

	return 0;
}
Reverse a number in c++
Reverse a number in c++

Reverse a number in java

import java.util.Scanner;

public class reverse_number {

	public static void main(String[] args) {
		//input a number
		System.out.println( " Reverse a number in java \n Enter a number" );
		int number;
		Scanner stdin =new Scanner( System.in);
		number=stdin.nextInt();
		boolean isNegative=false;
		int temp=number;
		int remainder=0;
		int reverseNumber=0;
		if ( temp < 0 )
		{
			temp = temp * (-1);
			isNegative = true;
		}
		 while ( temp>0  )
		 {
			 remainder = temp % 10;
			 reverseNumber = ( reverseNumber * 10 ) + remainder;
			 temp=temp/10;
		 }
		 if( isNegative )
		 {
			 reverseNumber = reverseNumber * (-1);
		 }
		 System.out.println( "Reverse of " + number + " is " + reverseNumber  );

	}

}
Reverse a number in java
Reverse a number in java