Home > Mobile >  C Programming for perfect squares
C Programming for perfect squares

Time:03-07

I cannot figure out how to get a number from the user then turn out an output to state all the possible perfect squares up till the program reaches the given number.

We are using Loops in C and we have to use a for loop to take the number and output the possible perfect squares.

Here is an example of what the output should be: Enter a number between 1 and 1000 inclusive: -50 The number you entered, -50, was not between 1 and 1000. Please re-enter a number between 1 and 1000 inclusive: 100

The perfect squares between 1 and 100 inclusive are: 1 4 9 16 25 36 49 64 81 100

My code:

int main(void)
{
    // Constant and Variable Declarations
    const int MIN_NUM = 1; // constant for the minNum allowed
    const int MAX_NUM = 1000; // constant for the maxNum allowed
    int userNum = 0; // input variable
    int userNum1 = userNum;
    int perfectSquare = 0;


    // *** Your program goes here ***
    printf("Enter a number between 1 and 1000 inclusive: "); // asking the user to input their number
    scanf("%d", &userNum);

    // validation
    while (userNum < MIN_NUM || userNum > MAX_NUM) {
        printf("   The number you entered, %d, was not between 1 and 1000.\n", userNum); // telling user num they entered was not valid
        printf("   Please re-enter a number between 1 and 1000 inclusive: "); // asking the user to enter a different number
        scanf("%d", &userNum);
    }
    printf("\n"); // blank line

    // output
    printf("The perfect squares between 1 and 100 inclusive are:");

    


    printf("\n");
    return 0;
} // end main()

not sure how to find the perfect sqaures Sorry if any of this is unclear this is my first time posting on overflow! Thanks for all the help

CodePudding user response:

One thing you could do is, instead of checking which numbers less than userNum are perfect squares, print each perfect square starting from 1 until you find one that is more than userNum. This can easily be done with a for loop.

(the code itself is left as an exercise for the reader)

CodePudding user response:

Thank you guys for the help!

int main(void)
{
    // Constant and Variable Declarations
    const int MIN_NUM = 1; // constant for the minNum allowed
    const int MAX_NUM = 1000; // constant for the maxNum allowed
    int userNum = 0; // input variable
    int userNum1 = userNum;
    int perfectSquare = 0;
    int root1 = 0;
    int root2 = 0;


    // *** Your program goes here ***
    printf("Enter a number between 1 and 1000 inclusive: "); // asking the user to input their number
    scanf("%d", &userNum);

    // validation
    while (userNum < MIN_NUM || userNum > MAX_NUM) {
        printf("   The number you entered, %d, was not between 1 and 1000.\n", userNum); // telling user num they entered was not valid
        printf("   Please re-enter a number between 1 and 1000 inclusive: "); // asking the user to enter a different number
        scanf("%d", &userNum);
    }
    printf("\n"); // blank line

    // output
    printf("The perfect squares between %d and %d inclusive are:\n", MIN_NUM, userNum);
    
    for (int i = 1; perfectSquare <= userNum - 1; i  ) {
        root1 = sqrt(floor(userNum));   
        root2 = sqrt(userNum);
        perfectSquare = i * i;
        printf("%d\n", perfectSquare);
        if (perfectSquare > userNum) {
            break;
        }
    }



    printf("\n");
    return 0;
} // end main()

output:

Enter a number between 1 and 1000 inclusive: 81

The perfect squares between 1 and 81 inclusive are:
1
4
9
16
25
36
49
64
81
  •  Tags:  
  • c
  • Related