Question

In: Computer Science

PLEASE NOTE:1)-DO NOT USE FUNCTIONS USE ONLY DO WHILE LOOP.                          2)DO NOT USE IN-BUILT FUNCTIONS....

PLEASE NOTE:1)-DO NOT USE FUNCTIONS USE ONLY DO WHILE LOOP.

                         2)DO NOT USE IN-BUILT FUNCTIONS.

                         3)Use of string and char is not allowed.            

Write a program in c laungage that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range1 through 256.

Solutions

Expert Solution

Here is the code that will print table of number, binary equivalent of number, octal equivalent of number and hexa decimal equivalent of number using a do while loop:

/**********************************************************
 Program to convert a decimal number to binary, 
 octal and hexadecimal using recursion for numbers 1-256
 **********************************************************/

#include<stdio.h> // include stdio.h library
 //Method declaration
void convertToBaseX(int, int);

//main method
int main(void) {
  //Number variable to keep 1st number
  int num = 1;
  //Printing table header
  printf("number\t\t\tBinary\t\tOctal\t\tHexadecimal\n");
  //Do while loop
  do {
    //print number in first column
    printf("%d", num);
    printf("\t\t\t");
    //calculate binary and print the result for table
    convertToBaseX(num, 2);
    if (num > 7 && num <= 127) {
      printf("\t\t");
    } else if (num > 127) {
      printf("\t");
    } else {
      printf("\t\t");

    }

    //calculate octal and print the result for table
    convertToBaseX(num, 8);
    if (num > 7 && num <= 127) {
      printf("\t\t\t");
    } else if (num > 127) {
      printf("\t\t\t");
    } else {
      printf("\t\t\t");

    }
    //calculate hexadecimal and print the result for table
    convertToBaseX(num, 16);
    //new line for next row
    printf("\n");
    //increment the number
    num++;
  } while (num <= 256); //while loop will run till number is 256    

  return 0; // return 0 to operating system
}
/**
 * Method to print in requried base
 * @param num: number to convert
 * @param base: base in which number is requried to be converted
 */
void convertToBaseX(int num, int base) {
  //remainder handling variable
  int rem;

  //if number is zero then no conversion
  if (num == 0) {
    return;
  }
  //other wise convert the number to requried base
  else {
    //divide number by base and get the remainder
    //to find rightmost digit
    rem = num % base;

    convertToBaseX(num / base, base); // recursive call        
    //handle base 16
    if (base == 16 && rem >= 10) {

      printf("%c", rem + 55);
    } else {
      printf("%d", rem);
    }
  }
}

Output:

Screen shot of output table (Output is very large so broken into different images):


Related Solutions

Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the for loop, and selection. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Please Note: Only do Problem 2 and please read Problem 2 carefully the requirements thanks /***********************************...
Please Note: Only do Problem 2 and please read Problem 2 carefully the requirements thanks /*********************************** * Filename: arraySorted.c * Author: Last name, first name * Email: Your preferred email address * Login ID: Your EECS login ID ************************************/ #include <stdio.h> /********* DO NOT CHANGE THESE CONSTANTS IN THE FINAL SUBMISSION *********/ #define MAX_SIZE 20 #define SUCCESS 0 /****************** YOUR CODE STARTS HERE ******************/ /************************************************************/ /* Input: array A with "size" elements and an integer d Output: d is added...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
1) Write a denotational semantics for do-while loop
1) Write a denotational semantics for do-while loop
You can only use built in Lisp functions and you cannot use setq function. Write a...
You can only use built in Lisp functions and you cannot use setq function. Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT