1+2+1=4 1+2+3+2+1=9 1+2+3+4+3+2+1=16 but I just haven\'t figured out to make tha
ID: 3879830 • Letter: 1
Question
1+2+1=4
1+2+3+2+1=9
1+2+3+4+3+2+1=16
but I just haven't figured out to make that functional using recursive functions
Explanation / Answer
Main.c
#include <stdio.h>
unsigned int square(unsigned int n)
{
//if the number is 1 simply return 1
if(n==1)
return 1;
else //add the number and the number previous to it
//so that the number except the given number whose
// square is to be computed is added twice along with
//return value of recursive function call with parameter
//having the previous number(n-1)
return n+(n-1)+square(n-1);
}
/* To test the square function */
int main()
{
unsigned int res,in;
scanf("%u",&in);
res=square(in);
printf("Hello World: %u",res);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.