Question

In: Computer Science

Write a program in C that reads prompts the user for a positive integer and then...

Write a program in C that reads prompts the user for a positive integer and then prints out that number in base 16, base 8 and base 2.

Solutions

Expert Solution

Implement te program as follows:

  1. declare integer variable num to store a number
  2. declare array to store binary number
  3. declare counter for binary array
  4. ask the user to enter an integer
  5. read integer from user
  6. print base 10 number using %d format command
  7. print base 8 number using %o format command
  8. print base 16 number using %x format command
  9. repeat until num<0
  10. storing remainder in binary array
  11. divide num by 2
  12. increment i by 1
  13. print binary array in reverse order

Program:

#include <stdio.h>

int main()
{
    int num;                                /* declare integer variable num to store a number */
    int binaryNum[32];                      /* array to store binary number */
    int i = 0;                              /* counter for binary array */
    
    printf("Enter a positive integer: ");   /* ask the user to enter an integer */
    scanf("%d", &num);                      /* read integer from user */
    
    printf("\nBase 10 number : %d ", num);  /* print base 10 number using %d format command */
    printf("\nBase 8 number : %o", num);    /* print base 8 number using %o format command */
    printf("\nBase 16 number : %x", num);   /* print base 16 number using %x format command */
    
    /* convert num to binary */
    while (num > 0) {                       /* repeat until num<0 */
        binaryNum[i] = num % 2;             /* storing remainder in binary array */
        num = num / 2;                      /* divide num by 2 */
        i++;                                /* increment i by 1 */
    } 
  
    printf("\nBase 2 number : "); 
    for (int j = i - 1; j >= 0; j--)        // printing binary array in reverse order
        printf("%d",binaryNum[j]);

    return 0;
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT