0% found this document useful (0 votes)
3 views

Conditional Statements

The document provides several examples of C++ programs that demonstrate basic programming concepts such as checking if a number is even or odd, finding the maximum of two or three numbers, and displaying the corresponding day of the week based on a number input. Each example includes code snippets and uses different control structures like if statements and switch cases. These examples serve as practical illustrations for beginners learning C++.

Uploaded by

johnddd19090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Conditional Statements

The document provides several examples of C++ programs that demonstrate basic programming concepts such as checking if a number is even or odd, finding the maximum of two or three numbers, and displaying the corresponding day of the week based on a number input. Each example includes code snippets and uses different control structures like if statements and switch cases. These examples serve as practical illustrations for beginners learning C++.

Uploaded by

johnddd19090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Example: C++ Program to Check Whether Number is Even or

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;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;
if ((n1 >= n2) && (n1 >= n3))
cout << "Largest number: " << n1;

else if ((n2 >= n1) && (n2 >= n3))

cout << "Largest number: " << n2;


else
cout << "Largest number: " << 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;
}

Example: C++ Program to read the numbers from 1 to 7 and display


their correspondence day of week using case statement.
#include <iostream>
using namespace std;
int main()
{
int Day_Number;
cout << "inter the number of day";
cin>> Day_Number;
switch (Day_Number)
{

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";
}
}

You might also like