Lab Report 05

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Lab Report 05: Switch Case Operation

Task 01: Write a C program to print the day of the week name using switch case.
Title: To print day of week name using switch case in a C program.
Theory: The following program written in C language helps to print the name of the day of the week
using a switch case. It can also be used to translate the number of the week's day (1-7) to its
appropriate name. If the user inputs a number between 1 to 7, the program will display the message
"Today Is [Day Name]". However, if the user tries to enter a value other than 1 to 7, the program will
show the message "Don't be Smart - Wrong Choice, Try Again!!!". The program works only if the
input values are between 1 to 7.
Basic Input:
To print the name of a weekday based on a user input number, you can follow the below steps:
1. Get the day number input from the user and store it in a variable called "week".
2. Use a switch statement with "week" as the parameter to compare with the different cases.
3. Since there are seven possible values for the "week" variable (1-7), write seven cases inside the
switch block. Add a default case as an else block to handle any other value.
4. Print the corresponding weekday for each case (1-7). For example, for "case 1:", print
"MONDAY", and so on. For "case 7:", print "SUNDAY".
5. If none match, use the default case to print "Invalid week number".
Make sure to follow these steps to accurately print the name of the weekday based on the user input
number.
Code:

Output:

Task 02: Write a C program to print a total number of days in a month using switch case.
Title: To print a total number of days in a month using switch case in a C program.
Theory: This C program demonstrates how to find the number of days in a month using a switch case
statement. In this program, we do not use the "break" keyword after each "case" statement since we
can check multiple case values for the same body.
Basic Input: Here is the step-by-step descriptive logic to print the number of days in a month using
switch...case:
1. First, take input from the user for the month number and store it in a variable called "month".
2. Next, use the switch statement to evaluate the value of "month".
3. Since there are 12 possible values (choices) for "month" (from 1 to 12), write 12 cases inside the
switch statement and one default case as an else block.
4. For case 1, 3, 5, 7, 8, 10, and 12, print "31".
5. For case 4, 6, 9, and 11, print "30".
6. For case 2, print either "28" or "29", depending on whether it is a leap year or not.
7. Finally, for the default case, print "invalid input".
Code:

Output:

Task 03: Write a C program to print the input alphabet is vowel or consonant.
Title: To print the input alphabet is vowel or consonant in a C program.
Theory: In this program, we will write a C program to determine whether a character is a vowel or a
consonant by utilizing a switch case statement. The program prompts the user to input an alphabet,
checks whether it is a vowel or consonant, and displays the result on the screen.
Basic Input:
To determine whether a letter is a vowel or consonant, you can follow these steps:
1. Ask the user to input a letter and store it in a variable called 'ch'.
2. Use a switch statement to check the value of 'ch'.
3. For 'ch', there are 10 possible vowel cases to check: 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', and 'U'.
4. Write all 10 possible cases for vowels and print "Vowel" for each case.
5. If the letter is not a vowel, then add a default case and print "Consonant".
By following these steps, you can easily determine whether a letter is a vowel or a consonant.
Code:

Output:

Task 04: Write a C program to print the largest number from two inputs using switch case.
Title: To print the largest number from two inputs using switch case in a C program.
Theory:
In this program, we will learn how to use switch case statements in C programming to find the Largest
of two numbers. The program takes two integer inputs from the user and displays the largest of the
two.
Basic Input: To determine whether num1 is greater than num2, we use the expression "num1 >
num2." This expression evaluates to 1 if num1 is greater than num2, and 0 otherwise. If we use this
expression in the switch statement as "switch(num1 > num2)", there are two possible cases: case 0
and case 1. Here is a step-by-step guide to finding the largest using switch...case:
1. Get two numbers from the user and store them in variables named num1 and num2.
2. Use the switch statement with the expression switch(num1 > num2).
3. The expression (num1 > num2) can have two possible values: 0 and 1.
4. In case 0, print "num2 is largest."
5. In case 1, print "num1 is largest."
Code:
Output:

Task 05: Write a C program to print the larger number from three inputs using switch case.
Title: To print the larger number from three inputs using switch case in a C program.
Theory: When it comes to programming, finding the largest number among three is a common task.
There are several approaches to solve this problem, and in this program, we will discuss each of them
in detail. By reading this program, you will be able to choose the best approach that suits your
programming style and requirements.
Basic Input: To find the largest of three numbers, we use the following algorithm:
1. Begin by initializing three integer variables as num1, num2, and num3 to store the three input
numbers.
2. Read three integer numbers num1, num2, and num3 from the user.
3. Compare num1 with num2 and num3 to find the largest of num1, num2, and num3.
4. If num1 is greater than num2 and num3, then output "num1 is the largest number".
5. If num2 is greater than num1 and num3, output "num2 is the largest number".
6. If num3 is greater than num1 and num2, output "num3 is the largest number".
7. Stop.
This algorithm allows us to easily and accurately determine the largest of three numbers.
Code:

Output:
Task 06: Write a C program to print the input number is even or odd using switch case.
Title: To print the input number is even or odd using switch case.
Theory: This program checks if an integer is even or odd using switch case statement in C
programming language.
Basic Input: The logic to implement is as follows:
1. Divide the number by 2 and find its modulus. If the modulus is 0, then the number is even, and if it
is 1, then the number is odd.
2. Check the case values for 0 and 1.
3. If the case value is 0, then the number is even, and if it is 1, then the number is odd.
Code:

Output:

Task 07: Write a C program to check the number is positive, negative or zero using switch case.
Title: To check whether the number is positive, negative, or zero switch case.
Theory: To use switch case, an expression that returns a list of known constants is required. Let's
begin by defining expressions to identify positive, negative, or zero numbers. For positive numbers,
the expression (num > 0) will return 1 (true), otherwise 0 (false). For negative numbers, the
expression (num < 0) will return 1, otherwise 0. For zero, the expression (num == 0) will return 1,
otherwise 0.
To code this, we will need a nested switch statement.
Basic Input: To check whether a number is positive, negative, or zero using a switch case:
1. Input the number from the user and store it in a variable called 'num'.
2. Use an expression in an outer switch statement to check if the number is positive: switch (num > 0).
3. If the switch statement returns 1, then the number is positive. Print "Positive number" and exit.
4. If the switch statement returns 0, use a nested switch statement to check if the number is negative:
switch (num < 0).
5. If the nested switch statement returns 1, then the number is negative. Print "Negative number" and
exit. If it returns 0, the number is zero. Print "Zero" and exit
Code:
Output:

Task 08: Write a C program to create a simple calculator using switch case.
Title: To create a simple calculator using switch case in a C program.
Theory: In this program, we will learn how to create a basic calculator using the switch...case
statement in the C programming language. The program will prompt the user to input an operator (+,
-, *, /) and two operands. It will then carry out calculations on the two operands based on the operator
selected by the user. Finally, the program will display the result of the calculation to the user.
Basic Input: Here are the step-by-step instructions to create a menu-driven calculator that can
perform basic arithmetic operations:
1. Ask the user to input two numbers and a character, representing the desired operation, in the given
format. Store these values in variables called num1, op, and num2.
2. Use the switch statement to evaluate the value of op.
3. There are four possible values of op: '+', '-', '*', and '/'.
4. For the case of addition, perform the calculation by adding num1 and num2 together and store the
result in a variable called result.
5. Similarly, for the case of subtraction, subtract num2 from num1 and store the result in the result
variable.
6. Repeat the process for multiplication and division, storing the results in the result variable.
7. Finally, print the value of the result variable to display the solution to the user.
Code:
Output:

You might also like