+lab3.pdf Background Positional number systems are not exactly modern: Babylonia
ID: 3591732 • Letter: #
Question
+lab3.pdf Background Positional number systems are not exactly modern: Babylonians used a sexagesimal (60 symbols) representation for umbers, we can see their contribution in how we still measure time and ages: Mayans used a vigesimal system (20 symbols) in their astronomical computations and the current decimal system was introduced first in India and then migrate to the Arabie world which taught it to Europeans. In this lab you will be ask to translate between different s number systems and practice while loop Assignment Write a C program that given a positive mamber 1 Sb (base 10) integer n reverse prints n in base b 9 called the hase and a decimal Example Suppose n = 8, if b-2 your prograrn should print 0001 since 0 x 2° + 0 x 2, +0×22 + 1 × 23-8 &5 your program should print 31 since 3 × 5° + 1 x 51 = 8 -1 yoar program should print since 1 x 1+1x1 x 12+11P+1 x1+1x 1+1 x 1 +1 x 1-Explanation / Answer
// C Program to convert decimal number to any given base b in reverse order
#include <stdio.h>
#include <string.h>
// To return char for a value. For example '5' is returned for 5
char reVal(int num)
{
// Conversion to number in proper base form eg 10 is A 11 is B
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
// Function to convert a given decimal number
// to a base 'base' and
char* fromDeci(char res[], int base, int inputNum)
{
int index = 0,j=0; // Initialize index of result
if(base==1){
// If base ==1 the simply append 1 inputNum times
for(j=0;j<inputNum;j++)
res[index++] = '1';
}
else{
// Convert input number is given base by repeatedly
// dividing it by base and taking remainder
while (inputNum > 0)
{
res[index++] = reVal(inputNum % base); // convert the last digit in give base and store in res
inputNum /= base; // truncate last digit
}
}
res[index] = '';
return res;
}
// Driver program
int main()
{
int inputNum = 8, base = 10;
char res[100];
// you can use scanf to take input of inputNum, base
while(1){
printf("Enter Natural Number and base b between 1 and 9");
scanf("%d,%d",&inputNum,&base);
if(base>9)
continue;
else
break;
}
printf("Equivalent of %d in base %d in reverse order is "
" %s ", inputNum, base, fromDeci(res, base, inputNum));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.