0% found this document useful (0 votes)
2 views

cse115-lab-manual-5-if_else part2

The document contains multiple C programming examples demonstrating the use of nested if-else statements and switch-case constructs. It includes programs for checking character types, determining vowels or consonants, identifying even or odd numbers, and implementing a simple calculator. Additionally, it provides exercises for calculating fruit prices, finding maximum numbers, and handling bill payments with specific denominations.

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

cse115-lab-manual-5-if_else part2

The document contains multiple C programming examples demonstrating the use of nested if-else statements and switch-case constructs. It includes programs for checking character types, determining vowels or consonants, identifying even or odd numbers, and implementing a simple calculator. Additionally, it provides exercises for calculating fruit prices, finding maximum numbers, and handling bill payments with specific denominations.

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CSE 115 Lab on nested if-else, switch-case – Ara2

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);
}

Try it yourself 1a: Write a C program to check whether an input alphabet is a vowel or a consonant (assume
that the input is an English letter).

Here is a C program to check whether an input alphabet is a vowel or a consonant:

#include <stdio.h>

int main() {
char ch;

// Ask the user for a character


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

// Convert the character to lowercase to handle both upper and lower case input
ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;

// Check if the character is a vowel or consonant


if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}

return 0;
}

Explanation:

1. The program takes a character input from the user.


2. It converts the character to lowercase using a simple conditional expression to handle both uppercase
and lowercase letters.
3. It checks if the character is a vowel (a, e, i, o, u). If it is, the program prints that it's a vowel; otherwise,
it prints that it's a consonant.
Sample Output:
Enter an alphabet: A
A is a vowel.

Enter an alphabet: b
b is a consonant.

This program will work for both uppercase and lowercase letters.

2. 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;
}
}

Try it yourself 2: Write a C program to check whether an input alphabet is a vowel or a consonant using
switch case (assume that the input is an English letter).

Here is the C program that checks whether an input alphabet is a vowel or a consonant using a switch case:

#include <stdio.h>

int main() {
char ch;

// Ask the user for a character


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

// Use switch-case to check for vowels


switch(ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}

return 0;
}

Explanation:

1. The program asks the user to input a character.


2. The switch statement is used to check if the character is a vowel by comparing it with the lowercase
and uppercase vowels.
3. If the character is a vowel, the program prints that it's a vowel.
4. If the character doesn't match any vowel case, the default case is executed, and it prints that the
character is a consonant.

Sample Output:
Enter an alphabet: A
A is a vowel.

Enter an alphabet: b
b is a consonant.

This program will work correctly for both uppercase and lowercase alphabets.

EXERCISE:

1. 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.
2. Find the maximum between two numbers using switch case.
3. 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.
Sample Input/Output:
Enter two numbers: 4.5 2.9
Enter operator: +
4.5 + 2.9 = 7.4

Assignment:

1. Write a C program to check whether a year is a leap year or not, using switch case.
2. 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.
Tentative Input/Output (bold ones are user inputs):
Enter amount: 1175
Enter number of notes:
500: 1
100: 7
50: 1
20: 0
10: 5
5: 1
2: 0
1: 0
Farhan can pay the bill by using the following counts of different notes:
500: 1
100: 6
50: 1
20: 0
10: 2
5: 1
2: 0
1: 0

Exercise Problems

1. Write a C program that takes input the first letter of any of the following fruits' name and suggests the
price of the fruit using switch case.

c
Copy
#include <stdio.h>

int main() {
char fruit;

// Ask the user to enter the first letter of the fruit


printf("Enter the first letter of the fruit (M, A, B, C): ");
scanf("%c", &fruit);

// Use switch-case to determine the price of the fruit


switch(fruit) {
case 'M': case 'm':
printf("Mango: Tk. 500/kg\n");
break;
case 'A': case 'a':
printf("Apple: Tk. 250/kg\n");
break;
case 'B': case 'b':
printf("Banana: Tk. 130/kg\n");
break;
case 'C': case 'c':
printf("Cherry: Tk. 270/kg\n");
break;
default:
printf("Invalid input! Please enter a valid letter.\n");
}

return 0;
}

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

c
Copy
#include <stdio.h>

int main() {
int num1, num2;

// Ask the user for two numbers


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

// Use switch-case to compare the numbers


switch (num1 > num2) {
case 1:
printf("The maximum number is: %d\n", num1);
break;
case 0:
switch (num1 < num2) {
case 1:
printf("The maximum number is: %d\n", num2);
break;
default:
printf("Both numbers are equal.\n");
}
break;
}

return 0;
}

3. Use switch case to make a simple calculator that can add, subtract, multiply, or divide two input
numbers. The operator (+, -, *, /) should also be read from the user.

c
Copy
#include <stdio.h>

int main() {
float num1, num2;
char operator;

// Ask the user for two numbers and an operator


printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); // Added space before %c to consume any leftover newline
character

// Use switch-case to perform the operation


switch(operator) {
case '+':
printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid operator!\n");
}

return 0;
}

Assignment Problems

1. Write a C program to check whether a year is a leap year or not using switch case.

c
Copy
#include <stdio.h>

int main() {
int year;

// Ask the user for a year


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

// Use switch-case to determine if it's a leap year


switch ((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 1 : 0) {
case 1:
printf("%d is a leap year.\n", year);
break;
case 0:
printf("%d is not a leap year.\n", year);
break;
}

return 0;
}

2. 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 the user and then outputs whether it is possible for Farhan to pay the bill or not. If it is possible,
then also output the number of each note required to pay the bill.

c
Copy
#include <stdio.h>

int main() {
int amount;
int notes[8];
int denominations[] = {500, 100, 50, 20, 10, 5, 2, 1};
int total = 0;

// Ask the user for the amount and number of notes


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

printf("Enter number of notes for 500, 100, 50, 20, 10, 5, 2, 1:\n");
for (int i = 0; i < 8; i++) {
printf("%d: ", denominations[i]);
scanf("%d", &notes[i]);
}

// Calculate total amount using the entered notes


for (int i = 0; i < 8; i++) {
total += notes[i] * denominations[i];
}

// Check if Farhan can pay the bill


if (total == amount) {
printf("Farhan can pay the bill by using the following counts of different notes:\
n");
for (int i = 0; i < 8; i++) {
printf("%d: %d\n", denominations[i], notes[i]);
}
} else {
printf("It is not possible for Farhan to pay the bill with the given notes.\n");
}

return 0;
}

Sample Output for the Bill Payment Program:


yaml
Copy
Enter the bill amount: 1175
Enter number of notes for 500, 100, 50, 20, 10, 5, 2, 1:
500: 1
100: 7
50: 1
20: 0
10: 5
5: 1
2: 0
1: 0
Farhan can pay the bill by using the following counts of different notes:
500: 1
100: 7
50: 1
20: 0
10: 5
5: 1
2: 0
1: 0

You might also like