Saturday, July 24, 2021

Numbers series and their sum

N terms of Natural numbers series and their sum ( 1^2+2^2 + 3^2+4^2+5^2 + 6^2 ..... n^2)

--------------------------------------------------------------------------------------------------- 

#include <stdio.h>

int main()

{

   int i,n,sum=0;

   printf("Input the number of terms in the series: \n");

   scanf("%d",&n);

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

   {

      sum+=(i)*(i);  //main logic series generation

   }

   printf("\nSum of Series upto %d terms : %d \n",n,sum);

  return 0;

}


------------------------------------------------------------------------------------------------

N terms of even numbers series and their sum ( 2^2 + 4^2 + 6^2 ..... n^2)

--------------------------------------------------------------------------------------------------- 

#include <stdio.h>

int main()

{

   int i,n,sum=0;

   printf("Input the number of terms in the series: \n");

   scanf("%d",&n);

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

   {

      sum+=(2i)*(2i);  //main logic even no's generation

   }

   printf("\nSum of Series upto %d terms : %d \n",n,sum);

  return 0;

}

--------------------------------------------------------------------------------------------------

N terms of odd numbers series and their sum ( 1^2 + 3^2 + 5^2+7^2 ..... n^2)

-----------------------------------------------------------------------------------------------

#include <stdio.h>

int main()

{

   int i,n,sum=0;

   printf("Input the number of terms in the series: \n");

   scanf("%d",&n);

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

   {

      sum+=(2i-1)*(2i-1);  //main logic odd number generation

   }

   printf("\nSum of Series upto %d terms : %d \n",n,sum);

  return 0;

}

No comments:

Post a Comment