While and do-while loop
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.
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;
}
|
thanks whoami32
Last edited on
Topic archived. No new replies allowed.