While and do-while loop
Mar 13, 2010 at 4:21am
Hi, could someone show me the codes uses a while loop and do-while loop to obtain and display the sum of odd integer in the range of 11 to 121.
thanks much.
Mar 13, 2010 at 5:09am
while loop can be never be accessed
do while loop always performs till while statement.
1 2 3 4 5 6 7 8 9 10 11
|
int main() {
int num = 11;
int result = 0;
while ( num <= 121){
if (num%2 == 1) {
result = result + num;
}
num ++;
}
cout<<result<<endl;
}
|
1 2 3 4 5 6 7 8 9 10 11
|
int main() {
int num = 11;
int result = 0;
do{
if (num%2 == 1) {
result = result + num;
}
num ++;
} while (num <=121);
cout<<result<<endl;
}
|
Mar 13, 2010 at 5:50am
thanks whoami32
Last edited on Mar 13, 2010 at 6:01am
Topic archived. No new replies allowed.