Followers

Monday, March 27, 2023

Program To Check If The Number Is Odd Or Not

Program:

 #include <iostream>    
 using namespace std;    
 int main() {  
      int num;  
      cout<<"Enter the number to check: ";  
      cin>>num;  
      if(num%2 != 0){  
           cout<<"The number "<<num<<" is odd!"<<endl;  
      }else{  
           cout<<"The number "<<num<<" is not odd!"<<endl;  
      }  
      return 0;    
 }   
Output:
 In Case Of Odd Number:

In Case Of Non-Odd Number:

Working Of The Program:

1. First, I declare the variable num.
2. Then, I prompt the user to enter a number to check whether it is odd or not using cout and read it in using cin.
3. I check whether the number is odd or not using an if-else statement.
4. If the number is not divisible by 2, i.e., if the remainder of the division operation num%2 is not equal to 0, then the number is odd. In that case, I print out a message saying "The number num is odd!" using cout.
5. If the number is divisible by 2, i.e., if the remainder of the division operation num%2 is equal to 0, then the number is not odd. In that case, I print out a message saying "The number num is not odd!" using cout.
6. Finally, I return 0 to indicate that the program has completed successfully.

No comments:

Post a Comment

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...