Enter a unsigned int, reverse it and see if its' still in range, if it is, print the reverse number, if not, print your number is out of range
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
unsigned int a = UINT_MAX; // 0xffff
printf("max unsigned int = %u\n\n", a);
unsigned int x = 0;
printf("please enter any unsigned int,\nit will show the reverse number if it's in range\n(enter 10 digit only)\n\n");
while (scanf("%u", &x) != EOF) {
//printf("x = %u\n", x);
// above %d will print each UINT's binary oder, ex: enter 4294967292, x = -1
// if change %u, it will print entered number, ex: enter 4294967292, x = 4294967292
unsigned int temp = x, result = 0;
int m = 0;
while (temp > 0) {
unsigned int digit = temp % 10;
if (result > 429496729) {
m++; //if reversed 9digits is already bigger 429496729,
//then 'result = result * 10 + digit', it will have over flow problem.
}
result = result * 10 + digit;
temp /= 10;
if (temp == 0) break;
}
printf("m=%d\n", m);
if (m >= 1)
printf("out of range\n");
else
printf("result is %u\n", result);
printf("\n");
}
return 0;
}
You never return the reversed number, all you return is the final (rightmost) digit.
And your function doesn't "return itself", it returns the result of calling itself, which is not the same.
Your test for overflow is incorrect because you compare the unsigned int result with a value that is larger than the range of this type, so the test always fails.
There are 2 ways to check for overflow:
taking advantage of unsigned arithmetics, you can compare is the result is smaller than the previous result.
alternately, you can compare if result is larger than UINT_MAX / 10 or if it is equal, if digit is larger than UINT_MAX % 10. This method works for all integer types, unsigned and signed.
Here is the modified code:
#include <stdio.h>
#include <limits.h>
int main(void) {
unsigned int x;
printf("max unsigned int = %u\n\n", UINT_MAX);
printf("please enter any unsigned int,\n"
"it will show the reverse number if it is in range\n"
"(enter 10 digit only)\n\n");
while (scanf("%u", &x) == 1) {
unsigned int temp = x, result = 0;
int overflow = 0;
while (temp > 0) {
unsigned int digit = temp % 10;
if (result > UINT_MAX / 10
|| (result == UINT_MAX / 10 && digit > UINT_MAX % 10)) {
overflow++; // the result will not fit
break;
}
result = result * 10 + digit;
temp /= 10;
}
if (overflow)
printf("out of range for unsigned int type\n\n");
else
printf("result is %u\n\n", result);
}
return 0;
}
Thanks for your answer, much appreciated!
1. Regarding to "compare if result is larger than UINT_MAX / 10 or if it is equal, if digit is larger than UINT_MAX % 10. This method works for all integer types, unsigned and signed."
Is (result == UINT_MAX / 10 && digit > UINT_MAX % 10)) necessary?
I assume input below digits
(reminder: only enter digit within UINT_MAX)
unsigned int 4294967295 (10digit), reverse will be 592769492(4) /out
range
unsigned int 4294967294 (10digit), reverse will be 492769492(4)
/out range
unsigned int 4227694924 (10digit), reverse will be 429496722(4) / in
range
unsigned int 927694924(9digit), reverse will be 429496729 / in
range
the closest input is 3927694924 i can think of, and it will fit (result == UINT_MAX / 10 && digit > UINT_MAX % 10)) / in range
However, I've done the same for signed int, please see the code at below, but im not sure how to apply your method of signed int:
#include <stdio.h>
#include <limits.h>
int main(void) {
int x;
printf("max int = %d\n", INT_MAX);
printf("min int = %d\n\n", INT_MIN);
printf("please enter any integer within -2,147,483,648 ~ 2,147,483,647,\n"
"it will show the reverse number if it is in range\n"
"(enter 10 digit or less)\n\n");
while (scanf("%d", &x) == 1) {
int temp = x, result = 0;
int overflow = 0;
while (temp >= INT_MIN || temp <= INT_MAX) {
int digit = temp % 10;
printf("temp is %u\n", temp);
printf("result is %u\n", result);
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && digit > INT_MAX % 10)) {
overflow++; // the result will not fit
break;
}
if (result < INT_MIN / 10 || (result == INT_MIN / 10 && digit > INT_MIN % 10)) {
overflow++; // the result will not fit
break;
}
result = result * 10 + digit;
temp /= 10;
if(temp == 0) break;
}
if (overflow)
printf("out of range for int type\n\n");
else
printf("result is %d\n\n", result);
}
return 0;
}
Related
Here is my code:
#include <stdio.h>
#include <math.h>
int main() {
int num, i = 0, new_num = 0, u;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
u = num % 10;
if (u % 2 == 0)
u = u;
if (u % 2 == 1)
u = u - 1;
new_num = new_num + u * pow(10, i);
num = num / 10;
i++;
}
printf("The new number is: %d", new_num);
return 0;
}
Now, when I am doing this in gcc(VS Code), for 2-digit number everything is ok. But for digits more than three I am getting a error. Like Input=23145 Output=22043. But I was expecting output=22044.
Also, if I run the same code in DevC/C++, there is no error.
Can anyone help me out in this?
Your program produces the expected output on my system: 22044 for 23145, but this might depend on the implementation of the pow function.
The reason you get a different output is probably a side effect of a precision issue with the pow() function in your C library: if pow(x, y) is implemented as exp(y * log(x)), the result for integral values of x and y could be very close but inferior to the actual integral value, causing the conversion to int to produce the previous integer. Some C library authors make a special case of integral arguments to avoid this problem, but it is highly recommended to avoid floating point functions for integer arithmetics to prevent such tricky issues.
I would advise some more changes in your code:
test the return value of scanf().
remove the if (u % 2 == 0) u = u; part, it has no effect.
in any case, there should be an else clause to not use the result of the previous case when testing for odd digits.
do not use the floating point function pow(): just keep a multiplier variable and update it in the loop.
the program does not handle negative numbers.
Here is a modified version:
#include <stdio.h>
int main() {
int num, new_num = 0, pow10 = 1;
printf("Enter a number: ");
if (scanf("%d", &num) != 1)
return 1;
while (num != 0) {
int digit = num % 10;
/* decrement odd digits absolute value */
digit -= digit % 2;
new_num = new_num + digit * pow10;
pow10 = pow10 * 10;
num = num / 10;
}
printf("The new number is: %d\n", new_num);
return 0;
}
Note that digit -= digit % 2; will decrement positive odd digits and actually increment negative odd digits, which effectively always decrements the absolute value of odd digits. This way both positive values and negative values are handled correctly.
In my instance of Visual Studio Code, for an input 23145, I indeed find 22044 as an output.
I guess the divergence comes with the cast of pow(10,i). Pow function in C returns a double which is not what you really want here. I strongly advice to not use the pow function for integer arithmetic.
A solution could be :
uint16_t i = 0u;
uint16_t current_digit = 0u, decimal_digit = 1u;
uint16_t new_number = 0u;
uint16_t number = 23145u;
while(number > 0u) {
current_digit = number % 10;
if (current_digit % 2) {
current_digit = current_digit - 1;
}
new_number = new_number + current_digit * decimal_digit;
decimal_digit *= 10u;
number /= 10;
i++;
}
printf("The new number is: %d", new_number);
It seems that the problem is using the function pow that returns a double value.
If you are dealing with integers then it is better to avoid using functions that return doubles due to a possible truncation then a double is converted to an integer.
Also pay attention to that the user can enter a negative number. Your program allows to do that. In this case your program also will produce an incorrect result.
I would write the program the following way
#include <stdio.h>
int main( void )
{
while (1)
{
const int Base = 10;
int num;
printf( "Enter a number (0 - exit): " );
if (scanf( "%d", &num ) != 1 || num == 0) break;
int new_num = 0;
for (int tmp = num, multiplier = 1; tmp != 0; tmp /= Base)
{
int digit = tmp % Base;
if (digit % 2 != 0)
{
digit += ( digit < 0 ? 1 : -1 );
}
new_num = new_num + multiplier * digit;
multiplier *= Base;
}
printf( "The original number is %d and the new number is: %d\n",
num, new_num );
putchar( '\n' );
}
}
The program output is
Enter a number (0 - exit): -123456789
The original number is -123456789 and the new number is: -22446688
Enter a number (0 - exit): 123456789
The original number is 123456789 and the new number is: 22446688
Enter a number (0 - exit): 0
If even for negative digits to add -1 then you should substitute this if statement
if (digit % 2 != 0)
{
digit += ( digit < 0 ? 1 : -1 );
}
for this one
if (digit % 2 != 0)
{
digit = ( digit -1 ) % Base;
}
In this case the program output might look like
Enter a number (0 - exit): -123456789
The original number is -123456789 and the new number is: -224466880
Enter a number (0 - exit): 123456789
The original number is 123456789 and the new number is: 22446688
Enter a number (0 - exit): 0
That is in this case the new value for the negative value -123456789 will be -224466880.
Problem statement :
Given a 32-bit signed integer, reverse digits of an integer.
Note: Assume we are dealing with an environment that could only store
integers within the 32-bit signed integer range: [ −2^31, 2^31 − 1]. For
the purpose of this problem, assume that your function returns 0 when
the reversed integer overflows.
I'm trying to implement the recursive function reverseRec(), It's working for smaller values but it's a mess for the edge cases.
int reverseRec(int x)
{
if(abs(x)<=9)
{
return x;
}
else
{
return reverseRec(x/10) + ((x%10)*(pow(10, (floor(log10(abs(x)))))));
}
}
I've implemented non recursive function which is working just fine :
int reverse(int x)
{
long long val = 0;
do{
val = val*10 + (x%10);
x /= 10;
}while(x);
return (val < INT_MIN || val > INT_MAX) ? 0 : val;
}
Here I use variable val of long long type to check the result with MAX and MIN of signed int type but the description of the problem specifically mentioned that we need to deal within the range of 32-bit integer, although somehow it got accepted but I'm just curious If there is a way to implement a recursive function using only int datatype ?
One more thing even if I consider using long long I'm failing to implement it in the recursive function reverseRec().
If there is a way to implement a recursive function using only int datatype ?
(and) returns 0 when the reversed integer overflows
Yes.
For such +/- problems, I like to fold the int values to one side and negate as needed. The folding to one side (- or +) simplifies overflow detection as only a single side needs testing
I prefer folding to the negative side as there are more negatives, than positives. (With 32-bit int, really didn't make any difference for this problem.)
As code forms the reversed value, test if the following r * 10 + least_digit may overflow before doing it.
An int only recursive solution to reverse an int. Overflow returns 0.
#include <limits.h>
#include <stdio.h>
static int reverse_recurse(int i, int r) {
if (i) {
int least_digit = i % 10;
if (r <= INT_MIN / 10 && (r < INT_MIN / 10 || least_digit < INT_MIN % 10)) {
return 1; /// Overflow indication
}
r = reverse_recurse(i / 10, r * 10 + least_digit);
}
return r;
}
// Reverse an int, overflow returns 0
int reverse_int(int i) {
// Proceed with negative values, they have more range than + side
int r = reverse_recurse(i > 0 ? -i : i, 0);
if (r > 0) {
return 0;
}
if (i > 0) {
if (r < -INT_MAX) {
return 0;
}
r = -r;
}
return r;
}
Test
int main(void) {
int t[] = {0, 1, 42, 1234567890, 1234567892, INT_MAX, INT_MIN};
for (unsigned i = 0; i < sizeof t / sizeof t[0]; i++) {
printf("%11d %11d\n", t[i], reverse_int(t[i]));
if (t[i] != INT_MIN) {
printf("%11d %11d\n", -t[i], reverse_int(-t[i]));
}
}
}
Output
0 0
0 0
1 1
-1 -1
42 24
-42 -24
1234567890 987654321
-1234567890 -987654321
1234567892 0
-1234567892 0
2147483647 0
-2147483647 0
-2147483648 0
You could add a second parameter:
int reverseRec(int x, int reversed)
{
if(x == 0)
{
return reversed;
}
else
{
return reverseRec(x/10, reversed * 10 + x%10);
}
}
And call the function passing the 0 for the second parameter. If you want negative numbers you can check the sign before and pass the absolute value to this function.
In trying to learn C programming I programed this question and get some correct results and some incorrect. I don't see the reason for the difference.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h> // requires adding link to math -lm as in: gcc b.c -lm -o q11
int ReverseInt(int startValue, int decimalPlace)
{
if(decimalPlace == 0) // if done returns value
{
return startValue;
}
int temp = startValue % 10; // gets units digit
int newStart = (startValue -temp)/10; // computes new starting value after removing one digit
int newDecimal = decimalPlace -1;
int value = temp*pow(10,decimalPlace);
return value + ReverseInt(newStart,newDecimal); // calls itself recursively until done
}
int main()
{
int x, decimalP, startValue;
printf("Input number to be reversed \n Please note number must be less than 214748364 :");
scanf("%d", &x);
if (x > 214748364)
{
printf("Input number to be reversed \n Please note number must be less than 214748364 :");
scanf("%d", &x);
}
decimalP = round(log10(x)); // computes the number of powers of 10 - 0 being units etc.
startValue = ReverseInt(x, decimalP); // calls function with number to be reversed and powers of 10
printf("\n reverse of %d is %d \n", x, startValue);
}
Output is: reverse of 1234 is 4321 but then reverse of 4321 is 12340
It's late and nothing better does not come into my mind. No float calculations. Of course, integer has to be big enough to accommodate the result. Otherwise it is an UB.
int rev(int x, int partial, int *max)
{
int result;
if(x / partial < 10 && (int)(x / partial) > -10)
{
*max = partial;
return abs(x % 10) * partial;
}
result = rev(x, partial * 10, max) + abs(((x / (int)(*max / partial)) % 10) * partial);
return result;
}
int reverse(int x)
{
int max;
return rev(x, 1, &max) * ((x < 0) ? -1 : 1);
}
int main(void){
printf("%d", reverse(-456789));
}
https://godbolt.org/z/M1eezf
unsigned rev(unsigned x, unsigned partial, unsigned *max)
{
unsigned result;
if(x / partial < 10)
{
*max = partial;
return (x % 10) * partial;
}
result = rev(x, partial * 10, max) + (x / (*max / partial) % 10) * partial;
return result;
}
unsigned reverse(unsigned x)
{
unsigned max;
return rev(x, 1, &max);
}
int main(void){
printf("%u", reverse(123456));
}
when using long long to store the result all possible integers can be reversed
long long rev(int x, long long partial, long long *max)
{
long long result;
if(x / partial < 10 && (int)(x / partial) > -10)
{
*max = partial;
return abs(x % 10) * partial;
}
result = rev(x, partial * 10, max) + abs(((x / (int)(*max / partial)) % 10) * partial);
return result;
}
long long reverse(int x)
{
long long max;
return rev(x, 1, &max) * ((x < 0) ? -1 : 1);
}
int main(void){
printf("%d reversed %lld\n", INT_MIN, reverse(INT_MIN));
printf("%d reversed %lld\n", INT_MAX, reverse(INT_MAX));
}
https://godbolt.org/z/KMfbxz
I am assuming by reversing an integer you mean turning 129 to 921 or 120 to 21.
You need an initial method to initialize your recursive function.
Your recursive function must figure out how many decimal places your integer uses. This can be found by using log base 10 with the value and then converting the result to a integer.
log10 (103) approx. 2.04 => 2
Modulus the initial value by 10 to get the ones place and store it in a variable called temp
Subtract the ones place from the initial value and store that in a variable called newStart.
divide this value by 10
Subtract one from the decimal place and store in another variable called newDecimal.
Return the ones place times 10 to the power of the decimal place and add it to the function where the initial value is newStart and the decimalPlace is newDecimal.
#include <stdio.h>
#include <math.h>
int ReverseInt(int startValue, int decimalPlace);
int main()
{
int i = -54;
int positive = i < 0? i*-1 : i;
double d = log10(positive);
int output = ReverseInt(positive,(int)d);
int correctedOutput = i < 0? output*-1 : output;
printf("%d \n",correctedOutput);
return 0;
}
int ReverseInt(int startValue, int decimalPlace)
{
if(decimalPlace == 0)
{
return startValue;
}
int temp = startValue % 10;
int newStart = (startValue -temp)/10;
int newDecimal = decimalPlace -1;
int value = temp*pow(10,decimalPlace);
return value + ReverseInt(newStart,newDecimal);
}
I am solving reverse integer problem in leetcode in c language.But it gives runtime error on line sum=sum+rem*10;.
runtime error: signed integer overflow: 964632435 * 10 cannot be represented in type 'int'
Here is the code.
#define INT_MAX 2147483647
#define INT_MIN -2147483648
int reverse(int x){
int sum=0,rem=0;
int p;
if(x > INT_MAX || x < INT_MIN){return 0;}
if(x==0){return 0;}
if(x<0){p=x;x=abs(x);}
while(x%10==0){x=x/10;}
while(x>0){
rem=x%10;
if(sum > INT_MAX || sum*(-1) < INT_MIN){return 0;}
sum=sum*10+rem;
x/=10;
}
if(p<0){sum=sum*(-1);return sum;}
else{return sum;}
}
One way - which isn't performance optimal but simple - is to convert the integer to a string and then revert the string and then convert back to integer.
The below solution is for positive integers - I'll leave it to OP to extend it to handle negative integers.
Could look like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int reverse(const int n)
{
if (n < 0)
{
printf("Handling of negative integers must be added\n");
exit(1);
}
char tmp1[100];
char tmp2[100] = { 0 };
sprintf(tmp1, "%d", n);
printf("Input : %s\n", tmp1);
size_t sz = strlen(tmp1);
for (size_t i = 0; i < sz; ++i)
{
tmp2[i] = tmp1[sz-i-1];
}
int result = tmp2[0] - '0';
char* p = tmp2+1;
while(*p)
{
if ((INT_MAX / 10) < result)
{
printf("oh dear: %d can't be reversed to int\n", n);
exit(1);
}
result = result * 10;
if ((INT_MAX - (*p - '0')) < result)
{
printf("oh dear: %d can't be reversed to int\n", n);
exit(1);
}
result += *p - '0';
p++;
}
return result;
}
int main()
{
printf("Output: %d\n", reverse(123));
printf("Output: %d\n", reverse(123456789));
printf("Output: %d\n", reverse(1234567899));
return 0;
}
Output:
Input : 123
Output: 321
Input : 123456789
Output: 987654321
Input : 1234567899
oh dear: 1234567899 can't be reversed to int
But it gives runtime error on line sum=sum+rem*10;.
To test for potential int overflow of positive sum, rem, compare against INT_MAX/10 and INT_MAX%10 beforehand.
if (sum >= INT_MAX / 10 && (sum > INT_MAX / 10 || rem > INT_MAX % 10)) {
// overflow
} else {
sum = sum * 10 + rem;
}
Handling negatives
Watch out for x = INT_MIN ... x = -x;. That is int overflow and undefined behavior.
Sometimes it is fun to solve such int problems of positive and negative numbers by converting the positive numbers to negative ones embrace the dark side - its your only hope. (maniacal laughter)
There are more int values less than zero than there are int values more than zero - by one. So x = -x is always well defined when x > 0.
// C99 or later code
#include <limits.h>
int reverse(int x) {
int x0 = x;
if (x0 > 0) {
x = -x; // make positive values negative, embrace the dark side
}
int reversed = 0;
while (x < 0) {
int rem = x % 10;
x /= 10;
if (reversed <= INT_MIN / 10
&& (reversed < INT_MIN / 10 || rem < INT_MIN % 10)) {
// overflow
return 0;
}
reversed = reversed * 10 + rem;
}
if (x0 > 0) {
if (reversed < -INT_MAX) {
// overflow
return 0;
}
reversed = -reversed;
}
return reversed;
}
Test code
#include <stdio.h>
int main() {
int x[] = {0, 1, -1, 42, 123456789, INT_MAX/10*10+1, INT_MIN/10*10-1,INT_MAX, INT_MIN};
size_t n = sizeof x / sizeof x[0];
for (size_t i = 0; i < n; i++) {
printf("Attempting to reverse %11d ", x[i]);
printf("Result %11d\n", reverse(x[i]));
}
}
Sample output
Attempting to reverse 0 Result 0
Attempting to reverse 1 Result 1
Attempting to reverse -1 Result -1
Attempting to reverse 42 Result 24
Attempting to reverse 123456789 Result 987654321
Attempting to reverse 2147483641 Result 1463847412
Attempting to reverse -2147483641 Result -1463847412
Attempting to reverse 2147483647 Result 0
Attempting to reverse -2147483648 Result 0
First, you should not be defining your own values for INT_MAX and INT_MIN. You should instead #include <limits.h> which defines these value.
Second, this:
sum > INT_MAX
Will never be true because sum can never hold a value larger than INT_MAX. So you can't perform an operation and then check afterward if it overflowed. What you can do instead is check the operation first and do some algebra that prevents overflow.
if ( INT_MAX / 10 < sum) return 0;
sum *= 10;
if ( INT_MAX - rem < sum) return 0;
sum += rem;
I have a function print_number.
The function checks if in front of the number there exists '-', then it reverse the number and takes every digit and prints it. The algorithm works pretty good but if i give -2.147.483.648 ( which should be the bottom limit of an integer ) it pritns -0 and i don't know why.
#include<stdio.h>
void print_char(char character)
{
printf("%c",character);
}
void print_number(int nr)
{
int reverse=0;
if (nr < 0)
{
print_char('-');
nr *= -1;
}
while(nr > 9)
{
reverse = reverse * 10 + nr % 10;
nr = nr / 10;
}
print_char(nr + '0');
while(reverse)
{
print_char(reverse % 10 + '0');
reverse = reverse / 10;
}
}
When you are doing
if (nr < 0)
{
print_char('-');
nr *= -1;
}
It inverses negative number to the positive one.
If you will run it for -2.147.483.648, you will receive
nr = 2.147.483.648 // == binary 1 0000000000000000000000000000000
As INT is 32 BIT variable in your architecture (and at least 16 BIT variable by the spec), so '1' overflows it and so on
nr = 0 // For gcc-like C realisation
And accepting the ISO9899 spec, this behaviour of signed int overflow is realisation-specified thing and may not be predicted in common.
Use long long value if you're needing to use your program for larger values.
Something like:
#include<stdio.h>
void print_char(char character)
{
printf("%c",character);
}
void print_number(long long nr)
{
int reverse=0;
if (nr < 0)
{
print_char('-');
nr *= -1;
}
while(nr > 9)
{
reverse = reverse * 10 + nr % 10;
nr = nr / 10;
}
print_char(nr + '0');
while(reverse)
{
print_char(reverse % 10 + '0');
reverse = reverse / 10;
}
}
void main(void){
print_number(-2147483648LL);
}
And test:
> gcc test.c
> ./a.out
-2147483648
Firstly, the MAX and MIN range for an INT are -2,147,483,648 and 2,147,483,647 respectively.
Negating -2,147,483,648 means a positive value 2,147,483,648 would result in an overflow by 1 as it is out of bounds for the MAX range.
This operation will result in the same value of -2,147,483,648.
Secondly, you might encounter an overflow during the integer reversing process.
Example, reversing 2147483647 causes an overflow after the intermediate result of 746384741.
Therefore, you should handle that by throwing an exception or returning 0.
Thirdly, your loop for reversing the number is inaccurate. It should loop till while(nr != 0)
Here's the complete code.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
void reverseNumber(int);
reverseNumber(124249732);
return 0;
}
void reverseNumber(int nr)
{
printf("nr = %d\n", nr);
int reverse = 0;
bool neg = false;
if (nr < 0) {
neg = true;
nr *= -1;
}
while (nr != 0) {
int digit = nr % 10;
int result = reverse * 10 + digit;
if ((result - digit) / 10 != reverse) {
printf("ERROR\n");
exit(0);
}
reverse = result;
nr = nr / 10;
}
if(neg) {
printf("%c", '-');
}
printf("%d\n", reverse);
}
nr *= -1; is a problme when nr == INT_MIN as that is signed integer overflow. The result is undefined behavior (UB). Best to avoid.
Wider integers are not always available.
Using OP's general, approach, do not change the sign of nr until it is reduced.
void print_number(int nr) {
int reverse = 0;
if (nr < 0) {
print_char('-');
//nr *= -1;
}
while (nr/10) { // new test
reverse = reverse * 10 + nr % 10;
nr = nr / 10;
}
reverse = abs(reverse); // reverse = |reverse|
nr = abs(nr); // nr = |nr|
print_char(nr + '0');
while (reverse) {
print_char(reverse % 10 + '0');
reverse = reverse / 10;
}
}
I have a long int number which is in binary form.
I want to subtract or add another binary number from this number keeping the data type of destination variable same, i.e long int
How can I do it?
I had a solution in which destination variable was an array of int.
what I tried so far
int main() {
long int binary1;
long int binary2 = 0001;
int i = 0, rem = 0;
int sum[20];
printf("Enter binary number");
scanf("%ld", &binary1);
while (binary1 != 0 || binary2 != 0) {
sum[i++] =(binary1 % 10 + binary2 % 10 + rem) % 2;
rem =(binary1 % 10 + binary2 % 10 + rem) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
return 0;
}
Here the result is stored as int. I want to store it in long int because I need to use this sum for the next addition operation.
How can I do the next addition operation where sum is one of the operand? sum here is in int, and another operand is in long int.
Store as chars it what you want to do I think
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
int main() {
long int binary1;
long int binary2 = 0001;
int i = 0, rem = 0;
char sum[20];
printf("Enter binary number: ");
scanf("%ld", &binary1);
while (binary1 != 0 || binary2 != 0) {
sum[i++]=(binary1 % 10 + binary2 % 10 + rem) % 2 > 0 ? '1' : '0';
rem =(binary1 % 10 + binary2 % 10 + rem) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%c", sum[i--]);
printf("\n");
return 0;
}
your problem is that
long int binary1=1011;
will load your variable with 1011 dec which is 1111110011 bin you can use standard addition operator but different load/print operations. If you want to stick to your DCB (decimal coded binary) encoding (similar to BCD binary coded decimal) then you need to rewrite all math operation. I think simpler would be this:
long int bin2dec(long int bin) // DCB -> binary
{
long int x,m;
for (m=1;m<bin;m*=10);
for (x=0;m;m/=10)
{
x<<=1;
x+=bin/m;
bin%=m;
}
return x;
}
long int dec2bin(long int dec) // binary -> DCB
{
long int x,m;
for (m=1;m<dec;m<<=1);
for (x=0;m;m>>=1)
{
x*=10;
x+=dec/m;
dec&=m-1;
}
return x;
}
void main()
{
long int b1=1011;
long int b2= 1;
long int b3= 100;
long int sum;
sum = dec2bin(bin2dec(b1)+bin2dec(b2)+bin2dec(b3));
// here print the sum or whatever
}
The point is to convert DCB into standard binary representation before math operations and then convert the result back to DCB for printing.