20_10

Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

Source Code:

 //Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms

#include <stdio.h>

int main()

{

  int n, i; /* n is number of terms as input and i is a variable*/

  long sum=0; /* sum is a variable to store the sum and give as output */

  printf("Enter the n th term: ");

  scanf("%d",&n); /*Input the number of terms */


int k=0;

for(i=0;i<n;i++)

{

k=(k*10)+1;

sum=sum+k;

}

printf("%ld",sum);

return 0;

}

Output:

Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms




No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...