Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-----------------------------------------------------------------------------------------------------
- PROGRAMMER: Kevin Tran
- LANGUAGE: C
- CLASS: CSE 1320-001, "Intermediate Programming", Spring 2011
- PLATFORM: OMEGA
- OS: UNIX
- COMPILER: gcc
- ASSIGNMENT: Lab #0
- DUE: 02/07/2011
- FILE NAME: lab0.c
- CONCEPTS: To read two integers n and b where n is a decimal number and b represents a numeric base.
- And convert the decimal number n into a number m in the numeric base b.
- ------------------------------------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- int main (void)
- {
- int mainNum; /* Variable declarations */
- int numBase;
- int remainder;
- int counter = 0;
- int i;
- int printArray[100];
- scanf("%d", &mainNum); /* Reads a decimal value and stores it in the variable mainNum and numBase */
- scanf("%d", &numBase);
- if ((numBase >= 2) && (numBase <= 16)) /* If statement that checks if numBase is any number from 2 to 16 */
- {
- while (mainNum > 0) /* While loop that converts the decimal number mainNum into a number reaminder in the numeric base numBase */
- {
- remainder = mainNum % numBase;
- mainNum = mainNum / numBase;
- printArray[counter] = remainder; /* Stores reaminder into an array variable printArray at position of counter */
- counter++; /* Counter is then increased up one value */
- }
- for(i = (counter - 1); i >= 0; i--) /* For loop that prints printArray in reverse order */
- {
- if (printArray[i] == 10) /* If and Else If statements that converts number from 10 to 16 into letters a to f */
- {
- printf("a");
- }
- else if (printArray[i] == 11)
- {
- printf("b");
- }
- else if (printArray[i] == 12)
- {
- printf("c");
- }
- else if (printArray[i] == 13)
- {
- printf("d");
- }
- else if (printArray[i] == 14)
- {
- printf("e");
- }
- else if (printArray[i] == 15)
- {
- printf("f");
- }
- else
- {
- printf("%d", printArray[i]);
- }
- }
- }
- if ((numBase < 2) || (numBase > 16)) /* If statement that exits program if numBase is lesser than 2 or greater than 16 */
- {
- exit(0);
- }
- printf("\n"); /* Print statement for a new line to keep output clean */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement