Header Ads Widget

C++ program to input week number and print week day

 

 C++ program to input week number and print week day

Given a week number , write a program to print the week day i.e if week number is 1 then week day is Monday , if 2 then Tuesday and so on .

We will use two methods for it . First simply by using if else statement and second one is by using map . 

 Sample input:

5

Sample output:

Friday

 C++ program to input week number and print week day

The objective of the code is print the week day based on the week number .

Method 1:

Code(C++):-

#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n;
cout<<"Enter week number\n";
cin>>n;
if(n==1)
cout<<"Monday";
else if(n==2)
cout<<"Tuesday";
else if(n==3)
cout<<"Wednesday";
else if(n==4)
cout<<"Thursday";
else if(n==5)
cout<<"Friday";
else if(n==6)
cout<<"Saturday";
else if(n==7)
cout<<"Sunday";
else
cout<<"Invalid input";
return 0;
}


Output:-

Enter week number
5
Friday

Method 2 (By using map ):-

#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n;
cout<<"Enter week number\n";
cin>>n;
unordered_map<int,string> mp;
mp={{1,"Monday"},{2,"Tuesday"},{3,"Wednesday"} , {4,"Thursday"},
{5,"Friday"},{6,"Saturday"},{7,"Sunday"}};
if(n>=1 && n<=7)
cout<<mp[n];
else
cout<<"Invalid input";
return 0;
}



Output:-

Enter week number
5
Friday

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-