Sum of 1+11+111+1111 ... up to n terms using recursive function

Program


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

/* Function Prototype */
int recSum(int n);

/* Main Function */
int main()
{
 int n, res;
 clrscr();
 printf("Enter number of terms:\n");
 scanf("%d", &n);
 
 /* Function Call */
 res = recSum(n);
 printf("Required sum = %d", res);
 getch();
 return 0;
}

/* Function Definition */
int recSum(int n)
{
 if(n<=0)
 {
  return 0;
 }
 else
 {
  return (pow(10,n)-1)/9 + recSum(n-1);
 }
}

Output of above program:

Enter number of terms:
4 ↲
Required sum = 1234 


Note: ↲ indicates ENTER is pressed.