Exp 2
Exp 2
Aim:
To write a C program that demonstrates the usage of decision-making constructs: if-
else, Goto
Algorithm:
Step1: Start
Step5: Stop
Program:
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("%d is even.\n", number);
}
else
{
printf("%d is odd.\n", number);
}
return 0;
}
Output:
Result:
Thus the C program to demonstrate if-else is executed and the output was verified
successfully.
b. Write a C Program to find the Largest Numbers Among Three Numbers
Aim:
Algorithm:
Result:
Thus the C program to find largest among three numbers is executed and the output was
verified successfully.
C. Demonstrating Switch Case:
Aim:
To write a C program to design a simple calculator using switch case constructs.
Algorithm:
1.Start the program.
2.Read a, b ,c.
3.print menu
4.read choice
5. Do operations according to the user choice
5. Print result
6. Stop
Program:
#include <stdio.h>
int main()
{
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator)
{
case '+':
result = num1 + num2;
printf("Result: %.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
printf("Result: %.2lf / %.2lf = %.2lf\n", num1, num2, result);
}
else
{
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator! Please use +, -, *, or /.\n");
}
return 0;
}
Output:
Result:
Thus the C program to design a simple calculator using switch case constructs was
executed and the output was verified successfully.
d. Goto
Aim:
Algorithm:
Program:
#include <stdio.h>
int main() {
int i = 1;
if (i == 3)
i++;
goto skip_three;
}
if (i == 7)
i++;
break;
if (i % 2 == 0)
i++;
continue;
i++;
skip_three:
return 0;
Output:
Result:
Thus the C program to demonstrate use of goto, break and continue statement was
executed and the output was verified successfully.