C, C++ and Python Program to Print Equivalent Name of the Day Based on User Input

Posted in

C, C++ and Python Program to Print Equivalent Name of the Day Based on User Input
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    In this tutorial, we will write simple programs in C, C++, and Python to print the equivalent name of the day based on user input. For example, if the user types 1, it should print Sunday. If the user enters 4 as the input, the program should display Wednesday.

    Problem Statement

    We need to write a program in C, C++, and Python that can print the weekday based on the weekday number entered by the user.

    For Example

    Input: 7
    Output: Its Saturday

    Solution

    To implement this problem, we can use a switch case statement, which is specially designed for such problems. Both C and C++ support switch case statements, but not Python. Therefore, we have different alternatives for the switch case in Python . Here, we will use the if..else statement.

    C Program to Print the Weekday Using the Switch Case

    #include <stdio.h>
    
    int main()
    {	
    	int num;
    	//input the week number
    	printf("Enter a number from 1 to 7: ");
    	scanf("%d",&num);
    	
    	// if user does not enter a valid number
    	while(1)
    	   {
    	   if(num <1 || num>7)
    	      {
    	       printf("Please enter a valid number from 1 to 7: ");
    	       scanf("%d",&num);
    	      }
    	   else
    	      break;
    	   }
    	
    	switch(num)
    	    {
    	    case 1:
    	    printf("Its Sunday");
    	    break;
    	
    	    case 2:
    	    printf("Its Monday");
    	    break;
    	
    	    case 3:
    	    printf("Its Tuesday");
    	    break;
    	
    	    case 4:
    	    printf("Its Wednesday");
    	    break;
    	
    	    case 5:
    	    printf("Its Thursday");
    	    break;
    	
    	
    	    case 6:
    	    printf("Its Friday");
    	    break;
    	
    	    case 7:
    	    printf("Its Saturday");
    	    break;
    	    }
    
    	return 0;
    }

    Output

    Enter a number from 1 to 7: 34
    Please enter a valid number from 1 to 7: 3
    Its Tuesday

    C++ Program to Print the Equivalent Name of the Weekday Based on User Input Using the Switch Case

    #include<iostream.h>
    #include<conio.h>
    
    void main()
    {
    clrscr();
    int num;
    cout<<"Enter a number from 1 to 7: ";
    cin>>num;
    while(1)
       {
       if(num <1 || num>7)
          {
           cout<<"Please enter a valid number from 1 to 7: ";
           cin>>num;
          }
       else
          break;
       }
    
    switch(num)
        {
        case 1:
        cout<<"Its Sunday";
        break;
    
        case 2:
        cout<<"Its Monday";
        break;
    
        case 3:
        cout<<"Its Tuesday";
        break;
    
        case 4:
        cout<<"Its Wednesday";
        break;
    
        case 5:
        cout<<"Its Thursday";
        break;
    
    
        case 6:
        cout<<"Its Friday";
        break;
    
        case 7:
        cout<<"Its Saturday";
        break;
        }
    
    getch();
    } 

    Output:

    Enter a number from 1 to 7: 10
    Please enter a valid number from 1 to 7: 4
    Its Wednesday

    Python Program to Print the Weekday Using the If else Statement

    # input the week number
    num = int(input("Enter a number from 1 to 7: "))
    
    # if user does not enter a valid number
    while True:
        if num<1 or num >7:
            num = int(input("Please enter a valid number from 1 to 7: "))
        else:
            break
    
    if num==1:
        print("Its Sunday")
    elif num ==2:
        print("Its Monday")
    elif num==3:
        print("Its Tuesday")
    elif num ==4:
        print("Its Wednesday")
    elif num ==5:
        print("Its Thursday")
    elif num == 6:
        print("Its Friday")
    elif num ==7:
        print("Its Saturday")

    Output

    Enter a number from 1 to 7: 5
    Its Thursday

    Wrapping Up!

    In this programming tutorial, we learned how to print weekdays using C, C++, and Python, based on user input. In C++ and C, we implemented the program using the switch case, but in Python, we used the if..else statement because Python 3.9 and lower versions do not support the switch case. Although for this tutorial, we have assumed Sunday as the first day of the week, you can take Monday as the first day of the week. Therefore, Sunday will be the last day of the week.

    People are also reading:

    Leave a Comment on this Post

    0 Comments