C Programming – HackerRank Solution | Power of K |

Write a Modular C Programming code to solve an Power of K, Given a number ‘N’ find if the number is perfect power of ‘K’ or not

Given a number ‘N’ find if the number is perfect power of ‘K’ or not.

Input Format

First Line contains N and K separated by a space.

Constraints

N>0 and K>0.

Output Format

Display YES if N is power of K else display NO

Sample Input 0

8 2

Sample Output 0

YES

Explanation 0

8 is power of 2

Sample Input 1

42.875 3.5

Sample Output 1

YES

Explanation 1

(3.5^3) = 42.875

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>



int main() 
{
    float a,b,c,status=0;
    scanf("%f %f",&a,&b);
    if(a==b)
    {
        printf("YES");
    }
    else
    {
    c=b;
    while(abs(b)<abs(a)) 
{ 
b=b*c; 
if(a==b)
 { 
status=1; 
break; 
} 
else if(b>a)
        {
            status=2;
            break;
        }
    }
    if(status==1)
        printf("YES");
    else if(status==2)
        printf("NO");
    }
    return(0);
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

8 2
Your Output (stdout)

YES
Expected Output

YES

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!