Computer Science, asked by subbaiahcool12, 4 months ago

Write a program to check if a number “N” can be written in sum of the powers of 5. The coefficients
of 5 should always be 1 and p should be unique in the summation series.
N = {5
If representation is not possible; print “Failed".
If representation is possible; print "Passed”.
Print for the below Numbers if they pass or fail the test and record your output.
Test Number
Output
394376
421875
390625
781250
484377
Please send the output table along with your program to the coordinator​

Attachments:

Answers

Answered by sarvodayar
16

Answer:

#include <stdio.h>

#include <math.h>

 

int main(void) {

int tests[] = { 394376, 394376, 421875, 390625, 781250, 484377,  };

int num_tests = sizeof(tests) / sizeof(tests[0]);

int i;

 

for (i = 0; i < num_tests; ++i)

{

int x = tests[i];

int n = (int)(log(x) / log(5));

      if (pow(5, n) == x)          

          printf("Passed %d is a power of 5\n", x);

      else

          printf("Failed %d is not a power of 5\n", x);

}

return 0;

}

Explanation:

First check if last digit is 5.

If last digit is 5; divide it by 5.

If result of division is 1, then number is power of 5.

Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).

Answered by sharadkrishana
0

Answer:

#include <bits/stdc++.h>

using namespace std;

bool ToCheckPowerofX(int n, int x)

{

 while (n > 0) {

 int rem = n % x;

 if (rem >= 2) {

  return false;

 }

 n = n / x;

}

return true;

}

int main()

{

    int testnum[]= {31,26,130,35,128};

// int testnum[] = { 394376, 394376, 421875, 390625, 781250, 484377 };

int num = sizeof(testnum) / sizeof(testnum[0]);

 

 

for(int i = 0 ; i < num; i ++)

{

if (ToCheckPowerofX(testnum[i], 5)) {

 cout << "PASSED" << endl;

}

else {

 cout << "FAILED" << endl;

}

}

return 0;

}

Explanation:

Similar questions