Write a program that reads a positive integer and checks if it is a perfect square
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n;
int is_square = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 0) {
printf("It is not a positive number");
exit(1);
}
i = 1;
while (i*i <= n) {
if (i*i == n) {
is_square = 1;
}
i++;
}
if (is_square) {
printf("%d is a perfect square", n);
}
else {
printf("%d is not a perfect square", n);
}
return 0;
}
Comments
Leave a comment