Docsity

engineering economy problems with solution engineering economy problems with solution, Exercises of Ethics

Partial preview of the text

Download engineering economy problems with solution engineering economy problems with solution and more Exercises Ethics in PDF only on Docsity!

Question

Answer

Solution:

/* The below program will convert positive decimal number to its Binary, Octal or Hexadecimal equivalent*/ #include<stdio.h> //Header file included

void convertFunction(int, int);//Function declaration. This function will convert decimal to desired number //Main function int main() { int number; //Declared an int to take user entered number printf("Enter a positive decimal number: ");//Ask the user to enter a number scanf("%d", &number);//Get user entered number printf("Value to which number system:\n ");//Giving choice to user printf("1 - Decimal to Binary \n"); //Enter 1 to convert into binary printf(" 2 - Decimal to Octal \n"); //Enter 2 to convert into octal printf(" 3 - decimal to hexadecimal\n"); //Enter 3 to convert into hexadecimal int choice;//Declared an int to take user entered choice value printf("Enter choice: ");//Ask user to enter choice scanf("%d", &choice);//Get user entered choice //Switch case statement. Based on user entered value a case will be executed switch(choice){ case 1://If choice is 1 printf("Equivalent of %d in binary is ", number); //Convert into binary convertFunction(number, 2);//Call convertFunction by passing number as first and 2 as second argument break; case 2://If choice is 2 printf("Equivalent of %d in otcal is ", number); //Convert into octal convertFunction(number, 8); //To Octal Conversion break; case 3://If choice is 3 printf("Equivalent of %d in hexadecimal is ", number);//Convert into hexadecimal convertFunction(number, 16); //To Hexadecimal conversion break; } return 0; }/End of main()/ //The below function convert decimal to binary or octal or hexadecimal based on second argument void convertFunction (int number, int baseCase) //Here base is the value according to which binary, octal and hexadecimal conversion will be done { int remainder = number%baseCase; //Calculating the remainder by using modulus operator if(number==0) //If number is 0 the no conversion and return from this function return; convertFunction(number/baseCase, baseCase);//else call the same function by self if(remainder < 10)//IF remainder is less than 10, then print the rem ainder printf("%d", remainder); else //Else printf("%c", remainder-10+'A' );