Programming Examples
Write a program to find the sum of the following series
Write a program to find the sum of the following series
/* 1. 1+2+3+..n */
/* 2. 12+22+32+..n2 */
Solution
# include <stdio.h>
# include <conio.h>
void main()
{
int sum=0,ssum=0,i,j;
clrscr();
printf(“Enter Number :”);
scanf(“%d”, &j);
clrscr();
printf(“ Numbers:”);
for(i=1;i<=j;i++)
printf(“%5d”,i);
printf(“\n\nSquares:”);
for(i=1;i<=j;i++)
{
printf(“%5d”,i*i);
sum=sum+i;
ssum=ssum+i*i;
}
printf(“\n\n Sum of Numbers from 1 to %d :%d ”,j, sum);
printf(“\n Sum of Squares of 1 to %d Numbers :%d” ,j, ssum);
}
Output
OUTPUT:
Enter Number: 5
Numbers: 1 2 3 4 5
Squares: 1 4 9 16 25
Sum of Numbers from 1 to 5: 15
Sum of Squares of 1 to 5 Numbers: 55