KEMBAR78
Program 3 | PDF | Computer Programming | Computing
0% found this document useful (0 votes)
11 views2 pages

Program 3

The document contains two C programs: the first is a simple calculator that performs addition, subtraction, multiplication, and division based on user input, while handling division by zero and invalid operators. The second program checks if a given integer is an Armstrong number, which is defined as a number that equals the sum of its digits raised to the power of the number of digits. Both programs include user input prompts and output results accordingly.

Uploaded by

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

Program 3

The document contains two C programs: the first is a simple calculator that performs addition, subtraction, multiplication, and division based on user input, while handling division by zero and invalid operators. The second program checks if a given integer is an Armstrong number, which is defined as a number that equals the sum of its digits raised to the power of the number of digits. Both programs include user input prompts and output results accordingly.

Uploaded by

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

// 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;
}

You might also like