0

hello i want to build a programme that finds even and odd number from given two numbers. when i build it it says succeed but still it says warning C4700: uninitialized local variable 'number' used

when i debug it debug error appeared how to solve this? and can anybody tell me the reason it happens? thank you so much

below is the code

#include<stdio.h>
#include <iostream>
using namespace std;


int main(){

    int number;
    int firstNum,secondNum;

    cout << "Enter the first number: ";
    cin >> firstNum;

   cout << "Enter the second number: ";
    cin >> secondNum;


    if(number % 2 !=0){
    for(number = firstNum;number <= secondNum; number++)
      cout << number<< " ";

    cout << "Odd numbers in given range are: ";
    cout << number<< " ";
    }
    else if(number % 2 ==0){

   for(number = firstNum;number <= secondNum; number++)
     printf("\nEven numbers in given range are: ");

             cout << number << " ";
    }
    return 0;
}

1 Answers1

0

The reason your program gives error is because you haven't initialized your variable number. I have corrected that. There were few other bugs too which would have given wrong output.I have corrected and reformatted your code a bit -

#include<stdio.h>
#include <iostream>
using namespace std;


int main(){

    int number;
    int firstNum,secondNum;

    cout << "Enter the first number: ";
    cin >> firstNum;

   cout << "Enter the second number: ";
    cin >> secondNum;

    number=firstNum;
    for(number = firstNum;number <= secondNum; number++)
      cout << number<< " ";
    //Following piece will print out all odd numbers
    cout << "\nOdd numbers in given range are: ";
    for(number = firstNum;number <= secondNum ; number++)
        if(number&1)
            cout<<number<<" ";
    cout<<"\n";    
    //Following piece will print out all even numbers        
    cout << "Even numbers in given range are: ";
    for(number = firstNum;number <= secondNum ; number++)
        if(!(number&1))
            cout<<number<<" ";
    cout<<"\n";




    return 0;
}

You are probably trying to print the odd numbers and even numbers in a given range. The above program does exactly that.

Hope this solves your question !

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • To avoid confusion, I have used `&` operator to find even or odd. This is less expensive than the normal `%` operator and also more efficient. The `&` operator will only check the last bit of your number. It's odd if it is set, otherwise even. – Abhishek Bhagate May 27 '20 at 14:01
  • Any compiler from this century will replace `% 2` with `& 1` if it's faster on the target platform. Modern compilers are better than humans at optimising code. – molbdnilo May 27 '20 at 14:06
  • @molbdnilo Most compiler probably would, but not in every case. Anyways, it's good to know this way of checking for parity. On your last point, I do agree that I could have just incremented `number` by two instead of checking everytime. – Abhishek Bhagate May 27 '20 at 14:11