/* This is the ninth lab of Lab EE107
The purpose of this lab understand how a program to convert a
positive integer to another base.
Creating the code and free of syntax and grammatical errors.
Author: Ekwegbalum Unachukwu
Star ID:do2170dt
Date: March 25th, 2024.
*/

#include <stdio.h>

int main() {
    int number_EU; // Positive integer to be converted
    int base_EU;   // Desired base for conversion
    char result_EU[100];
    int i_EU = 0;

    // Prompt the user to enter the number and base
    printf("Enter the positive integer to be converted: \n");
    scanf("%d", &number_EU);

    printf("Enter the desired base for conversion:\n ");
    scanf("%d", &base_EU);

    // Validate that the base is positive
    if (base_EU <= 1) {
        printf("Base must be greater than 1.\n");
        return 1; // Exit with an error code
    }

    // Convert integer to the specified base
    while (number_EU > 0) {
        int rem_EU = number_EU % base_EU;
        // Convert remainder to character representation
        result_EU[i_EU++] = (rem_EU > 9) ? (rem_EU - 10) + 'A' : rem_EU + '0';
        number_EU /= base_EU;
    }

    // Print the result in reverse order to get the correct representation
    printf("Converted number to base %d: ", base_EU);
    for (int j_EU = i_EU - 1; j_EU >= 0; j_EU--) {
        printf("%c", result_EU[j_EU]);
    }
    printf("\n");

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: