You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
// programmingMode
#include <stdio.h>
#include <math.h>
#include "programmingMode.h"
// Function to convert an integer to binary representation
char* binaryRepresentation(int num);
// get the NUmbers from the user
int getIntegerInput(const char* prompt) { int num; printf("%s", prompt); scanf("%d", &num); return num; }
// Display the diffrent operations and scan them
char getOperatorInput() { char operator; printf("Enter operator (+, -, *, /): "); scanf(" %c", &operator); return operator; }
// Calculation
int performOperation(int num1, char operatorType, int num2) { switch (operatorType) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': if (num2 != 0) { return num1 / num2; } else { printf("Error: Division by zero\n"); return 0; } default: printf("Invalid operator\n"); return 0; } }
void programmingMode() {
char choice; do { int num1 = getIntegerInput("Enter first integer: "); char operator = getOperatorInput(); int num2 = getIntegerInput("Enter second integer: ");
// Calculation
int result = performOperation(num1, operator, num2);
// Display the result in different bases based on user choice
int base; printf("Choose base to print the result (2 for binary, 8 for octal, 16 for hexadecimal): "); scanf("%d", &base);
switch (base) { case 2: printf("Result (Binary): %s\n", binaryRepresentation(result)); break; case 8: printf("Result (Octal): %o\n", result); break; case 16: printf("Result (Hexadecimal): %x\n", result); break; default: printf("Invalid base choice\n"); break; }
// Ask user if they want to continue
printf("Do you want to continue? (y/n): "); scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y'); }
// Function to convert an integer to binary representation
char* binaryRepresentation(int num) { static char binary[32]; for (int i = 31; i >= 0; i--) { binary[i] = (num & 1) + '0'; num >>= 1; } binary[32] = '\0'; return binary; }
|