✍️ Get Writing Help
Uncategorized

Test-Driven Development for Decimal to Different Base Conversion

I want you to use Test-Driven Development and add it to the code below.I have two files one of them the header file.This is the code
#include
#include
#include conversion.h
int main() {
char choice;
printf(REDDecimal BLUE to WHITE Different Base Conversionn);
// Main loop for the program
do {
displayMenu();
printf(WHITEEnter your choice ( RED 1 WHITE / RED 2 WHITE / RED 3 WHITE / RED 4 WHITE ): );
scanf( %c, &choice);
// Switch statement to handle user choice
switch(choice) {
case ‘1’:
convertToBinary(getDecimalInput());
break;
case ‘2’:
convertToOctal(getDecimalInput());
break;
case ‘3’:
convertToHexadecimal(getDecimalInput());
break;
case ‘4’:
printf(Exiting program…n);
exit(0);
default:
printf(Invalid choice. Please choose again.n);
}
printf(BLUEDoWHITE you wantBLUE to WHITE convert another number? (y/n): );
scanf( %c, &choice);
} while(choice == ‘y’ || choice == ‘Y’);
return 0;
}
// Display menu function
void displayMenu() {
printf(n);
printf(RED 1 WHITE. Convert BLUE to Binaryn);
printf(RED 2WHITE . Convert BLUE to WHITE Octaln);
printf(RED 3WHITE. Convert BLUE to WHITE Hexadecimaln);
printf(RED 4 WHITE . BLUE Exitn );
printf(n);
}
// Get decimal input from user
int getDecimalInput() {
int decimal;
printf(Enter a RED decimal WHITE number: );
scanf(%d, &decimal);
// Clear input buffer
while (getchar() != ‘n’);
// Error handling for negative numbers
if (decimal < 0) {
printf(Error: Negative numbers are not allowed.n);
exit(1);
}
return decimal;
}
// Convert decimal to binary
void convertToBinary(int decimal) {
printf(BLUEBinary WHITE equivalent: );
if (decimal == 0)
printf(RED 0 WHITE);
else {
int binary[32];
int i = 0;
// Decimal to binary conversion
while (decimal > 0) {
binary[i] = decimal % 2;
decimal /= 2;
i++;
}
// Print binary equivalent
for (int j = i – 1; j >= 0; j–) {
printf(RED %d WHITE, binary[j]);
}
}
printf(n);
}
// Convert decimal to octal
void convertToOctal(int decimal) {
printf(Octal equivalent: );
if (decimal == 0)
printf(RED 0 WHITE);
else {
int octal[32];
int i = 0;
// Decimal to octal conversion
while (decimal > 0) {
octal[i] = decimal % 8;
decimal /= 8;
i++;
}
// Print octal equivalent
for (int j = i – 1; j >= 0; j–) {
printf(RED %d WHITE, octal[j]);
}
}
printf(n);
}
// Convert decimal to hexadecimal
void convertToHexadecimal(int decimal) {
printf(Hexadecimal equivalent: );
if (decimal == 0)
printf(RED 0 WHITE);
else {
char hex[32];
int i = 0;
// Decimal to hexadecimal conversion
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10)
hex[i] = remainder + ‘0’;
else
hex[i] = remainder + 55; // Convert to ASCII values of A-F
decimal /= 16;
i++;
}
// Print hexadecimal equivalent
for (int j = i – 1; j >= 0; j–) {
printf(RED %c WHITE, hex[j]);
}
}
printf(n);
}AND THIS IS THE HEADER FILE
#ifndef CONVERSION_H
#define CONVERSION_H
#include
#include
// ANSI color escape codes
#define RED x1b[31m
#define BLUE x1b[34m
#define WHITE x1b[0m
// Function prototypes
void displayMenu();
int getDecimalInput();
void convertToBinary(int decimal);
void convertToOctal(int decimal);
void convertToHexadecimal(int decimal);
#endif /* CONVERSION_H */

The post Test-Driven Development for Decimal to Different Base Conversion appeared first on assignmentscribe.blog.

Expert academic writer and education specialist helping students in the UK, USA, and Australia achieve their best results.

Need help with your own assignment?

Our expert writers can help you apply everything you've just read — to your actual assignment.

Get Expert Help Now →
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
👋 Hi, how can I help?