dmilicev

1+11+111+1111+....c

Aug 12th, 2020 (edited)
175
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     1+11+111+1111+....c
  4.  
  5.     https://web.facebook.com/photo/?fbid=2725046141108551&set=gm.1443761712477398
  6.  
  7.     https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-26.php
  8.     https://www.geeksforgeeks.org/find-sum-series-1111111111-upto-n-terms/
  9.     https://www.codesansar.com/c-programming-examples/sum-of-1-11-111-1111-up-to-n-terms-using-recursive-function.htm
  10.  
  11.     You can find all my C programs at Dragan Milicev's pastebin:
  12.  
  13.     https://pastebin.com/u/dmilicev
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19. int main(void)
  20. {
  21.     int n,i;                // n is the number of terms
  22.     long sum=0;             // total sum
  23.     long int addition=1;    // addition is term
  24.  
  25.     printf("\n Input the number of terms n = ");
  26.     scanf("%d",&n);
  27.     printf("\n");
  28.  
  29.     for(i=1; i<=n; i++)
  30.     {
  31.         printf(" %ld ", addition);
  32.  
  33.         if (i<n)
  34.           printf("+");
  35.         else
  36.           printf("=");
  37.  
  38.         sum = sum + addition;
  39.         addition = addition*10 + 1;
  40.     }
  41.  
  42.     printf(" %ld \n", sum);
  43.  
  44.     return 0;
  45. }
  46.  
RAW Paste Data Copied