In: Computer Science
write C++ program using functions
(separate function for each bottom)
Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. .
the program should ask from user to enter a number that it
should ask to enter the base ranging from 2 to 16 after that it
should check if the number is palindrom or not
Sample Input:
A number is called a word if it's represented in its bottom.
e.g. Let bottom1 = 6 and bottom2 = 2 as it is not a word
in base 5(1010)(as the reciprocal of the number isnt a
palindrome).
bottom is base of a number
take bases as input from user
bases can go from decimal-hexadecimal.
for bottom1 = 3 & bottom2 = 4, then the number 130 (in base 10) will be called a large_word, as it is word in bottom 3 (11211) as well as in bottom 4 (2002). However, it is not a large_word for bottom1 = 3 and bottom2 = 5 as it is not a word in bottom 5(1010).
Number: 51
bottom(base) 1: 6
bottom(base) 2: 2
Sample Output:
51 is not large word
hint:
bottom is basically base of a number
A large _word is a word, phrase, number, or other sequence of characters which reads the same reverse or straightward.
word==palindrome
use function in the program at all time
Code-
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char val(int n)// to return the charcter of the number accordingly
{
if(n>=0 && n<=9)
return (char)(n+'0'); // returns the normal charcter from 0-9
else
return (char)(n-10+'A');// returns char from A-F accordingly
}
string reverse(string s)// to reverse the string
{
string rev="";
for(int i=s.length()-1; i>=0; i--)
rev+=s[i];
return rev;
}
int base1(int b1,int num)// to convert to base 1
{
string s1;
while(num>0)
{
s1+=val(num%b1);
num=num/b1;
}
if(s1==reverse(s1))// compare reverse with original
return 1;
else
return 0;
}
int base2(int b2,int num)// to convert to base 2
{
string s2;
while(num>0)
{
s2+=val(num%b2);
num=num/b2;
}
if(s2==reverse(s2))//compare reverse with original
return 1;
else
return 0;
}
int main()
{
cout<<"Number: ";
int num,b1,b2;
cin>>num;
cout<<"bottom base 1: ";
cin>>b1;
cout<<"bottom base 2: ";
cin>>b2;
if(base1(b1,num)==1 && base2(b2,num)==1)// checking for large word
cout<<num<<" is a large word";
else
cout<<num<<" is not a large word";
return 0;
}
Indentation-
Output-