Using LC3 Editor. This program accepts an integer typed in by the user, verifies
ID: 3715043 • Letter: U
Question
Using LC3 Editor. This program accepts an integer typed in by the user, verifies that the number is valid, and if it is valid prints the binary version of the number to the display as on the following page. Call the program part2.asm. As you can see the program rejects any input which doesn't start with a + or - sign and prints "The input is invalid." to the display. Also any integer values less than -511 or greater than +511 are rejected. Values like +0123 are acceptable. When a value is rejected the program loops back and asks for a new number. The program stops after printing out the binary representation of the entered number, with a space between each pair of bits.
Explanation / Answer
#include <stdio.h>
int binary_conversion(int);
int main()
{
int num, bin;
printf("Enter a number: ");
scanf("%d", &num);
If (num>=-511 && num<=+511)
bin = binary_conversion(num);
else
{
printf("Number is invalid enter the number again : ");
printf("Enter a number: ");
scanf("%d", &num);
}
printf("The binary equivalent of %d is %d ", num, bin);
}
int binary_conversion(int num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary_conversion(num / 2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.