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

Wednesday, 15 April 2020

C++ Question - Even Odd Number

Q: Write a program to check whether the given number is even or odd (using ? : ternary operator )

Solution:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
 int a;
 cout<<"Enter the Number : ";
 cin>>a;
 (a%2==0)?cout<<"Number is even":cout<<"Number is odd";
 return 0;
}
 
Screenshot: 


C++ Program - Swap Variables

Q: Write a program to swap the values of two variables. 
Solution:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
 int a,b,temp;
 cout<<"\nEnter two numbers : ";
 cin>>a>>b;
 temp=a; 
 a=b;
 b=temp;
 cout<<"\nAfter swapping numbers are : ";
 cout<<a<<" "<<b;
 return 0;
}

Screenshot: