Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that: Prompt the user to enter an integer number n from 0 to 6

ID: 3879354 • Letter: W

Question

Write a C program that:

Prompt the user to enter an integer number n from 0 to 65535

Prints out in binary and hexa the number n

Prints out in binary and hexa the ~n

Prints out in binary the 4 least significant bits (LSB) of the number n

Prints out the number n in hexadecimal, but in a customized format: the hexa digits must be separated by a dash (‘-‘)

Prints out in decimal the number of ones (‘1’) in the binary representation of the number n.

Prompts the user to enter a number m from 0 to 15, clears the mth bit of the number n. The 0th bit is the LSB.

Explanation / Answer

#include <stdio.h>
#include <math.h>
#include<string.h>
int main()
{
int m, res, number;
long int dec, rem, out = 0, n, bin, count = 0, base = 1, quot;
char hex[100];
int i = 1, j, hexa;
printf(" Enter numbers from 0 to 65535: ");
scanf("%ld", &n);
printf(" Enter the number (0 - 15): ");
scanf("%d", &m);
dec = n;
while(n > 0)
{
bin = n % 2;
if(bin == 1)
{
count++;
}
out = out + bin * base;
n = n / 2;
base = base * 10;
}
printf(" Integer number is: %d", dec);
printf(" Binary representation of %d is %ld", dec, out);
printf(" Number of 1's is : %ld", count);
while(dec!=0)
{
hexa = dec % 16;
if(hexa < 10)
hexa = 48 + hexa;
else
{
hexa = 55 + hexa;
i++;
}
hex[i++] = hexa;
dec = dec / 16;
}
printf(" Hexadecimal representation of %d is:", dec);
for(j=i; j>=0; j--)
printf("%c", hex[j]);
  
res = dec & (~(1 << m));
printf(" Number before clearing of %d is: %ld", m, dec);
printf(" Number after clearing of %d is: %d", m, res);
return 0;
}

OUTPUT


Enter numbers from 0 to 65535: 2454
Enter the number (0 - 15): 0
Integer number is: 2454
Binary representation of 2454 is 100110010110
Number of 1's is : 6
Hexadecimal representation of 0 is:

-----------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
#include<string.h>
int main()
{
long dec, out = 0, n, bin, count = 0, base = 1, quot;
printf(" Enter numbers from 0 to 65535: ");
scanf("%ld", &n);
dec = n;
while(n > 0)
{
bin = n % 2;
if(bin == 1)
{
count++;
}
out = out + bin * base;
n = n / 2;
base = base * 10;
}
printf(" Integer number is: %ld", dec);
printf(" Binary representation of %ld is %ld", dec, out);
printf(" Number of 1's is : %ld", count);
return 0;
}

OUTPUT

Enter numbers from 0 to 65535: 134
Integer number is: 134
Binary representation of 134 is 10000110
Number of 1's is : 3

#include <stdio.h>
#include <math.h>
#include<string.h>
int main()
{
  
char hex[100];
long int rem, n;
int i = 1, j, hexa;
printf(" Enter numbers from 0 to 65535: ");
scanf("%ld", &n);
long int dec = n;
while(dec!=0)
{
hexa = dec % 16;
if(hexa < 10)
hexa = 48 + hexa;
else
{
hexa = 55 + hexa;
i++;
}
hex[i++] = hexa;
dec = dec / 16;
}
printf(" Hexadecimal representation of %ld is:", dec);
for(j=i; j>=0; j--)
printf("%c", hex[j]);
return 0;
}

OUTPUT
Enter numbers from 0 to 65535: 134
Hexadecimal representation of 0 is:86

#include <stdio.h>
#include <math.h>
#include<string.h>
int main()
{
int m, res, n;
printf(" Enter numbers from 0 to 65535: ");
scanf("%d", &n);
printf(" Enter the number (0 - 15): ");
scanf("%d", &m);
res = n & (~(1 << m));
printf(" Number before clearing of %d is: %d", m, n);
printf(" Number after clearing of %d is: %d", m, res);
return 0;
}

OUTPUT

Enter numbers from 0 to 65535: 134
Enter the number (0 - 15): 1
Number before clearing of 1 is: 134
Number after clearing of 1 is: 132

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote