Wednesday 12 September 2012

Two integer operands and one operator form user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)


Description:
To take the two integer operands and one operator from user to perform the some arithmetic operations by using the following operators like +,-,*, /, %
Ex: 2+3=5
Algorithm:
Step 1: Start

Step 2: Read the values of a,b and operator

Step 3: if the operator is ‘+’ then
                        R=a+b
                        Go to step 8
                        Break

Step 4: Else if the operator is ‘-‘ then
                        R=a-b
                        Go to step 8

Step 5: Else if the operator is ‘*‘ then
                        R=a*b
                        Go to step 8

Step 6: Else if the operator is ‘/‘ then
                        R=a/b
                        Go to step 8

Step 7: Else if the operator is ‘%‘ then
                        R=a%b
                        Go to step 8

Step 8: write  R

Step 9:End

 Program:

#include<stdio.h>
main()
{
char op;
float a,b,c;
clrscr();
printf("enter two operands:");
scanf("%d%d",&a,&b);
printf("enter an operator:");
scanf(" %c",&op);
switch(op) // used to select particular case from the user
{
case '+':printf("sum of two numbers %2d %2d is:     %d",a,b,a+b);
                      break;
case '-':printf("subtraction of two numbers %2d %2d is:
                        %d",a,b,a-b);
                      break;
case '*':printf("product of two numbers %2d %2d is:
                       %d",a,b,a*b);
                      break;
         case '/':printf("quotient of two numbers %2d %2d is:
                       %d",a,b,a/b);
                       break;
          case '%':printf("reminder of two numbers %2d %2d is:             
                        %d",a,b,c);
                         break;
           default:printf("please enter correct operator");
                         break;
          }
  getch();
}

Input/Output:


1.enter two operands:2 3
    enter an operator:+
    sum of two numbers  2  3 is: 5

2.enter two operands:3 4
   enter an operator: -
     subtraction of two numbers  3  4 is: -1

3.enter two operands:3 5
    enter an operator:*
     product of two numbers  3  5 is: 15

4.enter two operands:5 2
   enter an operator:/
   quotient of two numbers  5  2 is: 2
 5.  enter two operands:5 2
     enter an operator:%
     reminder of two numbers  5  2 is: 1
conclusion: The program is error free



VIVA QUESATIONS:

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)            3) What are the types of  logical operators ?
Ans: logical AND (&&), logical OR(||), logical NOT(!)

No comments:

Post a Comment