Conditional Statements
Conditional Statements
Odd
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0; }
Example: Find maximum value of two variables
#include <iostream>
using namespace std;
int main() {
cout << "Insert two numbers to find the maximum one \n";
double FNum, SNum;
cout << "First Number= ";
cin >> FNum;
cout << "Second Number= ";
cin >> SNum;
cout << "\n";
if (FNum > SNum)
cout << "First Number= " << FNum << " Is the maximum Number\n";
else if (FNum < SNum)
cout << "Second Number= " << SNum << " Is the maximum Number\n";
else
cout << "First Number = Second Number";
return 0;
}
Example: C++ Program to Find Largest Number Among Three
Numbers
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
return 0;
}
Example: C++ Program to read the numbers from 1 to 7 and display
their correspondence day of week using if statement.
#include <iostream>
using namespace std;
int main()
{
int Day_Number;
cin >> Day_Number;
if (Day_Number == 1)
{
cout << " saturday ";
}
else if(Day_Number == 2)
{
cout << " sunday ";
}
else if (Day_Number == 3)
{
cout << " monday ";
}
else if (Day_Number == 4)
{
cout << " Tuerday ";
}
else if (Day_Number == 5)
{
cout << " wednesday ";
}
else if (Day_Number == 6)
{
cout << " thursday ";
}
else if (Day_Number == 7)
{
cout << " friday ";
}
else
{
cout << "error";
}
return 0;
}
case 1:
cout << " saturday ";
break;
case 2: cout << " sunday ";
break;
case 3: cout << " monday ";
break;
case 4: cout << " Tuesday ";
break;
case 5: cout << " wednesday ";
break;
case 6: cout << " thursday ";
break;
case 7: cout << " friday ";
break;
default: cout << "error";
}
}