0% found this document useful (0 votes)
2 views7 pages

Session 7 - C Program

The document outlines algorithms and code for three different programs using switch statements in C. The first program converts a month number (1-12) to its corresponding month name, the second simulates a simple calculator for basic arithmetic operations, and the third checks if a given character is a vowel or consonant. Each section includes an algorithm, flowchart, and corresponding C code.

Uploaded by

vigmahi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Session 7 - C Program

The document outlines algorithms and code for three different programs using switch statements in C. The first program converts a month number (1-12) to its corresponding month name, the second simulates a simple calculator for basic arithmetic operations, and the third checks if a given character is a vowel or consonant. Each section includes an algorithm, flowchart, and corresponding C code.

Uploaded by

vigmahi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Session 7

(i) To input month number and display its respec ve month in word using ‘switch’ statement.
Algorithm:
STEP-1: START
STEP-2: Input month number, num
STEP-3: Check the expression, num

If expression = 1: Print “January”


If expression = 2: Print “February”
If expression = 3: Print “March”
If expression = 4: Print “April”
If expression = 5: Print “May”
If expression = 6: Print “June”
If expression = 7: Print “July”
If expression = 8: Print “August”
If expression = 9: Print “September”
If expression = 10: Print “October”
If expression = 11: Print “November”
If expression = 12: Print “December”
default (no match) : Print “Invalid month number!”

STEP-4: END

Flowchart:
START

Prompt “Enter the month number (1-12) “

Read the number num

Check num

Expression = 1
Print “January”

Expression = 2
Print “February”

Expression = 3
Print “March”

Expression = 4
Print “April”

Expression = 5
Print “May”
Expression = 6
Print “June”
Expression = 7
Print “July”

Expression = 8
Print “August”

Expression = 9 Print “September”

Expression = 10
Print “October”

Expression = 11
Print “November”

Expression = 12
Print “December”

Default (no match)


Print “Invalid month number!”

END

Code:

#include <stdio.h>
int main()
{
int num;
printf("Enter month number (1-12): ");
scanf("%d", &num);

switch (num)
{ case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number!\n");
}
return 0;
}

(ii) To simulates a simple calculator to perform the basic arithme c opera ons (Consider the
operators +, -, x, / and % using ‘switch’ statement)
Algorithm:
STEP-1: START
STEP-2: Read the two numbers a,b and also Read the operator op
STEP-3: Check expression op
If expression = ‘+’ : Print the sum a+b
If expression = ‘-’ : Print the difference a-b
If expression = ‘*’: Print the product a*b
If expression = ‘/’ : IF (b ≠0) Print the sum a+b
ELSE Print “Error message”
If expression = ‘%’ : IF (b ≠0) Print the remainder a%b a er typecas ng them to int
ELSE Print “Error message”
Default (no match): Print “Invalid operator!”
STEP-4: END

Flowchart:
START

Read the numbers a,b

Read the operator character


Check op
Expression = ‘+’
Print the sum a+b

Expression = ‘-‘
Print the difference a-b

Expression = ‘*’
Print the product a*b

Expression = ‘/’ false


Is b≠0 ?

true

Print the quo ent a/b Print Error message

Expression = ‘%’
false
Is (int)b≠0 ?

true

Print the remainder Print Error message


(int)a%(int)b

Default (no match)


Print Invalid operator!

END
Code:

#include <stdio.h>
int main()
{char op;
float a,b;
printf("Enter the two numbers:");
scanf("%f %f", &a, &b);
printf("Enter the operator (+, -, *, /, %%): ");
scanf(" %c", &op); // Note the space before %c to consume any newline character
switch(op)
{
case '+':
printf("%.2f + %.2f = %.2f\n", a, b, a + b);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", a, b, a - b);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", a, b, a * b);
break;
case '/':
if (b != 0)
printf("%.2f / %.2f = %.2f\n", a, b, a / b);
else
printf("Error: Division by zero is not allowed.\n");
break;
case '%':
if ((int)b != 0)
printf("%.2f %% %.2f = %.2f\n", a, b, (int)a % (int)b);
else
printf("Error: Division by zero is not allowed.\n");
break;
default:
printf("Error: Invalid operator.\n");
break;
}
return 0;
}

(iii) To check whether a given alphabet is vowel or consonant using ‘switch’ statement.

Algorithm:
STEP-1: START
STEP-2: Read the character input ch
STEP-3: IF ch belongs to alphabet, con nue with STEP-4. Otherwise go to STEP-5
STEP-4: Check expression ch
if expression = ‘a’ :
if expression = ‘A’ :
if expression = ‘e’ :
if expression = ‘E’ :
if expression = ‘i’ :
if expression = ‘I’ :
if expression = ‘o’ :
if expression = ‘O’ :
if expression = ‘u’ :
if expression = ‘U’ :
Print “ch is a vowel.”
default (no match) : Print “ch is a consonant.”

END Program
STEP-5: Print “Invalid input”
STEP-6: END
Flowchart:
START

Read the character ch

Is ch false
alphabet?
true

Print “Invalid input”


Check ch

Expression = ‘a’
Expression = ‘A’
Expression = ‘e’

Expression = ‘E’

Expression = ‘i’
Print “ch is a vowel.”
Expression = ‘I’

Expression = ‘o’

Expression = ‘O’

Expression = ‘u’
Expression = ‘U’

Default (no match)


Print “ch is a consonant.”

END
Code:

#include <stdio.h>
#include <ctype.h>
int main()
{char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch); // Note the space before %c to consume any newline character
if (isalpha(ch))
{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);
break;
}
}
else
printf("Error: Invalid input. Please enter an alphabet.\n");
return 0;
}

You might also like