Answer to Question #308225 in C for ahmed

Question #308225

Write a program that reads a positive integer and checks if it is a perfect square


1
Expert's answer
2022-03-12T12:45:27-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS