Write C++ programs to perform the following tasks. In the program descriptions b
ID: 3786165 • Letter: W
Question
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems.
Write a program that prints out the binary, octal, decimal, and hexadecimal representation (respectively) of all unsigned shorts (in order). If the number is divisible by 3, then also print “Go”. If the number is divisible by 5, then also print “GOGOGO”. Otherwise, “S” should also be printed. Below is a sample of some of the entries near the middle:
output must look the same as the example provided above.
Explanation / Answer
using namespace std;
#include<string>
#include<iostream>
int toBinary(int);
string toHex(int);
int toOctal(int);
int main()
{
string str;
for(int i=0;i<=65535;i++)
{
if(i%3==0)str="Go";
else if(i%5==0)str="GOGOGO";
else str="S";
cout<<" 0b"<<toBinary(i)<<" "<<toHex(i)<<" "<<toOctal(i)<<" " <<i<<" "<<str;
}
return 0;
}
int toOctal(int num)
{
int total = 0;
while(num > 0)
{
total = num % 8;
num /= 8;
cout << total << " ";
}
return total;
}
int toBinary(int num)
{
int total = 0;
while(num > 0)
{
total = num % 2;
num /= 2;
}
return total;
}
string toHex(int num)
{
char hexdecnum[100];
int temp;
string final;
int quot;
quot=num;
int i=1;
while(quot!=0)
{
temp=quot%16;
// to convert integer into character
if(temp<10)
{
temp=temp+48;
}
else
{
temp=temp+55;
}
hexdecnum[i++]=temp;
quot=quot/16;
}
final=hexdecnum;
return final;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.