Thread: Help with Perfect Squares before and after

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Help with Perfect Squares before and after

    Hello I am new to C programming and am simply doing it for fun. I'm trying to write a program that asks the user for a number and tells them if it's a perfect square or not, and then what the perfect square before and after is. Here is what I have so far:

    Code:
        int x, y;    int temp = y;
    
    
    
    
        /*
            int loopCounter;
            int nextPerfectSquare;
        */
        /*Sub Method- Takes y as parameter*/
        int perfectSquares(y)
        {
            x = sqrt(y);
            /*See if x times x is y*/
            return (x * x == y);
        }
    
    
        /* Program Heading */
        printf("Perfect Squares\n");
        printf("---------------\n\n");
        printf("Enter a positive integer (1-1000000):\n");
    
    
        /*The check if number entered is a perfect square*/
        scanf("%d", &y);
      
        /*fgets(&y, maxValue, stdin);*/
    
    
        /*Test print*/
        printf("VALUE ENTERED: %d\n",y);
    
    
        if(perfectSquares(y))
        {
            printf("\n");
            printf("The number you entered is a perfect square!\n");
        }
    
    
        else
        {
    
    
            printf("The number you entered is not a perfect square!\n");
    
    
        }
    
    
        /*Print Perfect Square Before and After*/
    
    
        int loopCounter = 1;
        for(loopCounter =1; loopCounter<y; loopCounter++)
        {
            y--;
            if(perfectSquares(y))
            {
                printf("\nPerfect square before: %d \n",y);
    
    
            }
        }
    The program works correctly in telling the user if it's a perfect square or not but I'm not sure how to fix my loop so it only says 1 perfect square before and 1 after. Can someone please help me? Also how can I use fgets instead of scanf and still get it to work? Thank you for your time.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You will need two loops, one for before and one for after. Start loopCounter at y and go down (before) or up (after) until you find the first perfect square.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Is this correct?
    Code:
     /*Print Perfect Square Before and After*/
    
    
        int loopCounter = y;
        int i=0;
        for(i=0; i<loopCounter; i++)
        {
            y--;
            if(perfectSquares(y))
            {
                printf("\nPerfect square before: %d \n",y);
    
    
            }
        }
    
    
        for(i=0; i<loopCounter; i++)
        {
            y++;
            if(perfectSquares(y))
            {
                printf("\nPerfect square after: %d \n",y);
    
    
            }
        }
    
    
    
    
    
    
    
    
    }
    Sorry if I misunderstood. Why does it print heaps of perfect squares before instead of just 1 before? Also my after is glitched xD

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It would probably be easier to demonstrate in a while loop, now that I think about it. Try something along these lines:
    Code:
    loopCounter = y;
    while loopCounter is not a perfect square
        loopCounter--;
    print "perfect square before is" loopCounter
    After is similar, but loopCounter needs to increment, not decrement.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Would the correct syntax be like this?
    Code:
    int loopCounter = y;
        while(perfectSquares(loopCounter) ==FALSE){
    
    
    loopCounter --;
    printf("\nPerfect square before: %d \n",y);
              }
    Or this?

    Code:
    int loopCounter = y;
        while(perfectSquares(!loopCounter)){
    
    
    loopCounter --;
    printf("\nPerfect square before: %d \n",y);
              }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The first loop condition would work, so long as you have FALSE defined as 0 somewhere. The version using ! would look like this (this is how I would do it):
    Code:
    while (!perfectSquares(loopCounter)) {
        loopCounter--;
    }
    // Notice this is outside the loop, as my indentation showed in the above pseudo code
    // Also notice, I am printing loopCounter, not y (remember, y is the number the user entered)
    printf("Perfect square before: %d\n", loopCounter);

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Thanks, I got it working. I would appreciate it if you could answer one more quick question, how can I replace
    Code:
     scanf("%d", &y);
    with an fgets statement?

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Fgets() input into a temporary (stack) char buffer, and then use atoi() to get the number.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why would you want to?
    If you really do want to, you'd have to read the line into a string then use e.g. atoi to extract the integer.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Magic Squares
    By KidCourageous in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2004, 12:26 AM
  2. Small program, PRIME/ PERFECT SQUAREs
    By thynksheraze in forum C++ Programming
    Replies: 6
    Last Post: 01-15-2003, 09:54 AM
  3. squares
    By abrege in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 10:33 PM
  4. Squares
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-08-2002, 11:47 AM
  5. perfect squares?
    By ahalya in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2001, 09:38 PM