Question
Modify this code, by adding to methods, the conversions need to be done arithmetically in C programming 1)DecimaltoHex 2)DecimaltoOctal Show correct output to get rating
Modify this code, by adding to methods, the conversions need to be done arithmetically in C programming
1)DecimaltoHex
2)DecimaltoOctal
Show correct output to get rating Thanks
-------------------------
#include
/*Removed this method as it is not required char getche();*/ char* getDateAndTime(); /*changed the parameter from long to int*/ long DecimaltoBinary(int n); /*Added this method as it required*/ int getInteger(char* str_array);
int main() { /*change the type of long to int as getInteger return the integer*/ int Decimal; do { char buffer[100]; printf(" Enter an Integer (11000000) or type x to exit: "); fgets(buffer, 100, stdin); /*getting the decimal value from calling the method*/ Decimal=getInteger(buffer); /*if user inputs x then getInteger return -1*/ if(Decimal == -1){ printf("Goodbye! "); /*terminating the program*/ break; } printf("Decimal: %d ", Decimal); printf("Hexadecimal: %x ", Decimal); printf("Octal: %o ", Decimal); long x = DecimaltoBinary(Decimal); printf("Binary: %ld ",x);
printf("Save to file? (y/n): ");
char ch[5]; fgets(ch, 100, stdin); FILE *fptr; char file[100]; /*getting the user value and comparing with y*/ if(ch[0]=='y') { /*taken in while loop if file not created then againg ask for file name*/ while(1){ printf(" Enter the file name:"); fgets(file, 100, stdin); FILE *fptr; fptr = fopen(file, "w"); if(fptr == NULL) { printf("Error! on opening file exiting program"); } else{ char name[100]; printf("Enter your Name : ",&name); fgets(name,100,stdin); char* time=getDateAndTime(); /*modified the file output content*/ fprintf(fptr,"Name : %s ",name); fprintf(fptr, "%s ",time); fprintf(fptr, "Decimal : %d ",Decimal); fprintf(fptr,"Hexadecimal: %x ", Decimal); fprintf(fptr, "Octal : %o ",Decimal); fprintf(fptr,"Binary : %ld",x); printf(" File saved"); fclose(fptr); break; } } } } while (1); return 0; }
long DecimaltoBinary(int n) { int remainder; long binary = 0, i = 1;
while (n != 0) { remainder = n % 2; n = n / 2; binary = binary + (remainder*i); i = i*10; } return binary; }
char* getDateAndTime() { time_t t = time(NULL); struct tm *tm = localtime(&t); return asctime(tm); }/*added this method this method takes string and checking if it contains x if yes then return -1 else convert the string to int by usin atoi*/ int getInteger(char *str_array) { if(str_array[0]=='x') { return -1; }
return atoi(str_array); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started