Palindrome without using extra space [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I know various ways to check if an integer is a palindrome or not by using string conversion, stack and number breaking, but here question is
"How can we check wether an integer is a palindrome or not, without using any extra space?"

You can revert a number with some code like:
int revert(int num) {
int reverted = 0;
while (num) {
reverted = reverted*10 + num%10;
num /= 10;
}
return reverted;
}
And now you only check if
num == revert(num)
That is all. Sorry for giving the exact solution instead of just a tip, but I don't think I could have given any tip without the solution itself.

Related

Execution Control In C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I was solving a problem where i was asked to output the list of numbers satisfying certain conditions. The score awarded depended upon the size of the output ( as it is a partial marking question). How do i restrict my code to keep outputting the numbers till it does hits the time limit.
It seems like the obvious structure would be something like this:
while (current_time < end_time) {
current_number = *next_number++;
if (meets_conditions(current_number))
output(current_number);
}

How to print a character code from char*? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm getting a character from a user input using getchar(), but instead of using cout to print the character I want to print the character code, like the one for Return, ESC, etc, so I can use in my code later to check using a if.
To do this you can cast the char to an int,
int charval = (int) mychar;
printf("%d\n", c); will show you the character code.
so I can use in my code later to check using a if.
You don't need to expicitly convert it to an int for that.
if(getchar() == char_code)
doSomething();
Cast the char as an int and print the int value.

what is the complexity of the following code? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the complexity of the following piece of code??
for (int i = 1; i * i <= n; i++)
{
if (n%i == 0)
//do anything
}
The loop runs √n times, and the conditional is met every time i is a factor of n — the latter is a non-trivial condition and needs to be analysed carefully. It depends on the prime factorisation of n. For example, if n is prime, the condition is only true once, for i == 1, and never again.

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

how can i solve binary system in c++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
how can i see the answer when i need to solve the decimal code to binary code?
decimal code to binary code? i think that you mean decimal number to binary number, so use std::bitset e.g.
#include <bitset>
...
int i = 49;
std::cout << std::bitset<sizeof(i)*8>(i).to_string() << std::endl;