answersLogoWhite

0


Best Answer

int main() {

int x, y;

char op;

int res;

printf("Enter two numbers and a operator\n");

scanf("%d %d %c", &x, &y, &op);

switch (op) {

case '+':

res = x+y;

break;

case '-':

res = x-y;

break;

case '*':

res = x*y;

break;

case '/':

res = x/y;

break;

default:

printf("Unknown operator %c\n", op);

exit(1);

}

printf("Resault is %d\n", res);

return 0;

}

#include

void math_operations(char operation, int arg1, int arg2);

using std::cout;

using std::cin;

using std::endl;

int main()

{

int num1 = 0;

cout << "Enter first number: ";

cin >> num1;

int num2 = 0;

cout << "Enter second number: ";

cin >> num2;

char operation = '+';

cout << endl << "Enter the operation:"

<< endl << "+ for addition"

<< endl << "- for substruction"

<< endl << "* for multiplication" << endl;

cin >> operation;

cout << endl << "You chose: ";

math_operations(operation, num1, num2);

system("PAUSE");

return 0;

}

void math_operations(char operation, int arg1, int arg2)

{

if (operation '-')

{

cout << "-" << endl;

cout << arg1 << " - " << arg2 << " = " << (arg1 - arg2);

}

else

{

cout << "*" << endl;

cout << arg1 << " * " << arg2 << " = " << (arg1 * arg2);

}

}

* The program was checked in VS2008

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program which takes two integer operands and one operator from the user performs the operation and then prints the result?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Performing Arts

What is an example of an acrstic poem for the word integers?

what is an example of an acrostic poem for the word integer


What has the author Nils Thun written?

Thorkil Naur has written: 'Integer factorization' -- subject(s): Factorization (Mathematics)


True or false the controlling expression of a switch statement must have a value that is an integer or a character?

False. In C++, it must evaluate to an integral type, which includes floating point types. In other languages, a character array or other object may also be acceptable.


What is critical region in operating systems?

1) CRITICAL REGIONS a) Motivation The time dependent errors can be easily generated when semaphores are used to solve the critical section problem. To overcome this difficulty a new language construct, the critical region was introduced. b) Definition and notation A variable v of type T, which is to be shared among many processes, can be declared: VAR v: SHARED T; The variable v can be accessed only inside a region statement of the following form: REGION v DO S; This construct means that while statement S is being executed, no other process can access the variable v. Thus, if the two statements, REGION v DO S1; REGION v DO S2; are executed concurrently in distinct sequential processes, the result will be equivalent to the sequential execution S1 followed by S2, or S2 followed by S1. To illustrate this construct, consider the frames CLASS defined in abstract data type. Since mutual exclusion is required when accessing the array free, we need to declare it as a shared array. VAR free: SHARED ARRAY [l..n] OF boolean; The acquire procedure must be rewritten as follows; PROCEDURE ENTRY acquire (MAR index: integer); BEGIN REGION free DO FOR index := 1 TO n DO IF free[index] THEN BEGIN free[index] := false; exit; END; index := 1; END; The critical-region construct guards against some simple errors associated with the semaphore solution to the critical section problem which may be made by a programmer. c) Compiler implementation of the critical region construct. For each declaration VAR v: SHARED T; the compiler generates a semaphore v-mutex initialized to 1. For each statement, REGION v DO S; the compiler generates the following code: p(v-mutex); S; V(v-mutex); Critical region may also be nested. In this case, however, deadlocks may result. Example of deadlock) VAR x, y: SHARED T; PARBEGIN Q: REGION x DO REGION y DO S1; R: REGION y DO REGION x DO S2; PAREND; 2) CONDITIONAL CRITICAL REGIONS The critical region construct can be effectively used to solve the critical section problem. It cannot, however, be used to solve some general synchronization problems. For this reason the conditional critical region was introduced. The major difference between the critical region and the conditional critical region constructs is in the region statement, which now has the form: REGION v WHEN B DO S; where B is a boolean expression. As before, regions referring to the same shared variable exclude each other in time. Now, however, when a process enters the critical section region. the boolean expression B is evaluated if the expression is true, statement S is executed. if it is false, the process relinquishes the mutual exclusion and is delayed until B becomes lame and no other process is in the region associated with v. Example for bounded buffer problem) VAR buffer: SHARED RECORD pool: ARRAY [0..n-l] OF item; count, in. out: integer; END; The producer process inserts a new item nextp in buffer by executing REGION buffer WHEN count &lt; n DO BEGIN pool[in] := nextp; in := in + i MOD n; count := count + 1; END; The counter process removes an item from the shared buffer and puts it in nextc by executing: REGION buffer WHEN count &gt; 0 DO BEGIN nextc := pool[out]; out := out +1 MOD n; count := count - 1; END; However, the CLASS concept alone cannot guarantee that such sequences will be observed. * A process might operate on the file without first gaining access permission to it; * A process might never release the file once it has been granted access to it; * A process might attempt to release a file that it never required; * A process might request the same file twice; Not that we have now encountered difficulties that are similar in nature to those that motivated us to develop the critical region construct in the first place. Previously, we had to worry about the correct use of Semaphores. Now we have to worry about the correct useo higher-level programmer-defined operations, with which the comelier can no longer assist us.


