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).
![Perfect Square in c Perfect Square in c](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVCTr9vIr0zqhLFHNqeI9uHEQAygWlHyx-IiehvwfFbE61BBeKKwbW7G30PDhfgZz66RAXBkl8aHrBf2RkbtHvy9E3JFTgyc1TipoOBWOfxNv9uivK9n4lX7kxQDDRI33A8XBqpGdaSC1UFSDZNThallXvoeJrUbn7NfcNU9yYbMnktVCQzRTNyO_K/s16000/perfect-square-banner.png)
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
![Check Perfect Square in C using for loop](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrK8rgNeW2cS5aZ71liGgzkZiqFcr4QzhGkDINZXQMxg_fpJYb_shDxfPdbLmoVNxBkN3B_SxC_xlbKfpFL-sky61_6JMji9e4LPz32ypYGUlL9dORDF624SAWmgzCb2DNmdpYCFRppgrPOS0y_Ti3M6_HXPBEto54btYjpwxESaH25a7hNpPA1Paa/w640-h181/perfect-square-in-c.png)
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 Perfect Square in C using sqrt() Function](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhntrl1RC1EBUcQbpH94xzZ2N4pYfo4Ah6-gTEn2mBjZW2KV7HyT10tkt8JAoO8OPNDBf8bzrvQTNhV_cxTMuggFoVjj1eAP-5n-KjElgTLf7mrqiz16eT2NZu1OAeWrjDXAuCTTBP3SaPg2do_6XC4qYRvJbBM6QAP-J9yfa4U-d04sjBUbFVMS7tb/w640-h133/perfect-square-using-sqrt.png)
Check out our other C programming Examples