Write a loop that calculate sum of the n elements of the series 5m Dec2008
Write a loop that calculate sum of the n elements of the series: 5
1+ 7 +13 +19 + 25 + …….
Write the loop in 3 different ways:
(i) using while loop
(ii) using do-while loop
#include<stdio.h>
void main()
{
int i,X,SUM=0;
clrscr();
printf(“\nGENERATED SERIES IS :- \n”);
i=1;
while(i<11)
{
X=i+(5*(i-1));
printf(” %d “,X);
SUM=SUM+X;
i++;
}
printf(“\n\nSUM OF GENERATED SERIES IS :- \t %d \n”,SUM);
getch();
}
code:-
[codesyntax lang=”c”]
#include<stdio.h> void main() { int i,X,SUM=0; clrscr(); printf("\nGENERATED SERIES IS :- \n"); i=1; while(i<11) { X=i+(5*(i-1)); printf(" %d ",X); SUM=SUM+X; i++; } printf("\n\nSUM OF GENERATED SERIES IS :- \t %d \n",SUM); getch(); }
[/codesyntax]
Screenshot:-