Menu Close

Algorithms and flow charts: UPSC Maths Optional PYQ Solutions (2008–2019)

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

Cover of the UPSC Prelims Topic-wise PYQ book with detailed explanations

From the Optional Province team

Preparing for Prelims too?

Our UPSC Prelims Topic-wise PYQ Book covers every Prelims question with detailed explanations, so you can see exactly where UPSC asks from. Now available as a hard copy and an eBook.

Buy Hard Copy on AmazonBuy eBook (PDF)

Download a free sample

Draw a flow chart and write a basic algorithm (in FORTRAN/C/C++) for evaluating y=∫06dx1+x2 using Trapezoidal rule.

2019 · Paper II · Q5e · 10 marks

Solution

Trapezoidal rule. With h=(b−a)/nh=(b-a)/n and xi=a+ihx_i=a+ih, y≈h2[f(x0)+f(xn)+2(f(x1)+⋯+f(xn−1))],\begin{aligned}y&\approx\frac h2\big[f(x_0)+f(x_n)\\ &\quad+2\left(f(x_1)+\cdots+f(x_{n-1})\right)\big],\end{aligned} with f(x)=11+x2f(x)=\dfrac{1}{1+x^2}. Algorithm

  1. Read nn; set a=0a=0, b=6b=6, h=(b−a)/nh=(b-a)/n.
  2. s←f(a)+f(b)s\leftarrow f(a)+f(b).
  3. For i=1i=1 to n−1n-1: s←s+2f(a+ih)s\leftarrow s+2f(a+ih).
  4. y←h2sy\leftarrow \tfrac h2\,s; print yy.
StartRead a = 0, b = 6, nh ← (b − a)/ns ← f(a) + f(b), i ← 1s ← s + 2·f(a + i·h)i ← i + 1i ≤ n − 1 ?NoYesy ← (h/2)·sPrint yStop
Trapezoidal rule, f(x) = 1/(1 + x²)

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 n=6n=6 (h=1h=1) the program prints y≈1.4108y\approx1.4108; the exact value is tan−16≈1.4056\tan^{-1}6\approx1.4056, and larger nn brings the result closer.

✓ Checked by computer We ran the same loop in Python: n=6n=6 gives 1.4107991.410799, n=60n=60 gives 1.4056401.405640; the exact value is tan−16=1.405648\tan^{-1}6=1.405648.

↑ Back to the question list

Write down the basic algorithm for solving the equation : xex−1=0 by bisection method, correct to 4 decimal places.

2018 · Paper II · Q5e · 10 marks

Solution

f(x)=xex−1f(x)=xe^x-1 has f(0)=−1<0f(0)=-1<0 and f(1)=e−1>0f(1)=e-1>0, so a root lies in [0,1][0,1] (and only one, as f′(x)=(1+x)ex>0f'(x)=(1+x)e^x>0 there). Each bisection halves the bracket [a,b][a,b] 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 2−14<10−42^{-14}<10^{-4}).

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 f(c)<0f(c)<0, b ← c when f(c)>0f(c)>0):

Step aa bb cc f(c)f(c)
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 [0.567139,0.567146][0.567139,\ 0.567146]; both ends round to 0.56710.5671, so the root is x≈0.5671x\approx0.5671.

✓ Checked by computer We ran this algorithm in Python: it stops after 17 halvings with a=0.567139a=0.567139, b=0.567146b=0.567146, giving x=0.5671x=0.5671; the exact root is 0.567143…0.567143\ldots.

↑ Back to the question list

Write an algorithm in the form of a flow chart for Newton-Raphson method. Describe the cases of failure of this method.

2017 · Paper II · Q8b · 15 marks

Solution

Flow chart. ε\varepsilon is the tolerance, NN the largest number of iterations allowed, δ\delta a small number used to guard against division by a near-zero derivative.

StartRead x₀, ε, Ni ← 1|f′(x₀)| < δ ?Yesf′ ≈ 0: methodfails. StopNox₁ ← x₀ − f(x₀)/f′(x₀)|x₁ − x₀| < ε ?YesPrint x₁StopNoi ≥ N ?YesNo convergencein N steps. StopNox₀ ← x₁, i ← i + 1
Flow chart of the Newton–Raphson method

