Input day number and print day name in C++
if else if - Question 3
In this question, we will see how to input a number between 1 to 7 and print the corresponding day of a week (day name) in C++ programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.
Q3) Write a program in C++ to input a number between 1 to 7 and print the corresponding day of a week (day name).
Example:
1 = Sunday
2 = Monday
3 = Wednesday
Program
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
if(n==1)
{
cout<<"Sunday";
}
else if(n==2)
{
cout<<"Monday";
}
else if(n==3)
{
cout<<"Tuesday";
}
else if(n==4)
{
cout<<"Wednesday";
}
else if(n==5)
{
cout<<"Thursday";
}
else if(n==6)
{
cout<<"Friday";
}
else if(n==7)
{
cout<<"Saturday";
}
else
{
cout<<"Invalid number";
}
return 0;
}
Output
Enter a number 5 Thursday