[Link].
2 Decision-making constructs: if-else, goto, switch-case,
Date: break-continue
Aim:
To write a C program that demonstrates the usage of decision-making constructs: if-
else, Goto
Algorithm:
Step1: Start
Step2: Get a value to check from user
Step3: check the given number is odd or even
Step4: print the result
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:
To Write a C Program to find the Largest Numbers among Three Numbers.
Algorithm:
[Link] the program.
[Link] a, b ,c.
[Link](a>b and a>c) big = a.
[Link] (b>a and b>c) big =b.
[Link] big = c.
[Link].
[Link] big.
[Link] the Program.
Program:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
{
printf("The largest number is: %d\n", num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("The largest number is: %d\n", num2);
}
else
{
printf("The largest number is: %d\n", num3);
}
return 0;
}
Output:
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:
[Link] the program.
[Link] a, b ,c.
[Link] menu
[Link] 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:
To write a C program to demonstrate use of goto, break and continue statement.
Algorithm:
Program:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10)
if (i == 3)
i++;
goto skip_three;
}
if (i == 7)
i++;
break;
if (i % 2 == 0)
i++;
continue;
printf("Number: %d\n", i);
i++;
skip_three:
printf("Skipped number 3 using goto\n");
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.