I am one week into a beginning C programming course and so far we have reached if statements, and nothing further (no while loops, just basic defining of variables, using relational operators, the if statements, etc.) So I need some help fixing my BASIC coding ...

Basically, the program needs to ask the user to input an integer, convert that integer to binary but only for the last 4 bits, and print those 4 bits. Then, it has to do this AGAIN for a second integer given by the user. (There is more to this program but this is the first part, and I need to fix it).

Basically, my code works for bigger integers that are already 4 or more bits in length when converted to binary. BUT integers that have binary lengths of LESS than 4 bits are printing wrong, because instead of 0's put to the left of their value to equal 4 bits of length, my code prints 1's. I'm using multiple if statements, one for each bit, and I need each bit to be given its own variable because I will be using these variables for something else later on.

Anyway, here is my code:

So how do I fix this so that values less than 4 bits add the appropriate # of 0's to the left to make it '4' bits? Make it all one if statement? If so, I'm not exactly 100% clear on the rules of elseif, so please be very specific, or even modify the code itself for me.Code:#include <stdio.h> /** Include the stdio.h file to allow for printf() to be used*/ /** Project 1 for CS151 by NS */ int main (void) { int x; // This is the FIRST integer that the user enters. int y; // This is the SECOND integer that the user enters. int newx; // This is the ever-changing value of x as we break it down to find the zero or one remainders. int newy; // This is the ever-changing value of x as we break it down to find the zero or one remainders. int a1, b1, c1, d1; // These are the values of the last 4 bits of the FIRST integer given by the user. Currently they are in reverse order. int a2, b2, c2, d2; // These are the values of the last 4 bits of the SECOND integer given by the user. Currently they are in reverse order. puts("Please enter an integer. (Integers are whole numbers - numbers without fractions or decimals, so please take note.): \n"); scanf("%i", &x); /** Convert the FIRST integer (x) into binary, printing only the last four bits. */ if (x/2 >= 1) { a1 = x%2; newx = x/2; } else { a1 = 1; } if (newx/2 >= 1) { b1 = newx%2; newx = newx/2; } else { b1 = 1; } if (newx/2 >= 1) { c1 = newx%2; newx = newx/2; } else { c1 = 1; } if (newx/2 >= 1) { d1 = newx%2; newx = newx/2; } else { d1 = 1; } /** Print the results of the decimal to binary conversion (last four bits only): */ printf("The last four bits of %i are %d%d%d%d. \n", x, d1, c1, b1, a1); puts("Now, please enter a second integer. \n"); scanf("%i", &y); /** Convert the SECOND integer (y) into binary, printing only the last four bits. */ if (y/2 >= 1) { a2 = y%2; newy = y/2; } else { a2 = 1; } if (newy/2 >= 1) { b2 = newy%2; newy = newy/2; } else { b2 = 1; } if (newy/2 >= 1) { c2 = newy%2; newy = newy/2; } else { c2 = 1; } if (newy/2 >= 1) { d2 = newy%2; newy = newy/2; } else { d2 = 1; } printf("The last four bits of %i are %d%d%d%d. \n", y, d2, c2, b2, a2); return (0); }

Also, I realize there is more than one way to go about this. One option someone suggested to me (without expanding on) was to go about it by seeing if x can subtract 8, then 4, then 2, then 1, etc. Our homework hinted that this was a possibility, but it also suggested the method I used, which is utilizing / and % operators. So if you think one method or the other is better, please be SPECIFIC in how to implement them!

Also, this assignment is due at midnight tonight (Pacific time), so please respond ASAP!! thank you!!!