Anda di halaman 1dari 6

RAGH INSTITUTE OF TECHNOLOGY

SPECIFICATION:
(2). C Program for taking Two integer operands and one operator form user, performs the
operation and then prints the result.
(Consider the operators +,-,*, /, % and use Switch Statement)
DESCRIPTION:
Consider two operands a and b, and assign the values for them at the time of execution. By making
use of the operator entered, the above given values are computed .

Example : Enter a and b Values : 5 6


Enter Operator : *
Result is 30

ALGORITHM:

Step 1: START
Step 2: Read the values of a,b
Step 3: Read Choice Ch
Step 4: Case _ begin
4.1 : + : Display a+b after computing a+b
4.2 : - : Display a-b after computing a-b
4.3 : * : Display a*b after computing a*b
4.4: / : Display a*b after computing a/b
4.5: % : Display a*b after computing a%b
4.6: Default : Display Invalid Choice
Case _end
Step 5 :STOP

FLOWCHART:

Department of Computer Science & Engg

RAGH INSTITUTE OF TECHNOLOGY

START

Read Input Operands : a and b & Operator

Ch==?

Default

Compute :
a*b
Compute
: a+b
Compute :
a/b

Compute
: a%b
Compute :
a-b

Show
a+b

Show
a-b

Show
a/b

Show
a*b

STOP

Department of Computer Science & Engg

Show
a%b

Invalid
Choice

RAGH INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program which takes two integer operands and one operator
from the user,performs the operation and then print the result */
Program name:

// wk2c.c

/* Done By : C-Faculty

Dated: 15/10/2013*/

#include<stdio.h>

// stdio.h imports i/p and o/p functions

int main()

// User defined program

// Main Program Begins

char op;

// Character variable op is declared for operator

int a,b,c;

// integer variables a,b & c are declared for operands

clrscr();

// Clears the output Screen

printf("enter two operands:\n");

// Displays the Quoted statement

scanf("%d%d",&a,&b);

// Reads the values of a , b ,c

printf("\n enter an operator:");

// Displays the Quoted statement

scanf(" %c",&op);

// Reads the operator

/* used to select particular case from the user */


switch(op)
{
case '+':printf("sum of two numbers %2d %2d is: %d\n",a,b,a+b); //Displays sum
break;

// Comes out of Case + after Computation completes .

case '-':printf("subtraction of two numbers %2d %2d is: %d\n",a,b,a-b);


//Displays subtraction
break;

// Comes out of Case after Computation completes .

case '*':printf("product of two numbers %2d %2d is: %d\n",a,b,a*b);


//Displays Multiplication
break;

// Comes out of Case * after Computation completes .

case '/':printf("quotient of two numbers %2d %2d is: %d\n",a,b,a/b);


//Displays division
break;

// Comes out of Case / after Computation completes .

case '%':printf("reminder of two numbers %2d %2d is: %d\n",a,b,a%b);


// Displays Remainder
break;

// Comes out of Case % after Computation completes .

Department of Computer Science & Engg

RAGH INSTITUTE OF TECHNOLOGY

default:printf("please enter correct operator");


break;

// Comes out of Case 1 after Computation completes .

}
return(0);

// It returns the value 0

// Main Program Ends

PROCEDURE FOR EXECUTING THE PROGRAM:


Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the program and
quit)
Step 2: Now compile the program by using the following command
cc wk2c.c lcurses
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk2c.c -curses o switch

EXPECTED I/P AND O/P:


Output (1)
enter two operands:2 3
enter an operator:+
sum of two numbers 2 3 is: 5
Output (2)
enter two operands:3 4
enter an operator: subtraction of two numbers 3 4 is: -1

ORIGINAL OUTPUT :
Output (1)
enter two operands:3 5
enter an operator:*
product of two numbers 3 5 is: 15

Department of Computer Science & Engg

RAGH INSTITUTE OF TECHNOLOGY

Output (2)
enter two operands:5 2
enter an operator:/
quotient of two numbers 5 2 is: 2

Output (3)
enter two operands:5 2
enter an operator:%
reminder of two numbers 5 2 is: 1
VIVA VOCE QUESTIONS:
1) What are the various types of arithemetic operators ?
Ans: addition (+), multiplication(*), subtraction (-), division(/) ,

modulo(%).

2) What are the types of relational operators ?


Ans: less than(<), grater than(>), less than or equal to(<=),equal to(==), etc..,

3) What are the types of logical operators ?


Ans: logical AND (&&), logical OR(||), logical NOT

---xXx--

Department of Computer Science & Engg

RAGH INSTITUTE OF TECHNOLOGY

Department of Computer Science & Engg

Anda mungkin juga menyukai