Thread: adding a number to a number

1. adding a number to a number

this is exactly what im trying to do, and its probably an easy question to solve. Say i have a number in a variable (ie. 1) and i want to add a 0 to the one (ie. 10) how would i go by doing this?

I have to make a decimal to binary converter, so id need to now how to add a number to a number variable.

2. Originally Posted by bigmac(rexdale)
this is exactly what im trying to do, and its probably an easy question to solve. Say i have a number in a variable (ie. 1) and i want to add a 0 to the one (ie. 10) how would i go by doing this?

I have to make a decimal to binary converter, so id need to now how to add a number to a number variable.
Try to be more specific in your terminology. "Adding numbers" is accomplished with the addition operator "+". What you are trying to do is append a digit.

Your approach seems to be to hold the binary "value" as an integer whose decimal representation consists only of 0 and 1. That is definitely not going to work, because integer values have limited range and you are wasting most of it by working in the wrong base.

Numbers never have to be converted to/from specific bases while internally represented. A value is a value, not a sequence of digits. The digits come into being when the value is represented in generated output.

3. OK, well this is what I've been thinking of doing.

The program is Integer to Binary converter

I use the integer number to be converted, in a function that does something like this:

for loop(starting at 32, and descending, called counter)
number = number - 2^counter(number is the number the user wants to be outputted to look like the binary equivalent, counter would be 32, then 31 ...)

a if statement, where if number is less than 0, i add back what i substracted and the first digit of the binary output would be 0 (the variable name is b)

else if, number is more than 0 or eqal to, the first digit of the binary output would be 1(the variable name is b)

binary(a variable) would equal b

is if the first line is true, it would equal 0, if not, then 1, but:

then it does a next loop with counter being 31, so, how would the next digit (either a 0 or a 1) be appended? to what the digit was before.

ie. if it was 1 when counter equaled 32, when it equals 31 and the answer is one again, i need the binary variable to equal '11'

4. I'm not sure if I understand you correctly, but if you want to convert int to displayable binary, one quick approach would be to store remaining parts of division (which is either 1 or 0) in char array, eg.

Code:
```int i = 0;
while ( num > 0 ) { // num == int that'd be converted to binary
if ( num &#37; 2 == 0 )
charArray[i] = '0';
else
charArray[i] = '1';
num /= 2;
i++;
}```
...and then simply reverse array and print output.

5. i didn't learn that yet though, should've mention im a beginner

basically, im looking for a way to combine two numbers, i want to combine a int variable that is 2, with a next variable that is 5 to make the variable now equal 25

6. Originally Posted by bigmac(rexdale)
i didn't learn that yet though, should've mention im a beginner

basically, im looking for a way to combine two numbers, i want to combine a int variable that is 2, with a next variable that is 5 to make the variable now equal 25
And what exactly does that have to do with the binary numeral system?

Using some simple arithmetic can accomplish what you want.

25 = 2 * 10 + 5 * 1.

So, basically..

int a = 2, b = 5;

a *= 10;
a += b*1;

Multiplying by one is pointless, but it's to clarify the concept.

7. ok, so i changed it up, since it is a pointless code

Code:
```#include<stdio.h>
int binar(int number, int *pbinary)
{
*pbinary = number &#37; 2;
return number / 2;
}

main () {
int number, binary, bin;
printf("number ");
scanf("%d", &number);
binary = binar(number, &bin);
printf("%d %d", binary, bin);
}```
i that that so far....I think i need to add an array and a for loop to proceed to get the rest of the binary digits

edit:

Code:
```#include<stdio.h>
int binar(int number, int *pbinary)
{
*pbinary = number % 2;
return number / 2;
}

main () {
int number, binary, bin, bina[32], counter;
printf("number ");
scanf("%d", &number);
while (number > 0 || number < 2147483647) {
counter = counter + 1;
binary = binar(number, &bin);
bina[counter] = bin;
}
printf("%d %d", bina[1]);
}```
i now have that, but its not displaying any text

8. Okay, maybe I could help here:

Code:
```int main (void)  // see FAQ entry here
{
int number, bin, counter = 0;  // you're starting accessing array at first index, which is 0
char bina[32];  // storing array was supposed to be char type, so it can be displayed as a string
printf("number ");
scanf("%d", &number);
while (number > 0 || number < 2147483647) // second condition isn't really needed
{
number = binar(number, &bin);  // you need to modify original number that's checked in while loop
bina[counter] = bin + '0';  // you don't want to put 1 or 0 into array, but it's character equivalent in ascii code
counter = counter + 1; // increment counter once you're done with it's current value, not before
}
bina[counter] = 0;  // c-string should end with 0
printf("%d %s", number, bina);
}```
Don't forget to reverse array before final displaying (because what you got now will display from bits from end - to - begining) - also watch out for terminating 0 when reversing, you don't want it at the very begining of your string.
Hope it's more clear now.

9. i fixed it up and reversed the arrays (i have a differen code from the last time though)

