How to take numerous inputs without assigning variable to each of them in C++? -


i'm beginning c++. question is: write program input 20 natural numbers , output total number of odd numbers inputted using while loop.

although logic behind quite simple, i.e. check whether number divisible 2 or not. if no, odd number.

but, bothers me is, have assign 20 variables user input 20 numbers?

so, instead of writing cin>>a>>b>>c>>d>>.. 20 variables, can done reduce calling of 20 variables, , in cases accepting 50 numbers?

you can process input number 1 one.

int = 0; // variable loop control int num_of_odds = 0; // variable output while (i < 20) {     int a;     cin >> a;     if (a % 2 == 1) num_of_odds++;     i++; } cout << "there " << num_of_odds << " odd number(s)." << endl; 

if want save input numbers, can use array.

int = 0; // variable loop control int a[20]; // array store numbers int num_of_odds = 0; // variable output  while (i < 20) {     cin >> a[i];     i++; }  = 0; while (i < 20) {     if (a[i] % 2 == 1) num_of_odds++;     i++; }  cout << "there " << num_of_odds << " odd number(s)." << endl; 

actually, can combine 2 while-loop first example.


Comments