Contact Learn C
Copy

Program 275: Program to check whether the given number is power of an integer or not

Program 275: Program to check whether the given number is power of an integer or not

#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:

  1. Declare Variables i,j,num,power,temp
  2. Lets take num=64 and power be 4
  3. Now temp=64(Used for storing temporarily)
  4. 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 executed 
  5.  while(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:
Program to check whether the given number is power of an integer or not
Program to check whether the given number is power of an integer or not


Program to check whether the given number is power of an integer or not




Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide