- 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

10th Nov 2017, 10:38 AM
Stv Nikto
Stv Nikto - avatar
6 ответов
+ 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; }
10th Nov 2017, 10:42 AM
RZK 022
RZK 022 - avatar
+ 2
oh sorry ,I just forgot that .
10th Nov 2017, 11:29 AM
RZK 022
RZK 022 - avatar
+ 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 .
10th Nov 2017, 11:31 AM
RZK 022
RZK 022 - avatar
+ 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;
10th Nov 2017, 10:44 AM
Shadow
Shadow - avatar
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"?
10th Nov 2017, 11:29 AM
Shadow
Shadow - avatar
0
Ah, I see. That else-statement still makes your loop infinite though 😓
10th Nov 2017, 1:03 PM
Shadow
Shadow - avatar