Illustrate it
summing the series 2+4+6+......to n terms using
(i) while loop (ii) do-while loop
Answer Posted / raju kalyadapu
i). using while-loop
int main()
{
int sum=0,n,i=0,j=2;
printf("Enter 2+4+6+...n upto nth term:");
scanf("%d",&n);
while(i++<n)
{
j=2*i;
sum=sum+i*2;
}
getch();
return 0;
}
ii). using do-while loop
int main()
{
int sum=0,n,i=0,j=2;
printf("Enter 2+4+6+...n upto nth term:");
scanf("%d",&n);
do
{
j=2*i;
sum=sum+i*2;
} while(i++<n);
printf("
Sum of the Series 2+4+6+----%d is:%d",j,sum);
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
What is the use of volatile?
How can you invoke another program from within a C program?
Between macros and functions,which is better to use and why?
How can I get the current date or time of day in a c program?
Why c is faster than c++?
What is cohesion and coupling in c?
Is void a keyword in c?
Is there a way to switch on strings?
Write a program to print “hello world” without using semicolon?
What's the difference between constant char *p and char * constant p?
Define C in your own Language.
What is c system32 taskhostw exe?
Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.
What is the advantage of c?
using only #include