i have this code and its output is 54. my question is, can someone please explain to me the process of getting this certain output, which is said to be 54. thank you. :)

``````#include<stdio.h>
#include<conio.h>

int sumS (int n, int m);

void main ()
{
int x=2, y=5;
clrscr();
printf("%d", sumS(x, y));
getch();
}

int sumS (int n, int m)
{
if (n==m)
return n*m;
else
return (n*n) + sumS (n+1, m)
}``````

if you understand Recursion then it's quite easy to understand this program..

return n*n + n*n + n*n + n*m
or
return 2*2 + 3*3 + 4*4 + 5*5

Those are not formula ..
U need to understand program flow, how actually program runs and how decisions are made in its way ..
Hope Vinit 'll agree with me ...

## All 7 Replies

if you understand Recursion then it's quite easy to understand this program..

return n*n + n*n + n*n + n*m
or
return 2*2 + 3*3 + 4*4 + 5*5

thank you. :) i think i understand now.

@vinitmittal2008 but where did you get those? (the idea of the formula)

Those are not formula ..
U need to understand program flow, how actually program runs and how decisions are made in its way ..
Hope Vinit 'll agree with me ...

oh okay. so is that the way recursion should process? thank u

Yup ..
Go on checking step by step how the program flows..

x=2 and y=5

When main calls sumS
n=2 and m=5
which are not equal
so else block executed
which returns n*n + again call to sumS with 3 and 5
3 and 5 are not equal so (n+1)*(n+1) + again call to sumS with 4 and 5 and so on
Which ends when 5=5 with returning only 5*5

so finally your result will contain

n*n + (n+1)*(n+1) + (n+2)*(n+2) and so on till n=m ...

2*2 + 3*3 + 4*4 + 5*5 = 54 ...

Hope its clear

Yup ..
Go on checking step by step how the program flows..

x=2 and y=5

When main calls sumS
n=2 and m=5
which are not equal
so else block executed
which returns n*n + again call to sumS with 3 and 5
3 and 5 are not equal so (n+1)*(n+1) + again call to sumS with 4 and 5 and so on
Which ends when 5=5 with returning only 5*5

so finally your result will contain

n*n + (n+1)*(n+1) + (n+2)*(n+2) and so on till n=m ...

2*2 + 3*3 + 4*4 + 5*5 = 54 ...

Hope its clear

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.