Session 7 - C Program
Session 7 - C Program
(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
STEP-4: END
Flowchart:
START
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 = 10
Print “October”
Expression = 11
Print “November”
Expression = 12
Print “December”
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
Expression = ‘-‘
Print the difference a-b
Expression = ‘*’
Print the product a*b
true
Expression = ‘%’
false
Is (int)b≠0 ?
true
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
Is ch false
alphabet?
true
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’
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;
}