Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, July 16, 2022

C program to display the name of the day on the basis of entered number for SEE and NEB

C program to display the name of the day on the basis of entered number for SEE and NEB

 



Introduction:


If you are a student of SEE or NEB in Nepal, you may have come across a program that asks you to enter a number from 1 to 7 and displays the name of the corresponding day of the week. This program is a classic example of conditional programming in C language. In this article, we will provide a C program to display the name of the day on the basis of the entered number, which is a common programming problem in both SEE and NEB exams.


Understanding the problem statement


The problem statement asks us to display the name of the day on the basis of the entered number. We can assume that the user will enter a number from 1 to 7, and our program should display the corresponding day of the week.


Logic of the program


To solve this problem, we need to use conditional programming in C language. We can use the switch statement to check the entered number and display the corresponding day of the week. Here is the logic of the program:
  1. Ask the user to enter a number from 1 to 7
  2. Read the entered number from the user
  3. Use the switch statement to check the entered number
  4. If the entered number is 1, display "Sunday"
  5. If the entered number is 2, display "Monday"
  6. If the entered number is 3, display "Tuesday"
  7. If the entered number is 4, display "Wednesday"
  8. If the entered number is 5, display "Thursday"
  9. If the entered number is 6, display "Friday"
  10. If the entered number is 7, display "Saturday"
  11. If the entered number is not between 1 and 7, display an error message


Implementation of the program

Now that we understand the logic of the program, let's implement it in C language. Here is the complete code for the program:


Here is the program using switch statement


Using switch statement

#include <stdio.h>

int main() {

   int day;

   printf("Enter the number of the day (1-7): ");

   scanf("%d", &day);

   switch(day) {

      case 1:

         printf("Sunday");

         break;

      case 2:

         printf("Monday");

         break;

      case 3:

         printf("Tuesday");

         break;

      case 4:

         printf("Wednesday");

         break;

      case 5:

         printf("Thursday");

         break;

      case 6:

         printf("Friday");

         break;

      case 7:

         printf("Saturday");

         break;

      default:

         printf("Invalid input. Please enter a number between 1 and 7.");

         break;

   }

   return 0;

}



Explanation of the Program:


The program starts by declaring an integer variable day to store the input number. Then, it prompts the user to enter a number between 1 and 7 using the printf function and reads the input using the scanf function.

The program then uses a switch statement to check the input number and display the corresponding day of the week. The case statements check the value of the input number and execute the corresponding block of code. The break statement is used to exit the switch statement after the corresponding block of code is executed.

If the input number is not between 1 and 7, then the default case is executed, and the program prints an error message.

Finally, the program returns 0, indicating successful execution.


Output:



We can write the above program by using 
Using if…else if statement.

#include <stdio.h>

#include <conio.h>

int main()

 

{

    int w;

 

    printf("Enter week number (1-7): ");

    scanf("%d", &w);

 

    if(w == 1)

    {

        printf("Sunday");

    }

    else if(w == 2)

    {

        printf("Monday");

    }

    else if(w == 3)

    {

        printf("Tuesday");

    }

    else if(w == 4)

    {

        printf("Wednesday");

    }

    else if(w == 5)

    {

        printf("Thursday");

    }

    else if(w == 6)

    {

        printf("Friday");

    }

    else if(w == 7)

    {

        printf("Saturday");

    }

    else

    {

        printf("Invalid day code. Try again");

    }

 

    return 0;

}

 


Conclusion


In this article, we have discussed the logic and implementation of a C program that displays the name of the day on the basis of the entered number. We have used conditional programming in C language to check the entered number and display the corresponding day of the week using the switch and if..else statements. We have also tested the program by compiling and running it on a C compiler. This program is a good example of how conditional programming can be used to solve simple problems.

FAQs


1. What is conditional programming?
Ans: Conditional programming is a programming paradigm that allows the execution of different statements based on the evaluation of a condition.

2. What is a switch statement in C language?
Ans: A switch statement is a control flow statement in C language that allows the selection of one of several code blocks to be executed based on the value of an expression.

3. Can we use if-else statements instead of switch statement in this program?
Ans: Yes, we can use if-else statements to check the entered number and display the corresponding day of the week. However, switch statements are more efficient when there are many conditions to be checked.

4. What is the purpose of the break statement in the switch statement?
Ans: The break statement is used to terminate a case in the switch statement and prevent the execution of the following case statements.

5. What happens if the user enters a number outside the range of 1 to 7?
Ans: If the user enters a number outside the range of 1 to 7, our program will display an error message.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages