0% found this document useful (0 votes)
286 views6 pages

Lab 5

The document contains 10 code snippets showing the use of switch case statements in C programs to check various conditions. The code examples check: 1) if a character is alphabet, digit or special character 2) if an alphabet is a vowel or consonant 3) if a number is even or odd 4) if an alphabet is a vowel or consonant using switch case 5) the price of a fruit based on its first letter 6) finds the maximum of two numbers 7) performs basic calculations like addition, subtraction, multiplication and division 8) checks if a year is a leap year 9) pays a bill by calculating required notes 10) checks vowel or consonant using switch case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
286 views6 pages

Lab 5

The document contains 10 code snippets showing the use of switch case statements in C programs to check various conditions. The code examples check: 1) if a character is alphabet, digit or special character 2) if an alphabet is a vowel or consonant 3) if a number is even or odd 4) if an alphabet is a vowel or consonant using switch case 5) the price of a fruit based on its first letter 6) finds the maximum of two numbers 7) performs basic calculations like addition, subtraction, multiplication and division 8) checks if a year is a leap year 9) pays a bill by calculating required notes 10) checks vowel or consonant using switch case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

C Program to check whether the input is a character, digit or a special


character.
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("%c is ALPHABET.\n", ch);
else if(ch >= '0' && ch <= '9')
printf("%c is DIGIT.\n", ch);
else
printf("%c is SPECIAL CHARACTER.\n", ch);
}

2. Write a C program to check whether an input alphabet is a vowel or a consonant


(assume
that the input is an English letter).
#include <stdio.h>

int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);

if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||


ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is Consonant.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}

return 0;
}

3. C program to check whether an input is an odd number or an even number using


switch-case.
#include <stdio.h>
void main()
{
int num;
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
switch(num % 2)
{
//If n%2 == 0
case 0: printf("Number is Even");
break;
//Else if n%2 != 0
case 1: printf("Number is Odd");
break;
}
}

4. Write a C program to check whether an input alphabet is a vowel or a consonant


using
switch case.
#include <stdio.h>

int main()
{
char ch;

/* Input an alphabet from user */


printf("Enter any alphabet: ");
scanf("%c", &ch);

/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}

5. Write a C program that takes input the first letter of any of the following
fruits name–
Mango ---- Tk.500/kg,
Apple ---- Tk.250/kg,
Banana ----Tk. 130/kg,
Cherry -----Tk. 270/kg, and suggests the price of the fruit as output, using switch
case.

#include<stdio.h>
int main()
{
char character;
printf("Enter a letter: ");
scanf("%c",&character);

switch(character)
{
case 'M':
printf("Mango---TK.500/kg");
break;
case 'A':
printf("Apple---TK.250/kg");
break;
case 'B':
printf("Banana---TK.130/kg");
break;
case 'C':
printf("Cherry---TK.270/kg");
break;
default:
printf("Error");
}

6. Find the maximum between two numbers using switch case.


#include <stdio.h>

int main()
{
int num1, num2;

/* Input two numbers from user */


printf("Enter two numbers to find maximum: ");
scanf("%d%d", &num1, &num2);

/* Expression (num1 > num2) will return either 0 or 1 */


switch(num1 > num2)
{
/* If condition (num1>num2) is false */
case 0:
printf("%d is maximum", num2);
break;

/* If condition (num1>num2) is true */


case 1:
printf("%d is maximum", num1);
break;
}

return 0;
}
7. Use switch case to make a simple calculator that can add, subtract, multiply or
divide two input
numbers. The operator (+ , - , * or /) should also be read from user.
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

8. Write a C program to check whether a year is a leap year or not, using switch
case.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}

10. Farhan must pay a bill to a shopkeeper. The shopkeeper doesn’t have any change,
so Farhan must
have all the notes required to pay the bill. Write a C program that reads the bill
and the number of
each type of note (500, 100, 50, 20, 10, 5, 2, 1) from user and then output whether
it is possible for
Farhan to pay the bill or not. If it is possible, then also output the number of
each notes required to
pay the bill.

#include <stdio.h>

int main()
{
int amount;
int note500, note100, note50, note20, note10, note5, note2, note1;

/* Initialize all notes to 0 */


note500 = note100 = note50 = note20 = note10 = note5 = note2 = note1 = 0;

/* Input amount from user */


printf("Enter amount: ");
scanf("%d", &amount);

if(amount >= 500)


{
note500 = amount/500;
amount -= note500 * 500;
}
if(amount >= 100)
{
note100 = amount/100;
amount -= note100 * 100;
}
if(amount >= 50)
{
note50 = amount/50;
amount -= note50 * 50;
}
if(amount >= 20)
{
note20 = amount/20;
amount -= note20 * 20;
}
if(amount >= 10)
{
note10 = amount/10;
amount -= note10 * 10;
}
if(amount >= 5)
{
note5 = amount/5;
amount -= note5 * 5;
}
if(amount >= 2)
{
note2 = amount /2;
amount -= note2 * 2;
}
if(amount >= 1)
{
note1 = amount;
}

/* Print required notes */


printf("Total number of notes = \n");
printf("500 = %d\n", note500);
printf("100 = %d\n", note100);
printf("50 = %d\n", note50);
printf("20 = %d\n", note20);
printf("10 = %d\n", note10);
printf("5 = %d\n", note5);
printf("2 = %d\n", note2);
printf("1 = %d\n", note1);

return 0;
}

You might also like