Cases where Newton–Raphson fails (or behaves badly).

  1. Zero or very small derivative. If f′(xn)=0f'(x_n)=0 the step is undefined; if f′(xn)f'(x_n) is tiny, xn+1x_{n+1} is thrown far away (the tangent is nearly horizontal).
  2. Poor starting value. Far from the root the iterates may diverge or converge to a different root. For example f(x)=tan−1xf(x)=\tan^{-1}x with |x0|>1.3917|x_0|>1.3917 gives iterates that grow without bound.
  3. Cycling. The iterates can repeat. For f(x)=x3−2x+2f(x)=x^3-2x+2 with x0=0x_0=0 we get x1=1,x2=0,x3=1,…x_1=1,\ x_2=0,\ x_3=1,\dots
  4. Multiple roots. At a root of multiplicity m>1m>1 (e.g. f(x)=(x−1)2f(x)=(x-1)^2) the convergence is only linear, not quadratic; the modified formula xn+1=xn−mf(xn)/f′(xn)x_{n+1}=x_n-m\,f(x_n)/f'(x_n) restores quadratic convergence.
  5. Inflection point or wrong-way curvature near the root (e.g. f(x)=x1/3f(x)=x^{1/3}, where each step doubles the distance from the root: xn+1=−2xnx_{n+1}=-2x_n).
  6. No real root at all (e.g. x2+1=0x^2+1=0): the iterates wander without settling.

✓ Proof checked step by step We traced the flow chart by hand on f(x)=x2−2f(x)=x^2-2, x0=1x_0=1: it reaches 1.4142141.414214 in four passes and exits through the convergence test.

↑ Back to the question list

Draw a flowchart for Simpson’s one-third rule.

2014 · Paper II · Q7b · 15 marks

Solution

Simpson’s 13\tfrac13 rule (nn even, h=(b−a)/nh=(b-a)/n, xi=a+ihx_i=a+ih): ∫abfdx≈h3[f0+fn+4(f1+f3+⋯+fn−1)+2(f2+f4+⋯+fn−2)].\begin{aligned}\int_a^bf\,dx&\approx\frac h3\big[f_0+f_n\\ &\quad+4(f_1+f_3+\cdots+f_{n-1})\\ &\quad+2(f_2+f_4+\cdots+f_{n-2})\big].\end{aligned}

StartRead a, b, n (n even)h ← (b − a)/ns ← f(a) + f(b), i ← 1i odd ?Yess ← s + 4·f(a + ih)Nos ← s + 2·f(a + ih)i ← i + 1i ≤ n − 1 ?NoYesI ← (h/3)·sPrint IStop
Simpson’s ⅓ rule: odd-numbered ordinates get weight 4, even-numbered interior ordinates weight 2

✓ Checked by computer We ran the same steps in Python on ∫01dx1+x2\int_0^1\frac{dx}{1+x^2} with n=10n=10: the result is 0.7853980.785398, matching π/4\pi/4 to six decimals.

↑ Back to the question list

Develop an algorithm for Newton — Raphson method to solve f(x)=0 starting with initial iterate x0, n be the number of iterations allowed, eps be the prescribed relative error and delta be the prescribed lower bound for f′(x).

2013 · Paper II · Q7a · 20 marks

Solution

