#include <stdio.h> #include <string.h> int getche(void); char* getDateAndTime();
ID: 3856665 • Letter: #
Question
#include <stdio.h>
#include <string.h>
int getche(void);
char* getDateAndTime();
long DecimaltoBinary(long n);
int main(){
long Decimal = 240;
do {
printf("Enter an Integer (1 – 1000000) or type x to exit: ");
scanf("%d", &Decimal);
}while(Decimal < 1 || Decimal > 1000000);
printf("Decimal: %d ", Decimal);
printf("Hexadecimal: %x ", Decimal);
printf("Octal: %o ", Decimal);
printf("Binary: %ld ", DecimaltoBinary(Decimal));
return 0;
}
/* Funccton to convert a binary number to decimal number */
long DecimaltoBinary(long 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;
}
printf("Save to file? (y/n): ");
ch=getche();
if(ch=='y')
{
printf(" enter the file name:");
scanf("%s",&file);
FILE *fptr;
fptr = fopen(file, "w");
if(fptr == NULL)
{
printf("Error!");
return 1;
}
char* time=getDateAndTime();
fprintf(fptr, "%s",time);
fprintf(fptr,"%s %d %d", hexa, oct, bin);
printf(" File saved");
fclose(fptr);
}
THIS IS MY CODE. Please help get the correct output. I need C language output
ConvertDec to_Hex-Oct-Bin.c is a simple conversion program that prompts the user for an integer from 1 to 1,000,000, and displays this integer value in hexadecimal, octal, and binary representation. The user is then asked if the output should be saved to a text file. If user replies in the affirmative, a name for the text file is requested. If the requested text file cannot be created, user is re-prompted for a different file name. The text file will contain user's name, current date, and output results (see example below). User Interface: Enter an Integer (1- 10 000000) or type x to exit: 240 Decimal: 240 Hexadecimal: FO Octal: 360 Binary: 11110000 Save to a file? (y/n): y Enter file name: cs222_hw3.txt File saved. Enter an Integer (1- 1000000) or typex to exit: x Good bye!Explanation / Answer
// please comment below,,if you find this answer useful...Thanx
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
char getche();
char* getDateAndTime();
long DecimaltoBinary(long n);
int main() {
long Decimal = 240;
do {
printf("Enter an Integer (1 – 1000000) or type x to exit: ");
scanf("%d", &Decimal);
} while (Decimal < 1 || Decimal > 1000000);
printf("Decimal: %d ", Decimal);
printf("Hexadecimal: %x ", Decimal);
printf("Octal: %o ", Decimal);
int x = DecimaltoBinary(Decimal);
printf("Binary: %ld ",x);
printf("Save to file? (y/n): ");
char ch = getche();
if (ch == 'y')
{
char file2[100];
printf("Enter the file name: ");
scanf("%s", &file2);
FILE *fptr;
fptr = fopen(file2,"w");
if (fptr == NULL)
{
printf("Error!");
fclose(fptr);
return 1;
}
char* time = getDateAndTime();
fprintf(fptr, "%s",time);
fprintf(fptr,"%x %o %ld", Decimal, Decimal,x);
printf(" File saved");
fclose(fptr);
exit(0);
}
else
{
printf("good bye ");
}
return 0;
}
long DecimaltoBinary(long 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 rawtime;
struct tm *info;
char *buffer;
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, 80 , "%x - %I:%M%p", info);
return buffer;
}
char getche()
{
char x;
scanf(" %c",&x);
return x;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.