In this tutorial we will learn how to write a program that print sum of series 1^2+ 2^2+ 3^2+ 4^2+.........n^2.
C program code
#include <stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,num,sum=0;
printf("\n Enter a number:\n ");
scanf("%d",&num);
for(a=1;a<=num;a++)
{
sum+=pow(a,2);
}
printf("\n Sum of series:\n ");
for(a=1;a<=num;a++)
{
if(a!=num)
{
printf("%d^2+ ",a);
}
else
{
printf("%d^2 = %d\n",a,sum);
}
}
getch();
}
k
OUTPUT:
In this output the value of entered number is 15
And the series as
Sum of series:
1^2+ 2^2+ 3^2+ 4^2+ 5^2+ 6^2+ 7^2+ 8^2+ 9^2+ 10^2+ 11^2+ 12^2+ 13^2+ 14^2+ 15^2
And the sum of this series is 1240
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.
Created by-- HARSH CHAUHAN
No comments:
Post a Comment