The Newton–Raphson step is xi+1=xi−f(xi)f′(xi)x_{i+1}=x_i-\dfrac{f(x_i)}{f'(x_i)}. The algorithm must guard against two things: a derivative too close to zero (the step would blow up), and non-convergence (stop after nn steps). The stopping test uses the relative error |xi+1−xi|/|xi+1|<eps|x_{i+1}-x_i|/|x_{i+1}|<\text{eps}.

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 |f′(x)|<|f'(x)|< delta the tangent is almost horizontal and the next iterate is unreliable.
  • In step 5 we write the test as |x1−x0|<eps|x1||x_1-x_0|<\text{eps}\,|x_1| to avoid dividing by x1x_1 when the root is 0.
  • Each pass needs one evaluation of ff and one of f′f'.

✓ Checked by computer We ran this algorithm in Python on f(x)=x3−2x−5f(x)=x^3-2x-5 with x0=2x_0=2, n=20n=20, eps =10−6=10^{-6}, delta =10−6=10^{-6}: it stops after 4 iterations at x=2.0945515x=2.0945515.

↑ Back to the question list

Cover of the UPSC Mains Topic-wise and Year-wise PYQs book (2013–2026)

From the Optional Province team

Strengthen your GS papers

Our UPSC Mains Topic-wise & Year-wise PYQ Book (2013–2026) maps every GS question to the syllabus, so you know what UPSC asks and how often. Now available as a hard copy and an eBook.

Buy Hard Copy on AmazonBuy eBook (PDF)

Download a free sample

Provide a computer algorithm to solve an ordinary differential equation dydx=f(x,y) in the interval [a,b] for n number of discrete points, where the initial value is y(a)=α, using Euler’s method.

2012 · Paper II · Q5c · 12 marks

Solution

Euler’s method. Divide [a,b][a,b] into nn equal steps h=(b−a)/nh=(b-a)/n, xi=a+ihx_i=a+ih. Starting from y0=αy_0=\alpha, yi+1=yi+hf(xi,yi),y_{i+1}=y_i+h\,f(x_i,y_i), for i=0,1,…,n−1i=0,1,\dots,n-1. (This follows the tangent line at (xi,yi)(x_i,y_i) for one step; the local error is O(h2)O(h^2), the global error O(h)O(h).)

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 y′=yy'=y, y(0)=1y(0)=1 on [0,1][0,1]: with n=10n=10 it prints y(1)≈2.593742y(1)\approx2.593742 (=1.110=1.1^{10}), and the error falls roughly in proportion to hh as nn grows.

↑ Back to the question list

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 M1 and for minors by M2 and M3. 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.

2012 · Paper II · Q7c · 20 marks

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 M1M_1 be the major and M2,M3M_2,M_3 the minors, all in percent.

  1. Fail if M1<50M_1<50 or M2<40M_2<40 or M3<40M_3<40.
  2. Otherwise, first class with distinction if M1,M2,M3≥75M_1,M_2,M_3\ge75.
  3. Otherwise, first class if M1,M2,M3≥60M_1,M_2,M_3\ge60.
  4. Otherwise, second class if the average (M1+M2+M3)/3≥50(M_1+M_2+M_3)/3\ge50 (the conditions M1≥50M_1\ge50, M2,M3≥40M_2,M_3\ge40 already hold, since step 1 was passed).
  5. Otherwise the candidate has passed (simple pass).
StartRead M1, M2, M3(percentages)M1 < 50 or M2 < 40or M3 < 40 ?YesPrint"Failed". StopNoM1, M2, M3all ≥ 75 ?YesPrint "First classwith distinction"NoM1, M2, M3all ≥ 60 ?YesPrint"First class"NoAvg ← (M1 + M2 + M3)/3Avg ≥ 50 ?YesPrint"Second class"NoPrint "Passed"Stop
Declaring the result (each “Print …” box on the right ends the run)

✓ 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.

↑ Back to the question list

Draw a flow chart for Lagrange’s interpolation formula.

2011 · Paper II · Q7c · 20 marks

Solution

Formula. For nn points (xi,yi)(x_i,y_i), f(x)≈∑i=1nyi∏j=1,j≠inx−xjxi−xj.f(x)\approx\sum_{i=1}^{n}y_i\prod_{j=1,\,j\ne i}^{n}\frac{x-x_j}{x_i-x_j} . The outer loop runs over ii (one term per data point); the inner loop over jj builds the product for that term.

StartRead n, (xᵢ, yᵢ) for i = 1..n,and the point xs ← 0, i ← 1t ← yᵢ, j ← 1If j ≠ i:t ← t·(x − xⱼ)/(xᵢ − xⱼ)j ← j + 1j ≤ n ?NoYess ← s + t, i ← i + 1i ≤ n ?NoYesPrint s (= f(x))Stop
Lagrange interpolation: the inner loop builds each term, the outer loop adds them

✓ Proof checked step by step We ran the same steps in Python on the data of the 2009 question (six points): it returns f(3)=10f(3)=10, as obtained there by hand.

↑ Back to the question list

(i) Find the hexadecimal equivalent of the decimal number (587632)10 (ii) For the given set of data points (x1,f(x1)),(x2,f(x2)),……,(xn,f(xn)) write an algorithm to find the value of f(x) by using Lagrange’s interpolation formula. (iii) Using Boolean algebra, simplify the following expressions (i) a+a′b+a′b′c+a′b′c′d+…… (ii) x′y′z+yz+xz where x′ represents the complement of x.

2010 · Paper II · Q7c · 20 marks

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

(587632)10=(8F770)16.(587632)_{10}=(8F770)_{16} .

(ii) Lagrange’s formula: f(x)≈∑i=1nf(xi)∏j≠ix−xjxi−xjf(x)\approx\displaystyle\sum_{i=1}^{n}f(x_i)\prod_{j\ne i}\frac{x-x_j}{x_i-x_j}.

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 p+p′q=p+qp+p'q=p+q repeatedly: a+a′b=a+ba+b+a′b′c=a+b+(a+b)′c=a+b+ca+b+c+a′b′c′d=a+b+c+d,…\begin{aligned}a+a'b&=a+b\\ a+b+a'b'c&=a+b+(a+b)'c\\ &=a+b+c\\ a+b+c+a'b'c'd&=a+b+c+d,\ \dots\end{aligned} so a+a′b+a′b′c+a′b′c′d+⋯=a+b+c+d+⋯a+a'b+a'b'c+a'b'c'd+\cdots=a+b+c+d+\cdots.

  1. x′y′z+yz+xz=z(x′y′+y+x)=z(x′+y+x)=z⋅1=z.\begin{aligned}x'y'z+yz+xz&=z(x'y'+y+x)\\ &=z(x'+y+x)\\ &=z\cdot1=z .\end{aligned}

✓ Checked by computer We checked by computer: 587632=(8F770)16587632=(8F770)_{16}; the algorithm, run on the six points of the 2009 interpolation question, returns f(3)=10f(3)=10; both simplifications were confirmed on all inputs.

↑ Back to the question list

Develop an algorithm for Regula-Falsi method to find a root of f(x)=0 starting with two initial iterates x0 and x1 to the root such that sign (f(x0))≠ sign (f(x1)). Take n as the maximum number of iterations allowed and eps be the prescribed error.

2009 · Paper II · Q7a · 30 marks

Solution

Idea. If f(x0)f(x_0) and f(x1)f(x_1) have opposite signs, the chord joining (x0,f(x0))(x_0,f(x_0)) and (x1,f(x1))(x_1,f(x_1)) cuts the xx-axis at x2=x0f(x1)−x1f(x0)f(x1)−f(x0).x_2=\frac{x_0f(x_1)-x_1f(x_0)}{f(x_1)-f(x_0)} . We keep whichever of [x0,x2][x_0,x_2], [x2,x1][x_2,x_1] 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
StartRead x₀, x₁, n, epsf₀ ← f(x₀), f₁ ← f(x₁)i ← 1, x_old ← x₀x₂ ← (x₀f₁ − x₁f₀)/(f₁ − f₀)f₂ ← f(x₂)|x₂ − x_old| < eps ?YesPrint x₂StopNof₀·f₂ < 0 ?Yesx₁ ← x₂f₁ ← f₂Nox₀ ← x₂, f₀ ← f₂i ≥ n ?YesNo convergenceStopNox_old ← x₂, i ← i + 1
Flow chart of the regula-falsi method

✓ Checked by computer We ran this algorithm in Python on f(x)=x3−2x−5f(x)=x^3-2x-5 with x0=2x_0=2, x1=3x_1=3, n=50n=50, eps =10−6=10^{-6}: it returns x=2.094551x=2.094551 after 13 iterations.

↑ Back to the question list

Draw a flow chart for solving equation F(x)=0 correct to five decimal places by Newton-Raphson method.

2008 · Paper II · Q7b · 30 marks

Solution

Newton–Raphson: xn+1=xn−F(xn)F′(xn)x_{n+1}=x_n-\dfrac{F(x_n)}{F'(x_n)}. For five-decimal accuracy we stop when two successive iterates differ by less than ε=0.5×10−5\varepsilon=0.5\times10^{-5}; NN caps the number of passes, and the test F′=0F'=0 prevents division by zero.

StartRead x₀, N(ε = 0.000005)i ← 1F ← F(x₀), D ← F′(x₀)D = 0 ?YesPrint "F′ = 0"StopNox₁ ← x₀ − F/D|x₁ − x₀| < ε ?YesPrint x₁ to5 decimals. StopNoi ≥ N ?YesNo convergenceStopNox₀ ← x₁, i ← i + 1
Newton–Raphson, stopping when successive iterates differ by less than 0.000005

✓ Proof checked step by step We traced the chart on F(x)=x2−3F(x)=x^2-3, x0=2x_0=2: it stops after 4 passes with 1.732051.73205, which is 3\sqrt3 to five decimals.

↑ Back to the question list

More on Numerical Analysis and Computer Programming

Questions: UPSC Civil Services (Main) Examination, Mathematics Paper II, 2008–2019. Solutions: Optional Province. Found an error? Tell us.

Spotted an error? Tell us.

We classify every PYQ with care, but mistakes can slip in. If a question is in the wrong topic, has a typo, the wrong year, or is missing, let us know. We review every report and update the page.

Report an Error
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted