Program 275: Program to check whether the given number is power of an integer or not
Output:
#include<stdio.h> #include<stdlib.h> int main() { int i,j,num,power,temp; printf("Enter Number\n"); scanf("%d",&num); printf("Enter Power you want to check\n"); scanf("%d",&power); temp=num; if(num==0 || num==1) { printf("Enter Number other than zero and 1\n"); exit(0); } while(num>1) { if(num%power!=0) { printf("Given Number %d is not power of %d\n",temp,power); exit(0); } num=num/power; } printf("Given Number %d is power of %d\n",temp,power); return(0); }Explanation:
- Declare Variables i,j,num,power,temp
- Lets take num=64 and power be 4
- Now temp=64(Used for storing temporarily)
if(num==0 || num==1) { printf("Enter Number other than zero and 1\n"); exit(0); }
As num=64 is not 0 or 1 then the if part is not executedwhile(num>1) { if(num%power!=0) { printf("Given Number %d is not power of %d\n",temp,power); exit(0); } num=num/power; }
As num=64 then it can be taken as 4^3 so when we divide 4^3 for three times one by one and finally when we reached num=1 then WHILE condition will break at this point.If the IF part is not executed till num became 1 then the Given number 64 is power of 4.
Output: