C programming.. for loop?

2009-07-13 12:24 pm
Write a for loop that will produce the following output on the screen:

2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110

You have to use comma operator in your for loop and the sum has to be calculated by your loop too.


anyone can give me some hints to write the code?? i really don't have any idea of it =(
thanks !!

回答 (10)

2009-07-14 8:19 am
✔ 最佳答案
Most contributions are either over complicated and or miss the requirements.

Douzo has provided a good solution, except that his loop will be not evaluate for i == 20

The conditional test needs to be i <= 20
2009-07-13 12:43 pm
int i, sum;
for (i=2,sum=i;i<20;i+=2,sum+=i)
    printf("%d + ", i);
printf("%d = %d", i, sum);
2009-07-13 12:34 pm
n=2
str = n.tostring

for i=1 to 10
n=n+2
str = str & " + " & n.tostring
next
str = str & " = 110"

console.writeln(str)

PS: Not C code, but you get the general idea
2016-05-25 7:06 am
Sorry. You should not develop such an impressions. It is an individuals preference to use i, j as loop control variables. It all depends upon the loop structure. for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) Here, i loop runs for n-1 times while j loop runs for n-1-i times. That is, when i is 0, inner loop runs for n-1 times, when i is 1 it runs for n-2 times .....when i value is n-2 inner loop runs for 1 time
2009-07-16 8:03 pm
That is a straight forward one
#include <stdio.h>
void main (void)
{int loop, sum=0;
for (loop =2; loop >=20; loop+=2)
{ sum+=loop;
printf ("%d + ",loop);
}
printf (" = %d \n",sum);
return;
}
2009-07-15 7:01 pm
I don't know what a comma operator is.
do you mean common?

this for loop will display that result to your screen:

for (int i = 0; i < 1; ++i)
{
cout << "2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110" << endl;
}

just remember to #include <iostream>

or alternatively you could do:

int sum = 0; int i;
for (i = 2; i < 20; i = i + 2)
{
sum += i;
cout << i << " + ";
}
cout << i << " = " << (sum += i) << endl;
2009-07-13 1:10 pm
int index;
int next;
int sum = 0;

for (index = 0, next = 0;
index < 10;
next += 2, sum += next, index++, printf("%d", next) )
{
if( index > 0 && index < 10)
{
printf(" + ");
}

}

printf(" = %d\n",sum);



I think this is what she needs, uses the comma operator and calculates the sum in the loop, Though Douzo Yoroshiku's method is better.
This kinda thing is rarely ever done...
2009-07-13 12:44 pm
include <stdio.h>

int main()
{
const int LIMIT_VALUE = 10;

int sum = 0;
for ( int loopNumber=1; loopNumber < LIMIT_VALUE+1 ; loopNumber++ )
{
int currentNumber = 2*loopNumber;
printf("%d", currentNumber);
sum = sum + currentNumber;

if (loopNumber < LIMIT_VALUE)
{
printf(" + ");
}
else
{
printf(" = ");
}
}
printf("%d", sum);

/* wait on a keypress - prevents the console disappearing on termination*/
int ch;
do
{
ch = fgetc ( stdin );
}
while ( ch != EOF && ch != '\n' );

return 0;
}
2009-07-13 12:37 pm
include<studio.h>
#include<conio.h>
void main()
int i=0,j=0,k=0;
for (i>=0;i>=10;i++)
{
j=j+2;
k=k+j;
}
getch();
}
if you are satisfy with my answar than pls give me reply.
參考: yahoo.com
2009-07-13 12:35 pm
include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,I;
for(i=2;i<=20;i=i+2)
sum=sum+i;
printf("%d",sum);
getch();
}


收錄日期: 2021-05-01 09:44:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090713042417AAkQa6T

檢視 Wayback Machine 備份