-7

I'm beginning with C++. The question is: to write a program to input 20 natural numbers and output the total number of odd numbers inputted using while loop.

Although the logic behind this is quite simple, i.e. to check whether the number is divisible by 2 or not. If no, then it is an odd number.

But, what bothers me is, do I have to specifically assign 20 variables for the user to input 20 numbers?

So, instead of writing cin>>a>>b>>c>>d>>.. 20 variables, can something be done to reduce all this calling of 20 variables, and in cases like accepting 50 numbers?

Paras Shah
  • 105
  • 11
  • 3
    At any time point in the execution the program needs to remember the sum so far. That's one variable. And due to the way the standard library is designed, unless you define your own machinery it needs a variable to hold the current numerical input. That's two variables. And, yes, it needs to remember how many numbers it has input, in order to stop at twenty. That's three variables. – Cheers and hth. - Alf Sep 26 '15 at 02:12
  • So, if I've something like, take 50 inputs and tell the sum of all the numbers, do I have to assign 50 variables to it? That's too much. There should be a simpler way to this.. – Paras Shah Sep 26 '15 at 02:16
  • The "vari" in "variable" means ... what? – Cheers and hth. - Alf Sep 26 '15 at 02:26
  • @ParasShah What do you not understand in the first comment? You don't need 50 variables, only 3. And for stuff where you really need a variable for each thing, use *arrays* – deviantfan Sep 26 '15 at 02:28
  • I don't understand the reason for all these downvotes.. OK. I know, I've asked something which is below newbie level, but as I am at a very beginning stage, these kind of questions are obvious (atleast for me).. – Paras Shah Sep 26 '15 at 15:16

3 Answers3

1

Q. Count total no of odd integer. A.

   #include <iostream>
    using namespace std;
    int main(int argc, char** argv)
    {
     int n,odd=0;
     cout<<"Number of input's\n";
     cin>>n;
     while(n-->0)
     {
      int y;
      cin>>y;
      if(y &1)
      {
        odd+=1;
      }
     }
     cout<<"Odd numbers are "<<odd;
     return 0;
     }
udit043
  • 1,610
  • 3
  • 22
  • 40
0

You can process the input number one by one.

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

If you do really want to save all the input numbers, you can use an array.

int i = 0; // variable for loop control
int a[20]; // array to store all the numbers
int num_of_odds = 0; // variable for output

while (i < 20) {
    cin >> a[i];
    i++;
}

i = 0;
while (i < 20) {
    if (a[i] % 2 == 1) num_of_odds++;
    i++;
}

cout << "there are " << num_of_odds << " odd number(s)." << endl;

Actually, you can also combine the two while-loop just like the first example.

mkvoya
  • 156
  • 1
  • 4
  • This is what exactly I wanted. Your first answer, is the perfect fit, something what I was thinking and trying to do. Never thought it was this damn easy.. Thanks a lot :) – Paras Shah Sep 26 '15 at 16:09
0

Take one input and then process it and then after take another intput and so on.

int n= 20; // number of input
int oddnum= 0; //number of odd number
int input;
for (int i = 0; i < n; i ++){
 cin >> input;
 if (input % 2 == 1) oddnum++;
 }
cout << "Number of odd numbers :"<<oddnum <<  "\n";
Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37