Thread: Perfect Square Programming Problem (C)

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    Perfect Square Programming Problem (C)

    Hi, I'm new to programming (like really new) and needed help with a problem that was on my home work

    problem: Write a program to check if the input number (less than 1000) is the summation of two perfect squares or not. For example 25 is 16+9 that both numbers 16 and 9 are perfect squares. The output should be as follows

    If input is 3 ->the message should be "3 is not a summation of two perfect squares"

    If input is 25 ->The message should be: 25 is the summation of two perfect squares because 25=SQ(3)+SQ(4)

    (In mathematics, a square number, sometimes also called a perfect square, is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3×3. )




    I know how to write a program that determines if a number is a perfect square but I am completely stumped on how to determine if an input is the summation of two perfect squares. i'm sorry if this is a bad question i'm very new to programming and this forum

    Thank you!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The most straight forward answer is in the description. You could try summing squares one integer apart. You will either eventually find the number you need to check, or find a higher sum, so you know it isn't a sum of perfect squares.

    For example a search for 25 goes like sq(1)+sq(2) = 5, sq(2)+sq(3) = 13, sq(3)+sq(4) = 25. And again, say you look at a smaller number like 12, the execution is the same, and you will know it's not a perfect square sum by the time you reach 13.
    Last edited by whiteflags; 09-30-2017 at 10:51 PM.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    but how would you go about writing this in code form, I already had the idea for what to do in my head but im stumped on how to write the code
    again sorry im new to all this

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would just use a nested loop.

    a * a + b * b == input

    Use one loop to count a up to 1000 one to count b.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Juan Velasquez View Post
    but how would you go about writing this in code form, I already had the idea for what to do in my head but im stumped on how to write the code
    You're missing a crucial step in the process. You should plan the logic first.

    If you wanted to build a shed, you would not go straight from conceptualizing the idea in your head to buying wood and nailing boards together. You would sit down with a pencil and paper and draw up plans. When the plans are complete and verified, then you use them as a reference to actually build the shed.

    Programming is like this. Once you have a general idea, you sit down with a pencil and paper and start writing out each logical step (in words) needed to solve the problem. Break it down into the smallest steps possible. Step through the logic and make sure it correctly solves the problem.

    Only then do you begin to write code, using the logic you've developed on paper as a reference. If you've done your logic planning well, translating it to code is actually a fairly straightforward process.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    That is where im confused I wrote it out and kind of understood the logic in the end this is the program i have written but it is not giving me the correct output repl.it - online REPL, Compiler & IDE
    what exactly is wrong with it
    Last edited by Juan Velasquez; 10-02-2017 at 08:31 AM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post your code here, using code tags.

    It looks like you took whiteflags' hint and directly inserted it into the source code. You need to think about the ideas we offer in order to apply them correctly.

    Can you describe the process, in words, of solving this problem?

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    alright sorry about not using code tags i really am trying to understand the logic and i feel like i do but im so confused and as you can see ive been working on this problem for a long time and fell stupid as hell but my college instructor hasnt explained the process well and ive been trying to self teach myself. So i understand the logic and the hint presented by whiteflags to add together the squares of all integers until the user input is reached and i know i have to use a for loop to determine this but i dont know how to write the code beyond that this program is the closest ive gotten.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    >> ... add together the squares of all integers ...

    You have two separate loops in your program, neither of which are doing anything useful. If you haven't learned about "nested loops", do a search and research this topic.

    >> ... until the user input is reached ...

    How do you check for this? How do you get your C program to "make a decision"? This is related to the hint whiteflags gave you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-05-2012, 12:55 PM
  2. Perfect stairs problem
    By Taturana182 in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2010, 05:45 PM
  3. anybody perfect in C programming??????
    By dogar sahab in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 03:27 PM
  4. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  5. perfect square prob... math?
    By skeptik in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 05:11 PM

Tags for this Thread