Question: Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in

Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code.

#include

#include

#include

using std::cout;

using std::cin;

using std::endl;

//function templates

int reverseNum(int);

int main()

{

//variables

char buf[100];

int num;

while (true)

{

//prompt user for input

cout << "Enter the number to performreverse (q or Q to quit): ";

cin >> buf;

num = atoi(buf);

//quit program if user entered a q

if (buf[0] == 'q' || buf[0] == 'Q') break;

cout << "Entered number: " << num << endl;

cout << "Reversed number: " << reverseNum(num) << endl;

}

}

int reverseNum(int number)

{

int reversedNum = 0;

int reminder;

int zeros = 0;

bool firsttime = true;

while (number > 0)

{

reminder = number % 10;

//If reminder is 0 and the first non zero number is not yet encountered

if (reminder == 0 && firsttime) {

zeros++;

}

//If first non zero digit is encounrered, zeros encountered later should'nt be counted

else {

firsttime = false;

}

reversedNum = reversedNum * 10 + reminder;

number /= 10;

}

cout << "Reversed number With leading zeros : ";

for (int i = 0; i

cout << 0;

}

cout << reversedNum << endl;

return reversedNum;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!