Code:
```#include<stdio.h>
int binar(int number, int *pbinary)
{
*pbinary = number / 2;
return number &#37; 2;
}

main () {
int binary, bin, bina[32], counter;
int number, number2, numcheck;
printf("number ");
scanf("%d", &number);

while (number < 0 || number > 2147483647) {
printf("Enter Valid Number\n");
printf("number ");
scanf("%d", &number);
}
number2 = number;
binary = binar(number, &bin);
bina[1] = binary;
number = bin;
bina[2] = binar(number, &bin);
number = bin;
bina[3] = binar(number, &bin);
number = bin;
bina[4] = binar(number, &bin);
number = bin;
bina[5] = binar(number, &bin);
number = bin;
bina[6] = binar(number, &bin);
number = bin;
bina[7] = binar(number, &bin);
number = bin;
bina[8] = binar(number, &bin);
number = bin;
bina[9] = binar(number, &bin);
number = bin;
bina[10] = binar(number, &bin);
number = bin;
bina[11] = binar(number, &bin);
number = bin;
bina[12] = binar(number, &bin);
number = bin;
bina[13] = binar(number, &bin);
number = bin;
bina[14] = binar(number, &bin);
number = bin;
bina[15] = binar(number, &bin);
number = bin;
bina[16] = binar(number, &bin);
number = bin;
bina[17] = binar(number, &bin);
number = bin;
bina[18] = binar(number, &bin);
number = bin;
bina[19] = binar(number, &bin);
number = bin;
bina[20] = binar(number, &bin);
number = bin;
bina[21] = binar(number, &bin);
number = bin;
bina[22] = binar(number, &bin);
number = bin;
bina[23] = binar(number, &bin);
number = bin;
bina[24] = binar(number, &bin);
number = bin;
bina[25] = binar(number, &bin);
number = bin;
bina[26] = binar(number, &bin);
number = bin;
bina[27] = binar(number, &bin);
number = bin;
bina[28] = binar(number, &bin);
number = bin;
bina[29] = binar(number, &bin);
number = bin;
bina[30] = binar(number, &bin);
number = bin;
bina[31] = binar(number, &bin);
number = bin;
bina[32] = binar(number, &bin);
printf("Integer:         32-bit binary:\n");
printf("----------       --------------\n");
printf("%10d%8d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d",
number2, bina[32], bina[31], bina[30], bina[29], bina[28], bina[27], bina[26], bina[25], bina[24], bina[23],
bina[22], bina[21], bina[20], bina[19], bina[18], bina[17], bina[16], bina[15], bina[14], bina[13],
bina[12], bina[11], bina[10], bina[9], bina[8], bina[7], bina[6], bina[5], bina[4], bina[3], bina[2], bina[1]);

}```
edit: no problems i fixed it

and a reply aobut the line of code:
Code:
`while (number > 0 || number < 2147483647)`
the number has to be between 0 to 2147483647

I just have a question now, i wrote so many lines of the same code (the arrays), would a for loop work so it wouldn't be so many lines of the same code

10. the number has to be between 0 to 2147483647
Yes, but an int can't hold a number larger than 2147483647. So checking for it is redundant. It's like saying, if the number is less than or equal to its largest possible number. It will always be true.

I just have a question now, i wrote so many lines of the same code (the arrays), would a for loop work so it wouldn't be so many lines of the same code
Yes, definitely. I was going to suggest it myself. Use a for loop with something like
Code:
```     binary = binar(number, &bin);
bina[x] = binary;```
in the body, with x looping from 0 to 31, inclusive (x=0, x<32). You have from 1 to 32 inclusive, but that's causing a buffer overrun.

 I'd use a for loop for that last printf as well. [/edit]

11. ok so,

Code:
```for (x = 0; x<32;) {
binary = binar(number, &bin);
bina[x] = binary;
}```
would that do it?

and for the for loop for the last printf, i have to make it also line up with the printf above it, so how would that be done?

edit: i got it now, but for the bin[32] array it gives me a big number that i dont know what it stands for, so i multiplied it by 0 since it will always equal 0

Code:
```#include<stdio.h>
int binar(long number, int *pbinary)
{
*pbinary = number / 2;
return number &#37; 2;
}

main () {
int binary, bin, bina[32], x;
long number, number2;
printf("number ");
scanf("%d", &number);

while (number < 0 || number > 2147483647) {
printf("Enter Valid Number\n");
printf("number ");
scanf("%d", &number);
}
number2 = number;
binary = binar(number, &bin);
bina[1] = binary;
number = bin;
for (x = 2; x < 32; x = x + 1) {
bina[x] = binar(number, &bin);
number = bin;
}
printf("Integer:         32-bit binary:\n");
printf("----------       --------------\n");
printf("%10d%8d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d %d%d%d%d%d%d%d%d"
, number2, bina[32]*0, bina[31], bina[30], bina[29], bina[28], bina[27], bina[26], bina[25], bina[24], bina[23],
bina[22], bina[21], bina[20], bina[19], bina[18], bina[17], bina[16], bina[15], bina[14], bina[13], bina[12],
bina[11], bina[10], bina[9], bina[8], bina[7], bina[6], bina[5], bina[4], bina[3], bina[2], bina[1]);```

12. Code:
`x = x + 1`
is the same as
Code:
`x += 1`
or
Code:
`x ++`
It's common practise to use x++. You don't have to, of course, but it's shorter and arguably more readable.

edit: i got it now, but for the bin[32] array it gives me a big number that i dont know what it stands for, so i multiplied it by 0 since it will always equal 0
That's because bina[32] doesn't exist.

When you declare an array like this:
Code:
`type array[N];`
you get N elements. Except they start counting from zero. So if N=3, you get 0, 1, and 2 as elements. That's still 3 elements, but the last element is N-1. So if you declare an array as
Code:
`int bina[32];`
then your elements are 0, 1, 2, ... 31. Not 1, 2, 3, ... 32.

I'm sure you could work out how to convert the last printf() into a loop. It would be a version of this:
Code:
```for(...) {
printf("%d", num[x]);
}```

Popular pages Recent additions