Compare two strings and if they are equal backwards - c

Lets say we got two inputs. One being 123 and one being 321. Now, these two should return True.
Another eg. 543 with 345.
This is how far I've gotten:
int a,i=0;
printf("condition value");
scanf("%d",&i);
printf("comparison value");
scanf("%d",&a);
a=a%10;
i=a/10;
if(a==i){
printf("\nTrue");
}
Has anyone got any ideas on how to solve this?

If you want to know whether one string matches the reverse of another string, just compare character-by-character. Even if it's guaranteed that all the characters are digits, it's easier to solve the problem in the string domain.
Even if there's some number-theory trickery that would give you a closed-form solution for fixed-size integers, parsing strings into int in the first place will be slower than just a character-compare loop.
Often you can make your code simpler by taking advantage of limitations on the input, but it looks like this isn't one of those cases.

Given OP wants to check integers based on this comment
Simply reverse the digits of one of the numbers
Remove the least-significant digit from x, one at a time. Use that value to build up the reverse of x. Note that the range of the "reverse of x" is wider than the range of unsigned.
Use unsigned to avoid sign issues.
unsigned long long unsigned_rev(unsigned x) {
unsigned long long rev = 0;
while (x) {
rev = rev*10 + x%10;
x /= 10;
}
return rev;
}

Related

Can anyone explain to me about this decimal to binary convertion program

