Examples of if else in c++ programming
All Contributions
Example of how to Write a Program to print positive number entered by the user If the user enters a negative number, it is skipped:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if (number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
Example of write a program to know the day of the week :
#include<iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the number from 1 to 7 to know what is this day: ";
cin >> x;
if (x == 1)
cout << "sunday";
else if (x == 2)
cout << "monday";
else if (x == 3)
cout << "tuesday";
else if (x == 4)
cout << "wednesday";
else if (x == 5)
cout << "thursday";
else if (x == 6)
cout << "friday";
else if (x == 7)
cout << "saturday";
return 0;
}
Example :
Find the biggest number of 3 numbers
#include
using namespace std;
int main()
{
int x, y, z;
cout << "Enter the first number: ";
cin >> x;
cout << "Enter the second number: ";
cin >> y;
cout << "Enter the third: ";
cin >> z;
if (x > y && x>z)
cout << "the largest number is: " << x<<endl;
else if(y>x && y>z)
cout << "the largest number is: " << y<<endl;
else
cout << "the largest number is: " << z << endl;
return 0;
}
total contributions (4)
How to Write a Program to check whether an integer is positive, negative or zero :