0% found this document useful (0 votes)
24 views3 pages

C Style Simple Calculator Basic Arithmetic, Prime and Armstrong Numbers

The document contains 3 C code examples that demonstrate: 1. A simple calculator program that uses a switch statement to perform basic arithmetic operations like addition, subtraction, multiplication and division on two operands. 2. A program that finds and prints all prime numbers between two integers entered by the user. 3. A program that checks if a number entered by the user is a prime number or Armstrong number using separate functions for each check.

Uploaded by

Animal Cares
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)
24 views3 pages

C Style Simple Calculator Basic Arithmetic, Prime and Armstrong Numbers

The document contains 3 C code examples that demonstrate: 1. A simple calculator program that uses a switch statement to perform basic arithmetic operations like addition, subtraction, multiplication and division on two operands. 2. A program that finds and prints all prime numbers between two integers entered by the user. 3. A program that checks if a number entered by the user is a prime number or Armstrong number using separate functions for each check.

Uploaded by

Animal Cares
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/ 3

// Simple Calculator using switch Statement

#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

// Prime Numbers Between Two Integers

#include <stdio.h>
int checkPrimeNumber(int n);
int main() {

int n1, n2, i, flag;

printf("Enter two positive integers: ");


scanf("%d %d", &n1, &n2);

// swap n1 and n2 if n1 > n2


if (n1 > n2) {
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}

printf("Prime numbers between %d and %d are: ", n1, n2);


for (i = n1 + 1; i < n2; ++i) {

// flag will be equal to 1 if i is prime


flag = checkPrimeNumber(i);

if (flag == 1) {
printf("%d ", i);
}
}

return 0;
}

// user-defined function to check prime number


int checkPrimeNumber(int n) {
int j, flag = 1;

for (j = 2; j <= n / 2; ++j) {

if (n % j == 0) {
flag = 0;
break;
}
}

return flag;
}

// Check Prime and Armstrong

#include <math.h>
#include <stdio.h>

int checkPrimeNumber(int n);


int checkArmstrongNumber(int n);

int main() {
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);

// check prime number


flag = checkPrimeNumber(n);
if (flag == 1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);

// check Armstrong number


flag = checkArmstrongNumber(n);
if (flag == 1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
return 0;
}

// function to check prime number


int checkPrimeNumber(int n) {
int i, flag = 1, squareRoot;

// computing the square root


squareRoot = sqrt(n);
for (i = 2; i <= squareRoot; ++i) {
// condition for non-prime number
if (n % i == 0) {
flag = 0;
break;
}
}
return flag;
}

// function to check Armstrong number


int checkArmstrongNumber(int num) {
int originalNum, remainder, n = 0, flag;
double result = 0.0;

// store the number of digits of num in n


for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}

for (originalNum = num; originalNum != 0; originalNum /= 10) {


remainder = originalNum % 10;

// store the sum of the power of individual digits in result


result += pow(remainder, n);
}

// condition for Armstrong number


if (round(result) == num)
flag = 1;
else
flag = 0;
return flag;
}

You might also like