- 1
Using while loop Write a c++ program that generate even numbers in range of 10 to 50 and display the numbers and their sum
Someone help plizzzz
6 Réponses
+ 3
#include <iostream>
using namespace std;
int main() {
int sum=0,num=10;
while(num<=50) {
if(num%2==0) {
cout<<num;
sum+=num;
}
else {
cout<<"";
}
cout<<" ";
num++;
}
cout<<sum;
return 0;
}
+ 2
oh sorry ,I just forgot that .
+ 2
actually I had first used a for loop and then just saw the question : "using while loop"
So I forgot to change the rest of the code .
+ 1
//preprocessor, main stuff
int sum = 0, number = 10;
while (number <= 50) {
std::cout << number << std::endl;
sum += number;
number += 2;
}
std::cout << "Sum is: " << sum;
0
@Atikrant Negi
Not only you used an uninitialized "i", which seems pretty pointless to me, you also created an infinite loop by never incrementing neither "i" nor "sum"?
0
Ah, I see.
That else-statement still makes your loop infinite though 😓