Using a loop (not the output format character %o), create a program that takes a positive integer n, and then displays the polynomial for the octal representation of that integer. Use successive division, as demonstrated in the binary conversion example from the lesson, to do this. For example, for n = 157, the program should output the following:
157 = + (5 * 8^0) + (3 * 8^1) + (2 * 8^2)
When this part is working properly, “surround” this code with a loop that gets the value of n from the user, and exits the loop when the value is negative.
The language for the program was not mentioned so I have done this in C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i, n, b, a, k, rem;
while(true)
{
int arr[50] = {0};
cout << "Enter Positive Number for octal representation \n
Enter Negative Number to quit" << endl;
cin >> n;
if (n < 0)
break;
cout << endl;
a=n;
for(i=1;i<=20;i++)
{
b=pow(8,i);
if(a<b)
{k=i;break;}
}
i = 0;
while(a > 0)
{
rem = a % 8;
arr[i] = rem;
a /= 8; i++;
}
cout << n << " = ";
for(i = 0;i < k; i++)
cout << "+ (" << arr[i] << " * 8^" << i
<< ") ";
cout << endl;
}
return 0;
}
Hope you have understood the solution
Get Answers For Free
Most questions answered within 1 hours.