Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. /*-----------------------------------------------------------------------------------------------------
  2. PROGRAMMER: Kevin Tran
  3. LANGUAGE: C
  4. CLASS: CSE 1320-001, "Intermediate Programming", Spring 2011
  5. PLATFORM: OMEGA
  6. OS: UNIX
  7. COMPILER: gcc
  8. ASSIGNMENT: Lab #0
  9. DUE: 02/07/2011
  10. FILE NAME: lab0.c
  11. CONCEPTS: To read two integers n and b where n is a decimal number and b represents a numeric base.
  12. And convert the decimal number n into a number m in the numeric base b.
  13. ------------------------------------------------------------------------------------------------------*/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. int main (void)
  19. {
  20. int mainNum; /* Variable declarations */
  21. int numBase;
  22. int remainder;
  23. int counter = 0;
  24. int i;
  25. int printArray[100];
  26.  
  27. scanf("%d", &mainNum); /* Reads a decimal value and stores it in the variable mainNum and numBase */
  28. scanf("%d", &numBase);
  29.  
  30. if ((numBase >= 2) && (numBase <= 16)) /* If statement that checks if numBase is any number from 2 to 16 */
  31. {
  32. while (mainNum > 0) /* While loop that converts the decimal number mainNum into a number reaminder in the numeric base numBase */
  33. {
  34. remainder = mainNum % numBase;
  35. mainNum = mainNum / numBase;
  36.  
  37. printArray[counter] = remainder; /* Stores reaminder into an array variable printArray at position of counter */
  38. counter++; /* Counter is then increased up one value */
  39. }
  40.  
  41. for(i = (counter - 1); i >= 0; i--) /* For loop that prints printArray in reverse order */
  42. {
  43. if (printArray[i] == 10) /* If and Else If statements that converts number from 10 to 16 into letters a to f */
  44. {
  45. printf("a");
  46. }
  47.  
  48. else if (printArray[i] == 11)
  49. {
  50. printf("b");
  51. }
  52.  
  53. else if (printArray[i] == 12)
  54. {
  55. printf("c");
  56. }
  57.  
  58. else if (printArray[i] == 13)
  59. {
  60. printf("d");
  61. }
  62.  
  63. else if (printArray[i] == 14)
  64. {
  65. printf("e");
  66. }
  67.  
  68. else if (printArray[i] == 15)
  69. {
  70. printf("f");
  71. }
  72.  
  73. else
  74. {
  75. printf("%d", printArray[i]);
  76. }
  77. }
  78. }
  79.  
  80. if ((numBase < 2) || (numBase > 16)) /* If statement that exits program if numBase is lesser than 2 or greater than 16 */
  81. {
  82. exit(0);
  83. }
  84.  
  85. printf("\n"); /* Print statement for a new line to keep output clean */
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement