// Program 3.
A: Develop C program to build simple calculator using switch
#include <stdio.h>
int main()
{
char operator;
double num1, num2, result;
// Display options and input choice
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
// Input two numbers
printf("Enter two numbers:\n");
scanf("%lf %lf", &num1, &num2);
// Perform calculation based on operator using switch
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("Error: Invalid operator.\n");
break;
}
return 0;
}
// Program 3.B: Develop C program to check whether the given number is Armstrong
number or not.
/* An Armstrong number is a number that is equal to the sum of its own digits each
raised to the power of the number of digits.*/
#include <stdio.h>
#include <math.h>
int main()
{
int number, temp, remainder, digits_count = 0;
int sum = 0;
// Input the number from the user
printf("Enter an integer: ");
scanf("%d", &number);
// Store the original number for later comparison
temp = number;
// Find the number of digits in the given number
while (temp != 0)
{
temp = temp / 10;
digits_count++;
}
// Reset temp to the initial input
temp = number;
// Calculate the sum of each digit raised to the power of numDigits
while (temp != 0)
{
remainder = temp % 10;
sum = sum + pow(remainder, digits_count);
temp = temp / 10;
}
// Check if the sum is equal to the original number
if (sum == number)
{
printf("%d is an Armstrong number.\n", number);
}
else
{
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}