All 11 questions UPSC has asked on algorithms and flow charts in the Mathematics Optional (Paper II, Numerical Analysis and Computer Programming) from 2008 to 2019, solved step by step – latest first.
Try them yourself first: the same questions, without solutions, are on our Numerical Analysis and Computer Programming topic-wise PYQs page.
How we check these solutions
These are our own solutions, prepared with AI help. UPSC does not publish official answers. Every solution is checked a second way before it goes up, and says how:
- ✓ Checked by computer the answer was worked out again independently with Python: each iteration, table or conversion was run again in double precision (or exact fractions), and algorithms were run on test equations.
- ✓ Proof checked step by step for “prove that” questions, every step was checked against the definitions.
Spotted a mistake or a neater method? Tell us in the comments below or through Report an error.
Jump to a question (year · question number):
2019 · 5e 2018 · 5e 2017 · 8b 2014 · 7b 2013 · 7a 2012 · 5c 2012 · 7c 2011 · 7c 2010 · 7c 2009 · 7a 2008 · 7b
Draw a flow chart and write a basic algorithm (in FORTRAN/C/C) for evaluating using Trapezoidal rule.
Solution
Trapezoidal rule. With and , with . Algorithm
- Read ; set , , .
- .
- For to : .
- ; print .
Program (C)
/* Trapezoidal rule for y = integral of 1/(1+x*x) from 0 to 6 */
#include <stdio.h>
double f(double x) { return 1.0/(1.0 + x*x); }
int main(void) {
double a = 0.0, b = 6.0, h, s, y;
int n, i;
printf("Number of sub-intervals n: ");
scanf("%d", &n);
h = (b - a)/n;
s = f(a) + f(b);
for (i = 1; i <= n - 1; i++)
s = s + 2.0*f(a + i*h);
y = h/2.0*s;
printf("Integral = %f\n", y);
return 0;
}
With () the program prints ; the exact value is , and larger brings the result closer.
✓ Checked by computer We ran the same loop in Python: gives , gives ; the exact value is .
Write down the basic algorithm for solving the equation : by bisection method, correct to 4 decimal places.
Solution
has and , so a root lies in (and only one, as there). Each bisection halves the bracket while keeping the sign change inside it. The root is known to four decimal places once both ends of the bracket round to the same four-decimal number, so that is our stopping test (it needs at least 14 halvings, since ).
ALGORITHM Bisection for f(x) = x·e^x − 1 = 0
1. Define f(x) = x*exp(x) - 1
2. Read a, b (here a = 0, b = 1)
3. If f(a)*f(b) > 0: print "no sign change", stop
4. Repeat
c = (a + b)/2
if f(c) = 0: go to 6
if f(a)*f(c) < 0 then b = c
else a = c
until round(a, 4) = round(b, 4)
5. c = (a + b)/2
6. Print c rounded to 4 decimals, stop
Trace of the first steps (a ← c when , b ← c when ):
| Step | ||||
|---|---|---|---|---|
| 1 | 0.00000 | 1.00000 | 0.50000 | −0.17564 |
| 2 | 0.50000 | 1.00000 | 0.75000 | 0.58775 |
| 3 | 0.50000 | 0.75000 | 0.62500 | 0.16765 |
| 4 | 0.50000 | 0.62500 | 0.56250 | −0.01278 |
| 5 | 0.56250 | 0.62500 | 0.59375 | 0.07514 |
| 6 | 0.56250 | 0.59375 | 0.57812 | 0.03062 |
| 7 | 0.56250 | 0.57812 | 0.57031 | 0.00878 |
| 8 | 0.56250 | 0.57031 | 0.56641 | −0.00204 |
Continuing, after 17 halvings the bracket is ; both ends round to , so the root is .
✓ Checked by computer We ran this algorithm in Python: it stops after 17 halvings with , , giving ; the exact root is .
Write an algorithm in the form of a flow chart for Newton-Raphson method. Describe the cases of failure of this method.
Solution
Flow chart. is the tolerance, the largest number of iterations allowed, a small number used to guard against division by a near-zero derivative.
Cases where Newton–Raphson fails (or behaves badly).
- Zero or very small derivative. If the step is undefined; if is tiny, is thrown far away (the tangent is nearly horizontal).
- Poor starting value. Far from the root the iterates may diverge or converge to a different root. For example with gives iterates that grow without bound.
- Cycling. The iterates can repeat. For with we get
- Multiple roots. At a root of multiplicity (e.g. ) the convergence is only linear, not quadratic; the modified formula restores quadratic convergence.
- Inflection point or wrong-way curvature near the root (e.g. , where each step doubles the distance from the root: ).
- No real root at all (e.g. ): the iterates wander without settling.
✓ Proof checked step by step We traced the flow chart by hand on , : it reaches in four passes and exits through the convergence test.
Draw a flowchart for Simpson’s one-third rule.
Solution
Simpson’s rule ( even, , ):
✓ Checked by computer We ran the same steps in Python on with : the result is , matching to six decimals.
Develop an algorithm for Newton — Raphson method to solve starting with initial iterate , be the number of iterations allowed, eps be the prescribed relative error and delta be the prescribed lower bound for .
Solution
The Newton–Raphson step is . The algorithm must guard against two things: a derivative too close to zero (the step would blow up), and non-convergence (stop after steps). The stopping test uses the relative error .
ALGORITHM Newton–Raphson for f(x) = 0
Input : x0, n (max. iterations), eps (relative error), delta (lower bound for |f'|)
1. i = 1
2. fd = f'(x0)
3. If |fd| < delta then
print "Derivative too small – method fails", stop
4. x1 = x0 - f(x0)/fd
5. If |x1 - x0| < eps*|x1| then
print "Root =", x1, "after", i, "iterations", stop
6. If i >= n then
print "No convergence in", n, "iterations; last value", x1, stop
7. x0 = x1; i = i + 1; go to step 2
Remarks.
- Step 3 is the “delta” test: if delta the tangent is almost horizontal and the next iterate is unreliable.
- In step 5 we write the test as to avoid dividing by when the root is 0.
- Each pass needs one evaluation of and one of .
✓ Checked by computer We ran this algorithm in Python on with , , eps , delta : it stops after 4 iterations at .
Provide a computer algorithm to solve an ordinary differential equation in the interval for number of discrete points, where the initial value is , using Euler’s method.
Solution
Euler’s method. Divide into equal steps , . Starting from , for . (This follows the tangent line at for one step; the local error is , the global error .)
ALGORITHM Euler's method for dy/dx = f(x, y), y(a) = alpha, on [a, b]
Input : a, b, alpha, n (number of steps, so there are n+1 points)
1. h = (b - a)/n
2. x = a; y = alpha
3. Print x, y
4. For i = 1 to n do
y = y + h*f(x, y) (Euler step: uses the slope at the left end)
x = a + i*h (computed from a to avoid round-off build-up)
Print x, y
end for
5. Stop
A program would define f(x, y) as a separate function so that the same algorithm works for any equation.
✓ Checked by computer We ran the algorithm in Python on , on : with it prints (), and the error falls roughly in proportion to as grows.
In a certain examination, a candidate has to appear for one major and two minor subjects. The rules for declaration of results are : marks for major are denoted by and for minors by and . If the candidate obtains 75% and above marks in each of the three subjects, the candidate is declared to have passed the examination in first class with distinction. If the candidate obtains 60% and above marks in each of the three subjects, the candidate is declared to have passed the examination in first class. If the candidate obtains 50% or above in major, 40% or above in each of the two minors and an average of 50% or above in all the three subjects put together, the candidate is declared to have passed the examination in second class. All those candidates, who have obtained 50% and above in major and 40% or above in minor, are declared to have passed the examination. If the candidate obtains less than 50% in major or less than 40% in any one of the two minors, the candidate is declared to have failed in the examinations. Draw a flow chart to declare the results for the above.
Solution
Logic. The rules overlap (a candidate with 80% everywhere also satisfies the first-class and pass conditions), so we test them from the strictest outcome downwards, after first weeding out failures. Let be the major and the minors, all in percent.
- Fail if or or .
- Otherwise, first class with distinction if .
- Otherwise, first class if .
- Otherwise, second class if the average (the conditions , already hold, since step 1 was passed).
- Otherwise the candidate has passed (simple pass).
✓ Proof checked step by step We traced the chart on test cases: (80, 78, 90) → distinction; (70, 65, 61) → first class; (55, 45, 50) → average 50, second class; (52, 41, 45) → passed; (49, 90, 90) → failed.
Draw a flow chart for Lagrange’s interpolation formula.
Solution
Formula. For points , The outer loop runs over (one term per data point); the inner loop over builds the product for that term.
✓ Proof checked step by step We ran the same steps in Python on the data of the 2009 question (six points): it returns , as obtained there by hand.
(i) Find the hexadecimal equivalent of the decimal number (ii) For the given set of data points write an algorithm to find the value of by using Lagrange’s interpolation formula. (iii) Using Boolean algebra, simplify the following expressions (i) (ii) where represents the complement of .
Solution
(i) Repeated division by 16:
| Divide by 16 | Quotient | Remainder |
|---|---|---|
| 587632 | 36727 | 0 |
| 36727 | 2295 | 7 |
| 2295 | 143 | 7 |
| 143 | 8 | F (15) |
| 8 | 0 | 8 |
(ii) Lagrange’s formula: .
ALGORITHM Lagrange interpolation
Input : n, data points (x[1], f[1]), ..., (x[n], f[n]), the point x
1. sum = 0
2. For i = 1 to n do
term = f[i]
For j = 1 to n do
if j ≠ i then term = term*(x - x[j])/(x[i] - x[j])
end for
sum = sum + term
end for
3. Print "f(x) =", sum
4. Stop
(iii) (a) Using repeatedly: so .
✓ Checked by computer We checked by computer: ; the algorithm, run on the six points of the 2009 interpolation question, returns ; both simplifications were confirmed on all inputs.
Develop an algorithm for Regula-Falsi method to find a root of starting with two initial iterates and to the root such that sign sign . Take as the maximum number of iterations allowed and eps be the prescribed error.
Solution
Idea. If and have opposite signs, the chord joining and cuts the -axis at We keep whichever of , still has a sign change, so the root always stays bracketed.
ALGORITHM Regula-Falsi for f(x) = 0
Input : x0, x1 with sign(f(x0)) ≠ sign(f(x1)), n (max. iterations), eps
1. f0 = f(x0); f1 = f(x1)
2. If f0*f1 > 0 then print "Root not bracketed", stop
3. i = 1; xold = x0
4. x2 = (x0*f1 - x1*f0)/(f1 - f0) (chord meets the x-axis)
5. f2 = f(x2)
6. If |x2 - xold| < eps or f2 = 0 then
print "Root =", x2, "after", i, "iterations", stop
7. If f0*f2 < 0 then (root in [x0, x2])
x1 = x2; f1 = f2
else (root in [x2, x1])
x0 = x2; f0 = f2
8. If i >= n then
print "No convergence in", n, "iterations; last value", x2, stop
9. xold = x2; i = i + 1; go to step 4
✓ Checked by computer We ran this algorithm in Python on with , , , eps : it returns after 13 iterations.
Draw a flow chart for solving equation correct to five decimal places by Newton-Raphson method.
Solution
Newton–Raphson: . For five-decimal accuracy we stop when two successive iterates differ by less than ; caps the number of passes, and the test prevents division by zero.
✓ Proof checked step by step We traced the chart on , : it stops after 4 passes with , which is to five decimals.
More on Numerical Analysis and Computer Programming
- Numerical Analysis and Computer Programming topic-wise PYQs (questions only, for practice)
- Mathematics PYQ analysis: which units carry the marks year by year
- Year-wise question papers
- Mathematics booklist
Questions: UPSC Civil Services (Main) Examination, Mathematics Paper II, 2008–2019. Solutions: Optional Province. Found an error? Tell us.