Can someone explain to me how the calculation works?
what I don't understand is:
the getch(); function, what does that function does?
2.
Can someone explain to me how the int decimal_binary(int n) operates mathematically?
#include<stdio.h>
int decimal_binary (int n);
void main()
{
int n;
printf("Enter decimal number: ");
scanf("%d", &n);
printf("\n%d", decimal_binary(n));
getch();
}
int decimal_binary(int n)
{
int rem, i = 1, binary = 0;
while(n!=0)
{
rem = n % 2;
n = n/2;
binary = binary + rem*i;
i = i*10;
}
return binary;
}
if for example the n = 10
and this is how i calculate it
I'm not going to explain the code in the question, because I fundamentally (and rather vehemently) disagree with its implementation.
When we say something like "convert a number to base 2", it's useful to understand that we are not really changing the number. All we're doing is changing the representation. An int variable in a computer program is just a number (although deep down inside it's already in binary). The base matters when we print the number out as a string of digit characters, and also when we read it from as a string of digit characters. So any sensible "convert to base 2" function should have as its output a string, not an int.
Now, when you want to convert a number to base 2, and in fact when you want to convert to base b, for any base "b", the basic idea is to repeatedly divide by b.
For example, if we wanted to determine the base-10 digits of a number, it's easy. Consider the number 12345. If we divide it by 10, we get 1234, with a remainder of 5. That remainder 5 is precisely the last digit of the number 12345. And the remaining digits are 1234. And then we can repeat the procedure, dividing 1234 by 10 to get 123 remainder 4, etc.
Before we go any further, I want you to study this base-10 example carefully. Make sure you understand that when we split 12345 up into 1234 and 5 by dividing it by 10, we did not just look at it with our eyes and pick off the last digit. The mathematical operation of "divide by 10, with remainder" really did do the splitting up for us, perfectly.
So if we want to determine the digits of a number using a base other than 10, all we have to do is repeatedly divide by that other base. Suppose we're trying to come up with the binary representation of eleven. If we divide eleven by 2, we get five, with a remainder of 1. So the last bit is going to be 1.
Next we have to work on five. If we divide five by 2, we get two, with a remainder of 1. So the next-to-last bit is going to be 1.
Next we have to work on two. If we divide two by 2, we get one, with a remainder of 0. So the next bit is going to be 0.
Next we have to work on one. If we divide one by 2, we get zero, with a remainder of 1. So the next bit is going to be 1.
And now we have nothing left to work with -- the last division has resulted in 0. The binary bits we've picked off were, in order, 1, 1, 0, and 1. But we picked off the last bit first. So rearranging into conventional left-to-right order, we have 1011, which is the correct binary representation of the number eleven.
So with the theory under our belt, let's look at some actual C code to do this. It's perfectly straightforward, except for one complication. Since the algorithm we're using always gives us the rightmost bit of the result first, we're going to have to do something special in order to end up with the bits in conventional left-to-right order in the final result.
I'm going to write the new code as function, sort of like your decimal_binary. This function will accept an integer, and return the binary representation of that integer as a string. Because strings are represented as arrays of characters in C, and because memory allocation for arrays can be an issue, I'm going to also have the function accept an empty array (passed by the caller) to build the return string in. And I'm also going to have the function accept a second integer giving the size of the array. That's important so that the function can make sure not to overflow the array.
If it's not clear from the explanation so far, here's what a call to the new function is going to look like:
#include <stdio.h>
char *integer_binary(int n, char *str, int sz);
int main()
{
int n;
char result[40];
printf("Enter decimal number: ");
scanf("%d", &n);
char *str = integer_binary(n, result, 40);
printf("%s\n", str);
}
As I said, the new function, integer_binary, is going to create its result as a string, so we have to declare an array, result, to hold that string. We're declaring it as size 40, which should be plenty to hold any 32-bit integer, with some left over.
The new function returns a string, so we're printing its return value using %s.
And here's the implementation of the integer_binary function. It's going to look a little scary at first, but bear with me. At its core, it's using the same algorithm as the original decimal_binary function in the question did, repeatedly dividing by 2 to pick off the bits of the binary number being generated. The differences have to do with constructing the result in a string instead of an int. (Also, it's not taking care of quite everything yet; we'll get to one or two more improvements later.)
char *integer_binary(int n, char *binary, int sz)
{
int rem;
int j = sz - 2;
do {
if(j < 0) return NULL;
rem = n % 2;
n = n / 2;
binary[j] = '0' + rem;
j--;
} while(n != 0);
binary[sz-1] = '\0';
return &binary[j+1];
}
You can try that, and it will probably work for you right out of the box, but let's explain the possibly-confusing parts.
The new variable j keeps track of where in the array result we're going to place the next bit value we compute. And since the algorithm generates bits in right-to-left order, we're going to move j backwards through the array, so that we stuff new bits in starting at the end, and move to the left. That way, when we take the final string and print it out, we'll get the bits in the correct, left-to-right order.
But why does j start out as sz - 2? Partly because arrays in C are 0-based, partly to leave room for the null character '\0' that terminates arrays in C. Here's a picture that should make things clearer. This will be the situation after we've completely converted the number eleven:
0 1 2 31 32 33 34 35 36 37 38 39
+---+---+---+-- ~ --+---+---+---+---+---+---+---+---+---+
result: | | | | ... | | | | | 1 | 0 | 1 | 1 |\0 |
+---+---+---+-- ~ --+---+---+---+---+---+---+---+---+---+
^ ^ ^ ^
| | | |
binary final return initial
j value j
The result array in the caller is declared as char result[40];, so it has 40 elements, from 0 to 39. And sz is passed in as 40. But if we want j to start out "at the right edge" of the array, we can't initialize j to sz, because the leftmost element is 39, not 40. And we can't initialize j as sz - 1, either, because we have to leave room for the terminating '\0'. That's why we initialize j to sz - 2, or 38.
The next possibly-confusing aspect of the integer_binary function is the line
binary[j] = '0' + rem;
Here, rem is either 0 or 1, the next bit of our binary conversion we've converted. But since we're creating a string representation of the binary number, we want to fill the binary result in with one of the characters '0' or '1'. But characters in C are represented by tiny integers, and you can do arithmetic on them. The constant '0' is the value of the character 0 in the machine's character set (typically 48 in ASCII). And the bottom line is that '0' + 1 turns into the character '1'. So '0' + rem turns into '0' if rem is 0, or '1' if rem is 1.
Next to talk about is the loop I used. The original decimal_binary function used while(n != 0) {...}, but I'm using do { ... } while(n != 0). What's the difference? It's precisely that the do/while loop always runs once, even if the controlling expression is false. And that's what we want here, so that the number 0 will be converted to the string "0", not the empty string "". (That wasn't an issue for integer_binary, because it returned the integer 0 in that case, but that was a side effect of its otherwise-poor choice of int as its return value.)
Next we have the line
binary[sz-1] = '\0';
We've touched on this already: it simply fills in the necessary null character which terminates the string.
Finally, there's the last line,
return &binary[j+1];
What's going on there? The integer_binary function is supposed to return a string, or in this case, a pointer to the first character of a null-terminated array of characters. Here we're returning a pointer (generated by the & operator) to the element binary[j+1] in the result array. We have to add one to j because we always subtract 1 from it in the loop, so it always indicates the next cell in the array where we'd store the next character. But we exited the loop because there was no next character to generate, so the last character we did generate was at j's previous value, which is j+1.
(This integer_binary function is therefore mildly unusual in one respect. The caller passes in an empty array, and the function builds its result string in the empty array, but the pointer it returns, which points to the constructed string, does not usually point to the beginning of the passed-in array. It will work fine as long as the caller uses the returned pointer, as expected. But it's unusual, and the caller would get confused if accidentally using its own original result array as if it would contain the result.)
One more thing: that line if(j < 0) return NULL; at the top of the loop is a double check that the caller gave us a big enough array for the result we're generating. If we run out of room for the digits we're generating, we can't generate a correct result, so we return a null pointer instead. (That's likely to cause problems in the caller unless explicitly checked for, but that's a story for another day.)
So integer_binary as discussed so far will work, although I'd like to make three improvements to address some remaining deficiencies:
The decimal_binary function as shown won't handle negative numbers correctly.
The way the decimal_binary function uses the j variable is a bit clumsy. (Evidence of the clumsiness is the fact that I had to expend so many words explaining the j = sz-2 and return &binary[j+1] parts.)
The decimal_binary functions as shown only handles, obviously, binary, but what I really want (although you didn't ask for it) is a function that can convert to any base.
So here's an improved version. Based on the integer_binary function we've already seen, there are just a few small steps to achieve the desired improvements. I'm calling the new function integer_base, because it converts to any base (well, any base up to 10, anyway). Here it is:
char *integer_base(int n, int base, char *result, int sz)
{
int rem;
int j = sz - 1;
int negflag = 0;
if(n < 0) {
n = -n;
negflag = 1;
}
result[j] = '\0';
do {
j--;
if(j < 0) return NULL;
rem = n % base;
n = n / base;
result[j] = '0' + rem;
} while(n != 0);
if(negflag) {
j--;
result[j] = '-';
}
return &result[j];
}
As mentioned, this is just like integer_binary, except:
I've changed the way j is used. Before, it was always the index of the next element of the result array we were about to fill in. Now, it's always one to the right of the next element we're going to fill in. This is a less obvious choice, but it ends up being more convenient. Now, we initialize j to sz-1, not sz-2. Now, we do the decrement j-- before we fill in the next character of the result, not after. And now, we can return &binary[j], without having to remember to subtract 1 at that spot.
I've moved the insertion of the terminating null character '\0' up to the top. Since we're building the whole string right-to-left, it makes sense to put the terminator in first.
I've handled negative numbers, in a kind of brute-force but expedient way. If we receive a negative number, we turn it into a positive number (n = -n) and use our regular algorithm on it, but we set a flag negflag to remind us that we've done so and, when we're all done, we tack a '-' character onto the beginning of the string.
Finally, and this is the biggie, the new function works in any base. It can create representations in base 2, or base 3, or base 5, or base 7, or any base up to 10. And what's really neat is how few modifications were required in order to achieve this. In fact, there were just two: In two places where I had been dividing by 2, now I'm dividing by base. That's it! This is the realization of something I said back at the very beginning of this too-long answer: "The basic idea is to repeatedly divide by b."
(Actually, I lied: There was a fourth change, in that I renamed the result parameter from "binary" to "result".)
Although you might be thinking that this integer_base function looks pretty good, I have to admit that it still has at least three problems:
It won't work for bases greater than 10.
It can occasionally overflow its result buffer.
It has an obscure problem when trying to convert the largest negative number.
The reason it only works for bases up to 10 is the line
result[j] = '0' + rem;
This line only knows how to create ordinary digits in the result. For (say) base 16, it would also have to be able to create hexadecimal digits A - F. One quick but obfuscated way to achieve this is to replace that line with
result[j] = "0123456789ABCDEF"[rem];
This answer is too long already, so I'm not going to get into a side discussion on how this trick works.
The second problem is hiding in the lines I added to handle negative numbers:
if(negflag) {
j--;
result[j] = '-';
}
There's no check here that there's enough room in the result array for the minus sign. If the array was just barely big enough for the converted number without the minus sign, we'll hit this part of the code with j being 0, and we'll subtract 1 from it, and fill the minus sign in to result[-1], which of course doesn't exist.
Finally, on a two's complement machine, if you pass the most negative integer, INT_MIN, in to this function, it won't work. On a 16-bit 2's complement machine, the problem number is -32768. On a 32-bit machine, it's -2147483648. The problem is that +32768 can't be represented as a signed integer on a 16-bit machine, nor will +2147483648 fit in 32 signed bits. So a rewrite of some kind will be necessary in order to achieve a perfectly general function that can also handle INT_MIN.
In order to convert a decimal number to a binary number, there is a simple recursive algorithm to apply to that number (recursive = something that is repeated until something happen):
take that number and divide by 2
take the reminder
than repeat using as current number, the original number divided by 2 (take in account that this is a integer division, so 2,5 becomes 2) until that number is different to 0
take all the reminders and read from the last to the first, and that's the binary form of that number
What that function does is exactly this
take the number and divide it by 2
takes the reminder and add it in into the variable binary multiplied by and i that each time is multiplied by 10, in order to have the first reminder as the less important digit, and the last one as the most significant digit, that is the same of take all the reminders and read them from the last to the first
save as n the n/2
and than repeat it until the current number n is different to 0
Also getch() is sometimes used in Windows in order to hold the command prompt open, but is not that recommended
getchar() stops your program in console. Maths behind function looks like this:
n=7:
7%2=1; //rem=1
7/2=3; //n=3
binary=1;
next loop
n=3:
3%2=1;
3/2=1; //n=1;
binary=11 //1 + 1* 10
final loop
n=1:
1%2=1;
1/2=0; //n=0;
binary=111 //11+1*100

Bizarre situation - 100 digit number input asked

I need to make a program that can take numbers of up to 100 digits as input. No standard int datatype will be able to do that! I've never come across such a bizarre situation.
I don't get it at all. How am I supposed to solve this?
The question I'm working on is this:
A whole number will be given, and you have to make a program that will
determine whether it's an even or odd number.
Input Specification
In the first line, there will be an integer T denoting the number of
testcases. In the following T lines, a non-negative integer will be
given. The number can have a maximum of 100 digits.
Output Specification
For every whole number given, you will have to print whether it's odd
or even as output.
Can anyone guide me on how to solve the problem (if it is even possible to do so)?
The program will take a number as input and determine whether it's odd or even.
Read the input in a string (char [101]) and analyze only last digit to check whether number is odd or even. Rest of the digits are irrelevant for this task.
There is no standard numeric type guaranteed to hold that many digits. You need to store the value in a different way, e.g., as a string or other array. If you need to perform arithmetic on these numbers, you need to implement those operations for the types you use, or use some kind of arbitrary precision library.
(Tip: You also don't necessarily need the entire number for certain operations, e.g., you can tell whether it is even or odd by looking only at the last digit…)
The exercise is to determine whether a whole number of up to 100 digits is odd or even.
This does not require you to perform arbitrary arithmetic on the number, so if you need to handle numbers larger than the largest integer type on your system, you can treat them as a string of digits.
Whether it is even or odd only depends on the last digit.
To all those who took the time and effort to answer this question,
Thanks for the answers. And thanks for showing the way. I greatly appreciate the help!
The solution to the problem which I have coded is -
#include <stdio.h>
#include <string.h>
int main()
{
int T, i, j;
scanf("%d", &T);
for (i=1; i<=T; i++)
{
char N[101];
scanf("%s", N);
int k = strlen(N);
int p = N[k-1] - 48; //char to int conversion
if (p % 2 == 1)
{
printf("odd\n");
}
else
{
printf("even\n");
}
}
return 0;
}

Automated online judge rejects my solution to math puzzle

Hello guys I have found this question on some website
Description
Spade is a very good detective but he is not so good at math, this time his friend Archer has come to him with a very interesting math problem. Given two numbers 1 <= N <= 10^9 and 1 <= M <= 100, how many positive numbers with length N have the sum of its digits divisible by M. Archer is very obsessive and does not want numbers with leading zeroes to count. Spade has hired you to solve this problem, now his reputation is in your hands.
Input specification
Input contains a single line with two numbers N and M separated by a single space.
Output specification
Output a single line with the answer to the problem modulo 1000007.
Sample input
2 2
Sample output
45
My Code
Though I am getting the expected output it is not accepting my answer. Can any one please point out the error in the code.
#include <stdio.h>
int main(void)
{
int n,m,i,firstnum=1,count=0,lastnum=0,j=0,no=0;
scanf("%d",&n);
if (n>=1) {
scanf("%d",&m);
if (m>=1 && m<=100)
{
for (i=1;i<n;++i) {
firstnum*=10;
++count;
}
for (i=0;i<=count;++i)
lastnum=lastnum*10+9;
if (firstnum%m==0)
++no;
for (i=++firstnum;i<=lastnum;++i) {
j=i;
int sum=0,r;
do {
r=j%10;
sum+=r;
j=j/10;
} while(j);
if (sum%m==0)
++no;
}
}
}
printf("%d",no-1);
return 0;
}
You're trying to solve this problem by brute force, which won't work for the sizes involved. A number of length 10^9 can be up to 10^(10^9), which is a huge number that won't fit in an int or even a long long int. Even if it did, trying to enumerate all numbers of this length one by one would take billions of years.
You need to come up with an approach that doesn't look at the numbers one by one. Just like you can calculate that there are 33 numbers between 1 and 100 that are divisible by 3 without looking at them all, you need to come up with such an approach here. But here it will be harder because you will need to do it without actually calculating the value of 10^n.
Your approach is not actually correct.
You have declared "firstnum" variable as int, i.e. it cannot hold value greater than 2^(32-1) (on most of the on line judges). The max value of n in 10^9, hence you are trying to put 10^(10^9) in worst case.
I Hope you have got my point. I you want the approach you can comment below my answer. I don't want to spoil the question for you. :)

extracting int into array from long long in c

I'm new to programming and have no experience with arrays of undefined length.
I want to extract specific numbers from a long long that is generated by user input (because I failed to do so with a char).
The best result would be for me that it would end up in an array with each digit its own int so that i can run loops and do math with them.
Say that i want to get the 20th digit from the right, with the amount of digits that the user put in GetLongLong unknown, this is what my code looks like:
int spec20 = (digits / 10000000000000000000) % 10;
How can I make this simpler and use an array to store specific digits?
you can do this:
char str[100]; // your number should most probably fit
sprintf(str,"%lld",digits);
then str will contain string representation of your number, and you can find 20th element from the right (starting to count from 1) like this (not the only way for sure).
int d_len = strlen(str);
int the_20th_pos = (d_len>20? d_len-20: -1); // take care of numbers less than 20 digits
int the_20th = (int)(the_20th_pos<0? 0: str[d_len-the_20th_pos]-'0');

Why is prime number check getting wrong results for large numbers?

This small C script checks if a number is a prime... Unfortunately it doesn't fully work. I am aware of the inefficiency of the script (e.g. sqrt optimization), these are not the problem.
#include <stdio.h>
int main() {
int n, m;
printf("Enter an integer, that will be checked:\n"); // Set 'n' from commandline
scanf("%d", &n); // Set 'n' from commandline
//n = 5; // To specify 'n' inside code.
for (m = n-1; m >= 1; m--) {
if (m == 1) {
printf("The entered integer IS a prime.\n");
break;
}
if (n % m == 0) {
printf("The entered integer IS NOT a prime.\n");
break;
}
}
return 0;
}
I tested the programm with a lot of numbers and it worked... Then I tried a bigger number (1231231231231236) which is clearly not a prime...
BUT: the program told me it was!?
What am I missing...?
The number "1231231231231236" is too big to fit in an "int" data type. Add a printf statement to show what number your program thinks you gave it, and if that's prime, your program works fine; else, you might have a problem that merits checking. Adding support for integers of arbitary size requires considerable extra effort.
The reason you are having this problem is that intrinsic data types like int have a fixed size - probably 32 bits, or 4 bytes, for int. Given that, variables of type int can only represent 2^32 unique values - about 4 billion. Even if you were using unsigned int (you're not), the int type couldn't be used to store numbers bigger than around 4 billion. Your number is several orders of magnitude larger than that and, as such, when you try to put your input into the int variable, something happens, but I can tell you what doesn't happen: it doesn't get assigned the value 1231231231231236.
Hard to know without more details, but if your ints are 32-bit, then the value you've passed is outside the allowable range, which will no doubt be represented as something other than the value you've passed. You may want to consider using unsigned int instead.
The given number is too large for integer in C. Probably it only accepted a part of it. Try Printing the value of n.

Resources