Wednesday, February 5, 2014

Choice Program [using if-else]

//Choice Program[simple condition testing]
#include<stdio .h>
//#include<conio .h>
 void main()
{
  int a,b,ch,sum,dif,prod,quot;
  //clrscr();
  printf("\n Enter the two numbers and your choice");
  printf("\n 1 --> SUM\n 2--> DIFFRENCE");
  printf("\n 3 --> PRODUCT\n 2--> DIVIDE \n");
  scanf("%d%d%d",&a,&b,&ch);
 //The same program maybe written using switch case statements
   if(ch==1)
  {
  sum=a+b;
  printf("Sum [%d + %d] : %d\n",a,b,sum);
  }
   else if(ch==2)
  {
  dif=a-b;
  printf("Difference [%d - %d] : %d\n",a,b,dif);
  }
   else if(ch==3)
  {
  prod=a*b;
  printf("Product [%d * %d] : %d",a,b,prod);
  }
   else if(ch==4)
  {
   if(b==0)
   {
   printf("\n Divide by zero error");
   exit(1);
   }
   quot=a/b;
   printf("Quotient [%d / %d] : %d",a,b,quot);
  }
   else
        printf("\n Invalid Input \n");

  //getch();
}


No comments:

Post a Comment