Table of Contents
Examples
Approach used in the below program is as follows
Example
输出
Home Backend Development C++ Reverse a number in C++ using stack

Reverse a number in C++ using stack

Sep 14, 2023 pm 12:45 PM
number reverse stack

Reverse a number in C++ using stack

We are given an integer number Num as input. The goal is to find the reverse of the number using stack.

Stack:- A stack is a data structure in C which stores data in LIFO ( Last in First Out) manner. Major operations of stack are-:

Declaration-: stack stck; //stck is now a stack variable.

  • Finding Top using top(). Function stck.top() returns reference of top element in the stck

  • Removing Top using pop(). Function removes topmost element from the stck

  • Adding element to top using push(). Function stck.push( value ) adds item value in stack. Value should be of type stck.

  • Check if staxk is empty using empty(). Function stck.empty() returns true if stack is empty.

Examples

Input − Num = 33267

Output − Reverse of number is: 76233

Explanation

First we will push all elements to stack

7 - 6 - 2 - 3 - 3 ← top

7 * 10000 6 * 1000 2*100 3*10 3*1 ←

= 70000 6000 200 30 3 ←

= 76233

Input − Num = 111000

Output − Reverse of number is: 111

Explanation

First we will push all elements to stack

0 - 0 - 0 - 1 - 1 - 1 ← top

0 * 100000 0 * 10000 0*1000 1*100 1*10 1*1 ←

= 0 0 0 100 10 1 ←

= 111

Approach used in the below program is as follows

In this approach we will first take remainders of input number and push to stack and reduce number by 10 until number becomes 0. In this way stack will be filled with top as first digit.

  • Take the input number Num.

  • Take empty stack for integers using stack stck.

  • Function pushDigts(int num1) takes num1 and adds it to stack with first digit on top.

  • Take rem as variable.

  • Using a while loop check if num1 is non-zero, if true then set rem=num1.

  • Push rem to stack.

  • Reduce num1 by 10 for 2nd digit and so on.

  • Now reverse the number using elements of stack with function revrseNum().

  • Take variables revrs, topp, temp, i.

  • While the stack is not empty

  • Take the topmost element as topp=stck.top().

  • Reduce stack using stck.pop().

  • Set temp=topp*i.

  • Add temp to revrs.

  • Increase i by i*10 in multiples of 100.

  • At the end return the reverse of the input num as revrs.

  • Print result obtained inside main.

Example

#include <bits/stdc++.h>
using namespace std;
stack <int> stck;
void pushDigts(int num1){
   int rem;
   while (num1 > 0){
      rem=num1 % 10;
      stck.push(rem);
      num1 = num1 / 10;
   }
}
int revrseNum(){
   int revrs = 0;
   int i = 1;
   int temp;
   int topp;
   while (!stck.empty()){
      topp=stck.top();
      stck.pop();
      temp=topp*i;
      revrs = revrs + temp;
      i *= 10;
   }
   return revrs;
}
int main(){
   int Num = 43556;
   pushDigts(Num);
   cout<<"Reverse of number is: "<<revrseNum();
   return 0;
}
Copy after login

输出

如果我们运行上面的代码,它将生成以下输出

Reverse of number is: 65534
Copy after login

The above is the detailed content of Reverse a number in C++ using stack. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iOS 17: How to change iPhone clock style in standby mode iOS 17: How to change iPhone clock style in standby mode Sep 10, 2023 pm 09:21 PM

Standby is a lock screen mode that activates when the iPhone is plugged into the charger and oriented in horizontal (or landscape) orientation. It consists of three different screens, one of which is displayed full screen time. Read on to learn how to change the style of your clock. StandBy's third screen displays times and dates in various themes that you can swipe vertically. Some themes also display additional information, such as temperature or next alarm. If you hold down any clock, you can switch between different themes, including Digital, Analog, World, Solar, and Floating. Float displays the time in large bubble numbers in customizable colors, Solar has a more standard font with a sun flare design in different colors, and World displays the world by highlighting

How to customize and edit standby mode on iPhone: What's new in iOS 17 How to customize and edit standby mode on iPhone: What's new in iOS 17 Sep 21, 2023 pm 04:01 PM

Standby is a new feature in the iOS 17 update that provides a new and enhanced way to access information when your phone is idle quickly. With StandBy, you can conveniently check the time, view upcoming events, browse your calendar, get weather updates for your location, and more. Once activated, the iPhone will intuitively enter standby mode when set to landscape while charging. This feature is perfect for wireless charging points like your bedside table, or when you're away from your iPhone charging during daily tasks. It allows you to swipe through various widgets displayed in standby to access different sets of information from various applications. However, you may want to modify these widgets or even delete some based on your preferences and the information you need frequently. So let's dive into

Generate random numbers and strings in JavaScript Generate random numbers and strings in JavaScript Sep 02, 2023 am 08:57 AM

The ability to generate random numbers or alphanumeric strings comes in handy in many situations. You can use it to spawn enemies or food at different locations in the game. You can also use it to suggest random passwords to users or create filenames to save files. I wrote a tutorial on how to generate random alphanumeric strings in PHP. I said at the beginning of this post that few events are truly random, and the same applies to random number or string generation. In this tutorial, I'll show you how to generate a pseudo-random alphanumeric string in JavaScript. Generating Random Numbers in JavaScript Let’s start by generating random numbers. The first method that comes to mind is Math.random(), which returns a float

C++ program to round a number to n decimal places C++ program to round a number to n decimal places Sep 12, 2023 pm 05:13 PM

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding. Among the different approaches, it is important to use a C-like format string, use the precision argument, and use the round() function from the math library. Let’s look at them one by one. with

iOS 17: How to customize widgets in standby mode iOS 17: How to customize widgets in standby mode Sep 17, 2023 pm 01:57 PM

Standby is a new customizable lock screen mode in iOS 17 that can be activated when the iPhone is charging and lying on its side. Think of it as a kind of smart display for your iPhone, allowing quick access to different browsable information screens that can be viewed from a distance while your device is charging in the kitchen, desk, or nightstand, for example. The custom standby widget consists of three screens and can be accessed by swiping horizontally on the iPhone display. The first screen is where the interactive widgets are located, while swiping to the left reveals the second and third screens, which display photos from the photo gallery and a large clock display respectively. The widget screen consists of two interactive widget stacks displayed side by side that you can swipe up and down independently. These stacks are like home screen widget stacks

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

Find numbers that are not divisible by any number in a range, using C++ Find numbers that are not divisible by any number in a range, using C++ Sep 13, 2023 pm 09:21 PM

In this article, we will discuss the problem of finding numbers between 1 and n (given) that are not divisible by any number between 2 and 10. Let us understand this with some examples - Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharenotdivisible. Solved Simple method if

Represent a number as the sum of the largest possible number of prime numbers in C++ Represent a number as the sum of the largest possible number of prime numbers in C++ Aug 31, 2023 pm 04:29 PM

Discuss a problem, for example, given a number N, we need to split the number into its maximum prime numbers. , we can subtract a prime number from N and then check the difference in prime numbers. If the difference is a prime number, then we can express N as the sum of two prime numbers. But here we have to

See all articles