In order to open secret chambers present in the pyramids the ancient scholars ha
ID: 3723068 • Letter: I
Question
In order to open secret chambers present in the pyramids the ancient scholars have set keys to each of these chambers. The keys are evidently present at the entrance of the each of the chambers, but some of them are mines set to explode the entire pyramid upon entering the key. The trio come across a scripture that tells them that only those keys whose nth power of the sum of each of the digits equals the key are the ones which are valid keys. Here n is the number of digits present in the number.
Write a C++ program using functions (call by value, do not use call by reference) that takes in the key numbers and returns whether this key can help open the secret chambers or not. If the key is not the right one print a message “Do not open the chamber”. If the key is the right key, then print “Open the chamber”. You must write a function to validate the input, a function where you compute the logic and a function for printing.
Input and Input Validation: The key will be all positive numbers. Any key that is negative or zero should not be computed.
Sample Output and Explanation:
Enter the key: 153 Open the chamber.
The reason it is ok to open the chamber is because 13 + 5 3 + 33 = 153
Input Scenarios:
Key = 370
Key = -5
Key = 4679307774
Key = 351
Key = 199091
Explanation / Answer
//--------------below is the code in c++-------------
//If you have any doubts comment below--------------
//------------and plz THUMBS UP. thank you :)----------
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
cout<<" enter -1 to exit otherwise enter key ";
int key,temp;
do
{
cout<<"key = ";
cin>>key;
temp=key;
cout<<endl;
if(key>=0)
{
std::string s = std::to_string(key);
int sum=0,a=0,b=0, sz=(s.size())-1; //s,size() gives size of string s
for(int i=sz;i>-1;i--)
{
int p=pow(10,i);
a=key/p; //to find out every digit of nuber
sum=sum+pow(a,3);
key=key%p; //to find reminder
}
cout<<" for key = "<<temp;
if(sum==temp)
cout<<" open chember"<<endl;
else
cout<<" do not open chember "<<endl;
}
cout<<endl;
}while(temp!=(-1));
return 0;
}