Related questions

The modulus operator can be used only with integer operands?

True


The modulus operator percent can be used only with integer operands?

True


Which operator in 'c' takes only integer operands?

modulus (%) and shift (&lt;&lt;, &gt;&gt;) for examples.


What The modulus operator percent can be used only with integer operands is true or false?

true


State whether each what is TRUE or FALSE 1. All variables must be declared before they are used. 2. The modulus operator can be used only with integer operands.?

Both statements are true.


What activities are carried out by the ALU?

The Arithmetic Logic Unit (ALU) is an essential component of a computer's central processing unit (CPU). It performs various operations related to arithmetic and logic. One of the primary activities of the ALU is arithmetic operations such as addition, subtraction, multiplication, and division. These operations are performed on binary numbers, which are represented in the form of bits. The ALU can perform these operations on two or more operands, depending on the instruction given by the CPU. The ALU also performs logical operations such as AND, OR, NOT, and XOR. These operations are used to manipulate the bits in the operands to produce a desired output. For example, the AND operation returns a 1 only if both the operands have a 1 in the same position. Besides these basic operations, the ALU also performs comparison operations. It compares two operands and produces an output indicating whether they are equal, greater than, or less than each other. The ALU also performs shift operations, which involve moving the bits in an operand left or right by a certain number of positions. Shift operations are useful in programming, where they can be used to manipulate data stored in registers. Finally, the ALU can also perform other operations such as incrementing and decrementing. These operations are used to modify the value of a register or memory location. In summary, the ALU performs a variety of operations related to arithmetic, logic, comparison, and manipulation of binary numbers. Its efficient operation is crucial for the overall performance of a computer's CPU.


The part of the microprocessor that performs integer arithmetic operations is called the?

ALU


What is the restriction to be followed when using a modulo operator modulo operator?

When using the modulo operator in mathematics or programming, there is a restriction that the divisor (the number after the modulo operator) should be non-zero. A zero divisor would result in a division by zero error, which is undefined.


What is the operation or property of -8 in math?

It is a negative integer.


Is the product of an integer is an integer?

It is not possible to have the product of an integer. "product" is a binary operation and that means that it is an operation that combines two numbers to make the product - a third number. So you need two numbers as input, not just one.


What is Boolean operator in c language?

A Boolean operator is any operator that returns true or false. False is typically denoted by the integer value 0 while all non-zero values equate to true. The less-than operator (&lt;) is an example of a Boolean operator.


What does the modulus operator in Java do?

It is an binary arithmetic operator which returns the remainder of division operation. It can used in both floating-point values and integer values. opLeft % opRight where, opLeft is the left operand and opRight is the right operand. This expression is equivalent to the expression opLeft - ((int) (opLeft / opRight) * opRight)