Question

PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

PLease IN C not in C++ or JAVA,

Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors).

: Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support in lab 3, please add additional functionality to your calculator. Include the ability to compute the factorial of an integer (Example: 4! = 1 * 2 * 3 *4 = 24), and an integer power of a number (Example: 53 = 5 * 5 * 5 = 125). Finally, add an interesting mathematical function of your choosing which computes the first n numbers of a sequence of numbers and utilizes an array to store the result before displaying it (Examples of mathematical sequences: Fibonacci, Primes). Example of the output follows:

Calculator Menu:

(1) Addition

(2) Subtraction

(3) Multiplication

(4) Division

(5) Modulus (integers only)

(6) Test if prime (integers only)

(7) Factorial (integers only)

(8) Power

(9) Your function

(0) Exit

Please choose an operation: 8

Enter the first number: 5

Enter the second number: 3

5^3 = 125

Calculator Menu:

(1) Addition

(2) Subtraction

(3) Multiplication

(4) Division

(5) Modulus (integers only)

(6) Test if prime (integers only)

(7) Factorial (integers only)

(8) Power

(9) Your function

(0) Exit Please choose an operation: 0

Good Bye!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

input code:

output:

code:

#include <stdio.h>
#include <math.h>
void fun1() /*take value and do Addition*/
{
float a,b;
printf("Enter a first number:");
scanf("%f",&a);
printf("Enter a second number:");
scanf("%f",&b);
printf("%.2f + %.2f = %.2f\n",a,b,a+b);
}
void fun2() /*take value and do Subtraction*/
{
float a,b;
printf("Enter a first number:");
scanf("%f",&a);
printf("Enter a second number:");
scanf("%f",&b);
printf("%.2f - %.2f = %.2f\n",a,b,a-b);
}
void fun3() /*take value and do Multiplication*/
{
float a,b;
printf("Enter a first number:");
scanf("%f",&a);
printf("Enter a second number:");
scanf("%f",&b);
printf("%.2f * %.2f = %.2f\n",a,b,a*b);
}
  
void fun4() /*take value and do Division*/
{
float a,b;
printf("Enter a first number:");
scanf("%f",&a);
printf("Enter a second number:");
scanf("%f",&b);
/*check if divisor is zero or not*/
if(b!=0)
{
printf("%.2f / %.2f = %.2f\n",a,b,a/b);
}
else
{
printf("Divided by Zero.\n");
}
}
void fun5() /*find the Modulus*/
{
int c,d;
char ch='%';
printf("Enter a first number:");
scanf("%d",&c);
printf("Enter a second number:");
scanf("%d",&d);
printf("%d %c %d = %d\n",c,ch,d,c%d);
}
  
  
void fun6() /*check number is prime or not*/
{
int c,d,flag1=1,i;
printf("Enter a number:");
scanf("%d",&c);
/*take one for loop*/
for (i = 2; i <= sqrt(c); i++)
{
/*if number divisor than break loop*/
if (c % i == 0)
{
/*set flag*/
flag1 = 0;
break;
}
}
/*check flag*/
if (flag1 == 1)
{
printf("%d is a prime number\n", c);
}
else
{
printf("%d is not a prime number\n", c);
}
}
  
void fun7() /*find Factorial using for loop*/
{
int c,d,i,Factorial=1;
printf("Enter a number:");
scanf("%d",&c);
printf("%d!=",c);
for(i=1;i<=c;i++)
{
Factorial=Factorial*i;
if(i!=c)
{
printf("%d*",i);
}
else
{
printf("%d",i);
}
}
printf("=%d\n",Factorial);
}
void fun8() /*find Power using a power Function of math library*/
{
float a,b;
printf("Enter a first number:");
scanf("%f",&a);
printf("Enter a second number:");
scanf("%f",&b);
printf("%.2f ^ %.2f = %.2f\n",a,b,pow(a,b));
}
  
  
void fun9() /*find first n number of Fibonacci series*/
{
int c,i;
printf("Enter a number:");
scanf("%d",&c);
int array[c];
array[0]=0;
array[1]=1;
for(i=2;i<c;i++)
{
array[i]=array[i-1]+array[i-2];
}
printf("Fibonacci series is:");
for(i=0;i<c;i++)
{
printf("%d ",array[i]);
}
printf("\n");
}
  
int main()
{
int loop=1,n;
while(loop==1) /*take one loop so we can run untill user want*/
{
/*print menu*/
printf("Calculator Menu:\n");
printf("(1)Addition\n");
printf("(2)Subtraction\n");
printf("(3)Multiplication\n");
printf("(4)Division\n");
printf("(5)Modulus(intergers only)\n");
printf("(6)Test if prime(intergers only)\n");
printf("(7)Factorial(intergers only)\n");
printf("(8)Power\n");
printf("(9)Fibonacci\n");
printf("(0)Exit\n");
printf("Please choose an operation: ");
scanf("%d",&n);
if(n==0) /*check if n==0 than break loop*/
{
loop=0;
}
else
{
/*make switch case of every operation*/
switch(n)
{
case 1:
/*call fun1 which do Addition*/
fun1();
break;
case 2:
/*call fun2 which do Subtraction*/
fun2();
break;
case 3:
/*call fun3 which do Multiplication*/
fun3();
break;
case 4:
/*call fun4 which do Division*/
fun4();
break;
case 5:
/*call fun5 which do Modulus*/
fun5();
break;
case 6:
/*call fun6 which test prime or not*/
fun6();
break;
case 7:
/*call fun7 which find a Factorial*/
fun7();
break;
case 8:
/*call fun8 which find a power*/
fun8();
break;
case 9:
/*call fun9 which find a Fibonacci series*/
fun9();
break;
default:
printf("Invalid number\n");
}
}
}
printf("Good Bye!");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Write a java calculator program that perform the following operations: 1. function to Addition of two...

    Write a java calculator program that perform the following operations: 1. function to Addition of two numbers 2. function to Subtraction of two numbers 3. function to Multiplication of two numbers 4. function to Division of two numbers 5. function to Modulus (a % b) 6. function to Power (a b ) 7. function to Square root of () 8. function to Factorial of a number (n!) 9. function to Log(n) 10. function to Sin(x) 11. function to Absolute value...

  • RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the...

    RAPTOR PROGRAMMING Write a program which will receive two integer values as input. Then prompt the user to choose one of the following operations and produce an output with the selected operation. 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Example output with response: Please enter an input: 2 Please enter another input: 3 Please choose from the follow: 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Output: 2 % 3 is 2.

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two...

    Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two integers and an operator and does the math operation corresponding to the numbers and the operator. For instance, calculate(5,"+", 6) should return 11. Please support the following operators: +- X/^(addition, subtraction, multiplication, division, exponentiation) 1 2 3 function calculate (numi, operator, num2) { // Do the calculation } RUN CODE Parameters Expected Result Actual Result 1,"+", 4 1,"x", 0 20,"7", 5 3,"", 3

  • Convert the following C fragment to equivalent MIPS assembly language Write mini calculator that take two...

    Convert the following C fragment to equivalent MIPS assembly language Write mini calculator that take two integer numbers as input and ask the user if he need to compute addition, subtraction, remainder, division, or multiplication then print the result. Hint: you can give each operation a special number. For example, the addition (1), subtraction (2), …. And so on. The output must be something like: Enter the first integer number: 5 Enter the second integer number: 3 Which operation? 1...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT