Home Forums C Programming calculator programme

Viewing 2 reply threads
  • Author
    Posts
    • #2022
      mrwales
      Participant

      can any body get me calculator program in C-lang??Is it possible

    • #3262
      yeswanth
      Participant
      #include
      #include
      #include
      #include
      #include
      #include
      void main()
      {
      int gd=0,gm,i,j,answer,flag=0,k;
      char prev='1',current,*ch;
      char *nums[]={" 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 0 "};
      char *num[]={"1","2","3","4","5","6","7","8","9","0"};
      initgraph(&gd,&gm,"");
      button b[10],add,sub,exit,equal,mul,div,ac;
      textbox t;
      initmouse();
      showmouse();
      t.make(150,50,160);
      for(i=0,j=100;i<10;i++,j+=40)
      b.make(j,100,nums);
      add.make(100,150," + ");
      sub.make(140,150," - ");
      equal.make(260,150," = ");
      mul.make(180,150," * ");
      div.make(220,150," / ");
      exit.make(350,400," EXIT ");
      ac.make(300,150," AC ");
      j=0;
      while(1)
      {
      setcolor(WHITE);
      while(!flag)//This while terminates when user is finished inputting a number
      //i.e. when he presses '+', '-', '*', '/'
      {
      for(i=0;i<10;i++)
      {
      if(b.click())
      {
      t.append(num),j++,k=1;//display the number in textbox
      break;
      }
      }
      if(j==1&&k==1)
      t.write(""),t.append(num);
      if(add.click())
      current='+',flag=1;
      else if(sub.click())
      current='-',flag=1;
      else if(equal.click())
      current='=',flag=1;
      else if(mul.click())
      current='*',flag=1;
      else if(div.click())
      current='/',flag=1;
      else if(exit.click())
      flag=2;// flag==2 indicates user wants to exit.
      else if(ac.click())
      {
      prev='1';
      t.write("");
      }
      k=0;
      }//End of second while loop
      //Now Calculate the result.
      if(flag==2)
      break;//flagg==2, go out of the main loop to exit.
      if(prev=='1')
      answer=atoi(t.text);
      else if(prev=='+')
      answer+=atoi(t.text);
      else if(prev=='-')
      answer-=atoi(t.text);
      else if(prev=='*')
      answer*=atoi(t.text);
      else if(prev=='/'&&(atoi(t.text))==0)
      msgbox("Cannot divide by zero"),prev='1',t.write("");
      else if(prev=='/')
      answer/=atoi(t.text);
      flag=0,j=0;
      prev=current;
      itoa(answer,ch,10);
      t.write(ch);
      }
      closegraph();
      }
    • #3263
      yeswanth
      Participant

      #include
      #include
      #include
      main()
      {
       int a,b,sum,diff,prdct,ch;
       double sr;
       long sq,cube;
       float c,d,ratio;
       do
        {
          clrscr();
          printf("t*****Ruel Pagayon Copyright--2007--04--10--*****");
          printf("nnntt*****Simple Calculator*****nn");
          printf("tt**Operations**n");
          printf("ttt*(1)Additionn");
          printf("ttt*(2)Subtractionn");
          printf("ttt*(3)Multiplicationn");
          printf("ttt*(4)Divisionn");
          printf("ttt*(5)Squaren");
          printf("ttt*(6)Cuben");
          printf("ttt*(7)SquareRootn");
          printf("nEnter the number of your choice: ");
          scanf("%d", &ch);
          switch(ch)
          {
          case 1:
          printf("nttEnter two numbers: ");
          scanf("%d %d", &a,&b);
          sum = a + b;
          printf("nnttThe Sum is : %d", sum);
          break;
          case 2:
          printf("nttEnter two numbers: ");
          scanf("%d %d", &a,&b);
          diff = a - b;
          printf("nnttThe Difference is : %d", diff);
          break;
          case 3:
          printf("nttEnter two numbers: ");
          scanf("%d %d", &a,&b);
          prdct = a * b;
          printf("nnttThe Product is : %d", prdct);
          break;
          case 4:
          printf("nttEnter two numbers: ");
          scanf("%f %f", &c,&d);
          ratio = c / d;
          printf("nnttThe Quotient is : %f", ratio);
          break;
          case 5:
          printf("nttEnter a number: ");
          scanf("%d", &a);
          sq = a * a;
          printf("nnttThe Square of %d is : %d", a,sq);
          break;
          case 6:
          printf("nttEnter a number: ");
          scanf("%d", &a);
          cube = a * a * a;
          printf("nnttThe Cube of %d is : %d", a,cube);
          break;
          case 7:
          printf("nttEnter a number: ");
          scanf("%d", &a);
          sr = sqrt(a);
          printf("nnttThe SquareRoot of %d is : %f", a,sr);
          break;
          }
        printf("nnnnntttPress 'x' to quit, any other key to continuen");
        }
       while (getch() != 'x');
      }
Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.