0% found this document useful (0 votes)
5 views3 pages

Base Convert

The document contains a C program that converts base 10 numbers to another base ranging from 2 to 9. It prompts the user for the target base and a base 10 number, then calculates and displays the equivalent number in the specified base. The program handles up to 4-digit outputs in the target base.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Base Convert

The document contains a C program that converts base 10 numbers to another base ranging from 2 to 9. It prompts the user for the target base and a base 10 number, then calculates and displays the equivalent number in the specified base. The program handles up to 4-digit outputs in the target base.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 /**

2 * File: BaseConvert.c
3 * Purpose: Convert base 10 numbers to another base
4 */
5 #include <stdio.h>
6 #include <math.h> // Required for the pow() function
7
int
8 main
() 9
{
10 int base; // The new base (target)
11 int base10Num; // The number in base 10 to convert
12 int maxNumber; // Max number that fits in 4 digits
13
14 int place0; // Digit in the 1st (base^0) position
15 int place1; // Digit in the 2nd (base^1) position
16 int place2; // Digit in the 3rd (base^2) position
17 int place3; // Digit in the 4th (base^3) position
18 int q, h; // q for quotient, h for the final result
19 20 printf("Base Conversion program\n"); // Display program title 21 22
printf("Please enter a base (2 - 9): "); // Prompt for target base 23
scanf("%d", &base); // Read the base from user 24
maxNumber = pow(base, 4) - 1; // Calculate max value for 4digits
2
(base^4 - 1)
5
26
27 printf("\nEnter base ten number (less than %d): ", maxNumber); //
Show limit to user
28 scanf("%d", &base10Num); // Read base 10 number
29
30 place0 = base10Num % base; // Get remainder for 1st digit
31 q = base10Num / base; // Get quotient for next step
32
33 place1 = q % base; // Get remainder for 2nd digit
34 q = q / base; // Get quotient for next step
35
36 place2 = q % base; // Get remainder for 3rd digit
37 q = q / base; // Get quotient for next step
38
39 place3 = q % base; // Get remainder for 4th digit
40
41 // Combine digits into a single integer (e.g., 1, 0, 1, 1 becomes 1101)
42 h = place0 + (place1 * 10) + (place2 * 100) + (place3 * 1000);
4
3
4 printf("\nThe number in base %d is %d\n", base, h); // Print the
4 final result
4
5
46 return 0; // End of program
47 }
48

You might also like