Check Perfect Square Number C Program

By | July 29, 2016

C Program To Check Perfect Square Number

Learn How To Check Perfect Square Number in C Programming Language. This C Program finds if an entered if a Number is a Perfect Square or not with and without using Functions.

What is a Perfect Square Number?

A Perfect Square is also known as Simply Square Number. A Perfect Square is a Number that can be expressed as the Product of Two Equal Integers. 4 is a Perfect Square Integer since it is the product of 2 * 2.

Example

1, 4, 16, 25, 36

Must Read: C Program To Check if a Number is a Prime Number or Not

C Program To Check Perfect Square Number without Function

#include<stdio.h>

int main()
{
      int num, temp, count = 1;
      printf("\nEnter the Number:\t");
      scanf("%d", &num);
      temp = num;
      while(temp > 0)
      {
            temp = temp - count;
            count = count + 2;
      }
      if(temp == 0)
      {
            printf("\n%d is a Perfect Square\n", num);
      }
      else
      {
            printf("\n%d is Not a Perfect Square\n", num);
      }
      return 0;
}

C Program To Find Perfect Square Integer using Function

#include<stdio.h>

int perfect_square_check(int temp)
{
      int count = 1;
      while(temp > 0)
      {
            temp = temp - count;
            count = count + 2;
      }
      if(temp == 0)
      {
            return 1;
      }
      return 0;
}

int main()
{
      int num, result;
      printf("\nEnter the Number:\t");
      scanf("%d", &num);
      result = perfect_square_check(num);
      if(result == 1)
      {
            printf("\n%d is a Perfect Square\n", num);
      }
      else
      {
            printf("\n%d is Not a Perfect Square\n", num);
      }
      return 0;
}

Must Read: C Program To Raise A Number to a Positive Power

 

Output

C Program To Check Perfect Square Number using Function

If you have any compilation errors or doubts in this C Program To Check if a Number is Perfect Square or Not, let us know about in the Comment Section below.

One thought on “Check Perfect Square Number C Program

  1. Raja Venky

    Can we use For loop in the above C Program to check if a number is a perfect number or not?

    Reply

Let's Discuss