adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+......+ N) for a given integer N.

Write a C program to sum the series
    1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+......+ N)   for a given integer N.



#include<stdio.h>
#include<conio.h>
 void main()
    {
  int i,sum=1,j,n;
  printf("Enter a number:-");
  scanf("%d",&n);
  printf(" 1 +");
  for(i=2;i<=n;i++)
   {
  printf(" (");
   for(j=1;j<=i;j++)
    {
      sum+=j;
  if(j==i)
     printf(" %d ",j);
       else
      printf(" %d +",j);
     }
         if(i==n)
      printf(" )");
   else
   printf(" ) +");
  }
    printf("  Sum =%d",sum);
  getch();
 }
Output:-





Post a Comment

0 Comments