Check Perfect Square in C
Here, you will know about perfect Square, and get the program code to check Perfect Square in C Programming using 2 different methods.
What is Perfect Square?
A perfect square is an integer that is produced when a number is multiplied by itself. For example, 2 is multiplied by itself to give the number 4, which is a perfect square(2 x 2 = 4).
Program Code to Check Perfect Square in C using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // C program to Check for Perfect Square #include<stdio.h> int main() { // variable declaration int i, num; // user input printf("\n\n\t\tC program to Check for Perfect Square "); printf("\n\n\t\tEnter a number: "); scanf("%d", &num); // loop for checking number is perfect square or not for(i = 0; i <= num; i++) { if(num == i*i) { printf("\n\n\t\t%d is a perfect square\n\n\n", num); return 0; // break the program } } printf("\n\n\t\t%d is not a perfect square ", num); return 0; } |
Output
Program Code to Check Perfect Square in C using sqrt() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // C program to Check for Perfect Square using sqrt() Function #include<stdio.h> int main() { // variable declaration int i,k,num; // user input printf("\n\n\t\tC program to Check for Perfect Square using sqrt() Function"); printf("\n\n\t\tEnter a number: "); scanf("%d", &num); k=sqrt(num); if(k*k==num) printf("\n\n\t\t%d is a perfect square ", num); else printf("\n\n\t\t%d is not a perfect square ", num); } |
Output
Check out our other C programming Examples