Weekday or Weekend Program In C

Introduction

In this post, I am going to write a weekday or weekend program in c. This means that I will ask the user to enter the day. After reading that day, our program will display that the given day is the part of weekday or weekend.

Also Read: Program in C to Remove White Spaces and Comments from a File

I hope you got my point. If you not, see the following expected output.

Expected Outputs

weekday or weekend program in c

Here, we are asking the user to enter a day name. User has entered “Sunday”. We know that Friday, Saturday and Sunday are weekend days. Therefore, output is “Sunday is the part of weekend.”

Let us see the other outputs for the same program.

weekday or weekend program in c output 2

Now, we have entered the day name “Thursday”. So the output is Thursday is the part of weekday.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

What if user enters wrong day name. There are seven days in a week but there is a possibility that user can enter any other name. See the following output.

You can do the same program by using switch case statement. But, for this you have to ask the user to enter a number from 0 to 6. On the basis of these number, you can display the same output. But I am not using here this method.

I hope you have understood what exactly we have to do. Now, let us see the actual weekday or weekend program in c.

Also Read: C Program to Print Multiples of 5 using do-while loop

Weekday or Weekend Program In C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char day1[10],day[10];
    printf("Enter a day name\n");
    scanf("%s",day);
    strcpy(day1,day);
    strlwr(day);
    if((strcmp(day,"friday")==0)||(strcmp(day,"saturday")==0)||(strcmp(day,"sunday")==0))
    {
        printf("%s is the part of weekend",day1);
    }
    else if((strcmp(day,"monday")==0)||(strcmp(day,"tuesday")==0))
    {
        printf("%s is the part of weekday",day1);
    }
    else if((strcmp(day,"wednsday")==0)||(strcmp(day,"thursday")==0))
    {
        printf("%s is the part of weekday",day1);
    }
    else
    {
        printf("%s is invalid day name",day1);
    }
    return 0;
}

In the above program, we are simply using if-else statement to check whether a given day is weekday or weekend.

Here, I have taken two character array or string variables day and day1. Reason is user can enter a day name in any case like small case or capital case or mixed case. Therefore, I am copying original day name to the variable day1 and after that converting original day name into lower case.

While comparing, I am using the variable day and in printf() function, I am using the variable day1.

If you look at the program carefully and if you know control structures i.e. if-else statement very well, then I can guarantee that you have understood the program.

Still, if you have any difficulty for understanding this program, then feel free to contact me. Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment