Dear C++ experts,
I need help with this program:
Write a complete C++ program that reverses the digits of a given positive integer. For example if the user enters a positive number 123456 , your program would print 654321.
Kind Regards,
Ahmed Mahdy
Dear C++ experts,
I need help with this program:
Write a complete C++ program that reverses the digits of a given positive integer. For example if the user enters a positive number 123456 , your program would print 654321.
Kind Regards,
Ahmed Mahdy
Reverse Digits in C++
Vinod Anand
that's not the right approach to the problem. Try writing the string from the last char to the begining to a new string.
digitig
I used this code, but I’m not sure it’s right or wrong (Left Shifting):
//This is the wrong code, full of errors too :(< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#include<iostream>
using namespace std;
void main () {
int number,reverse;
cout<<"Enter an integer digit: ";
cin>>number;
if (0<=number<=9){
reverse=number<<1;
cout<<"Reverse="<<reverse<<endl;
}
else if (9<number<=99); {
reverse=number<<2;
cout<<"Reverse="<<reverse<<endl;
}
else if (99<number<=999); {
reverse=number<<3;
cout<<"Reverse="<<reverse<<endl;
}
else if (999<number<=9999); {
reverse=number<<4;
cout<<"Reverse="<<reverse<<endl;
}
else if(9999<number<=65535); {
reverse=number<<5;
cout<<"Reverse="<<reverse<<endl;
}
}
So Please I need help getting the right to do, heard I have to make something called swap or something like that to reverse digits. Please help me!!
Ahmed Mahdy
evaleah
It is just a simple loop.
int num = 1234;
int ret = 0;
while (num != 0) {
int r = num % 10;
ret = ret * 10 + r;
num = num / 10;
}
printf("%d\n", ret);
Penn Wallace
Thank you so much for the code
I just get this warning which bans pogram to be excuted
warning C4700: uninitialized local variable 'strEntry' used
Kind Regards
,
Ahmed Mahdy
Denis Ruckebusch
Sorry my bad
#include <iostream>
using
namespace std;void
ret_str(char* s){
if(*s != '\0')ret_str(s+1);
cout<<*(s);
}
int
main(){
cout << "Please write your string : " ;
//255 is an arbitrary number it represents the length of your input string
char strEntry[255];
cin >> strEntry;
ret_str(strEntry
); return 0;}
Hope it helps you !!!
laks_win
You might as well go all out and use recursion (an advanced C++ feature) also it's nice and short:
http://code.dreamincode.net/snippet313.htm
Checkup From the Neck Up
Thank you so much for this answer
. I just want a suggestion for code modification where user would enter an integer so that it could got reversed back and here is the code:
#include <iostream>
using
namespace std;void
ret_str(char* s){
if(*s != '\0')ret_str(s+1);
cout<<*(s);
}
int
main(){
ret_str(
"123456"); return 0;}
HannesCoetzee
#include <iostream>
using
namespace std;void
ret_str(char* s){
if(*s != '\0')ret_str(s+1);
cout<<*(s);
}
int
main(){
cout << "Please write your string : " ;
char* strEntry;
cin >> strEntry;
ret_str(strEntry
); return 0;}
great_ghost
How Please I need further help with a code!!
I didn't study strings in college yet!!
Regards,
Ahmed Mahdy
top.gun1
gexaman