Followers

Showing posts with label even number. Show all posts
Showing posts with label even number. Show all posts

Monday, March 27, 2023

Program To Check If The Number Is Even 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 even!"<<endl;  
      }else{  
           cout<<"The number "<<num<<" is not even!"<<endl;  
      }  
      return 0;    
 }   
Output:
In Case Of Even Number:

In Case Of Non-Even Number:

Working Of The Program:

1. Then, I prompt the user to enter a number to check whether it is even or not using cout. 2. I read in the user input using cin and store it in the variable num. 3. I check if the number is even using an if-else statement and the modulo operator %. 4. If the remainder of num divided by 2 is 0, then num is even. 5. In that case, I print out a message using cout that says "The number [num] is even!", where [num] is replaced with the value of num. 6. If the remainder of num divided by 2 is not 0, then num is odd. 7. In that case, I print out a message using cout that says "The number [num] is not even!", where [num] is replaced with the value of num. 8. Finally, I return 0 to indicate that the program has completed successfully.

Program to Find the Length of a String

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