Example : A C++ program to check whether the given no. is odd or even. Number is given as per user’s choice.
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
while(choice == 1 )
{
int a;
cout << "Enter a number to check Even or Odd" << endl;
cin >> a;
//check whether number is even or odd
if( a%2 == 0 )
{
cout << "The given number is Even" << endl;
}
else
{
cout << "The given number is Odd" << endl;
}
cout << "Do U Want to check more values : Press 1 for Yes and 0 for No" << endl;
cin >> choice;
}
cout << "I hope you checked all your numbers, Thank You" << endl;
return 0;
}
Output :
Enter a number to check Even or Odd
12
The given number is Even
Do U Want to check more values : Press 1 for Yes and 0 for No
1
Enter a number to check Even or Odd
25
The given number is Odd
Do U Want to check more values : Press 1 for Yes and 0 for No
0
I hope you checked all your numbers, Thank You
Example : A C++ program to check whether the given no. is +ive, -ive or zero.
# include <iostream>
using namespace std;
int main ()
{
int number;
cout << "Enter a numeric value : \n";
cin >> number;
// checks if the number is positive
if ( number > 0)
{
cout << "The entered numeric value is Positive: " << number << endl;
}
// checks if the number is negative
else if ( number < 0)
{
cout << "The entered numeric value is Negative: " << number << endl;
}
else
{
cout << "The entered numeric value is Zero: " << number << endl;
}
return 0;
}
Example : A C++ program to display the largest value among three values.
# include <iostream>
using namespace std;
int main ()
{
int n1, n2, n3;
cout << "Enter three different numbers: ";
cin >> n1 >> n2 >> n3;
if(n1==n2 || n2==n3)
{
cout << "Two numbers are equal : \n" << n2;
}
if(n1>=n2 && n1>=n3)
{
cout << "Largest number is : \n" << n1;
}
if(n2>=n1 && n2>=n3)
{
cout << "Largest number is : \n" << n2;
}
if(n3>=n1 && n3>=n2)
{
cout << "Largest number is : \n" << n3;
}
return 0;
}
------------ OR ------------
# include <iostream>
using namespace std;
int main ()
{
int n1, n2, n3;
cout << "Enter three different numbers: ";
cin >> n1 >> n2 >> n3;
if(n1>n2)
{
if(n1>n3)
{
cout << "Largest number is : \n" << n1;
}
else
{
cout << "Largest number is : \n" << n3;
}
}
else
{
if(n2>n3)
{
cout << "Largest number is : \n" << n2;
}
else
{
cout << "Largest number is : \n" << n3;
}
}
return 0;
}
0 Comments