Quote:
What is the method to solve a problem like this in c.
The same as with any other programming language.
Take a sheet of paper and a pencil and solve the problem by hand and find the maths.
You solved the problem by following a procedure, that procedure is your algorithm. You need to write down the steps in a mechanical manner (computer like). The program will follow those steps.
As programmer,
your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
We do not do your HomeWork.