Calculator program in C

Discussion in 'C' started by coderzone, Apr 5, 2006.

  1. matuatua

    matuatua New Member

    Joined:
    Aug 31, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    more of this stuffs shabbir but i really do need codes in c programming to try my hands on it please i really need it badly
     
  2. hhyy6623

    hhyy6623 New Member

    Joined:
    Sep 20, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    got a little problem here, it doesnt end when i input "n" where it asks me wanna countinues or not
     
  3. matuatua

    matuatua New Member

    Joined:
    Aug 31, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    it works perfectly cos i have tried it
     
  4. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    but i copy the code into the wxDEV_C++,can not compile successfully
    if(choice=='y'||choice=='Y')
    //main();
    something wrong in "main(); ",could you tell me how to modify it?thank you !!
     
  5. ratnesh

    ratnesh New Member

    Joined:
    Oct 3, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    lucknow (up)
     
  6. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    i forgot~~i wanted to say,i made a project in wxDEV_C++,and copyed the code in it,the wxDEV_C++ told me could not compile successfully;then i thought something was wrong in
    "if(choice=='y'||choice=='a') main();",
    as far as i concerned, the "main()" can not use again in one programme,
     
  7. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    At last i find a new solution solve my problem which i proposed yesterday,using the "goto";code is as follows:
    Code:
    #include<stdio.h>
    float add(float,float);
    float sub(float,float);
    float product(float,float);
    float divide(float,float);
    int main(int argc, char *argv[])
    {  
     float n1,n2;    
     char sym,choice;
                
     printf("This Program is a program for calculator\n\n");
        start_again: 
              
     scanf("%f%c%f",&n1,&sym,&n2);    
     if(sym=='+')       
        printf("\n%f",add(n1,n2));  
     if(sym=='-')       
        printf("\n%f",sub(n1,n2)); 
     if(sym=='*')     
        printf("\n%f",product(n1,n2));   
     if(sym=='/')      
         printf("%f",divide(n1,n2));   
     printf("\nDo you wish to continue[y/n]");   
     scanf("%s",&choice);    
     if(choice=='y'||choice=='Y')     
            goto start_again;
            
        system("PAUSE"); 
        return 0;
    }
    float add(float m1,float m2)
    {    return(m1+m2);}
    float sub(float m1,float m2)
    {    return(m1-m2);}
    float product(float m1,float m2)
    {    return(m1*m2);}
    float divide(float m1,float m2)
    {    return(m1/m2);}
     
    Last edited by a moderator: Oct 3, 2010
  8. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    i add some functioins and program a new code that based on codezone as follows:
    Code:
    #include<stdio.h>
    float add(float,float);
    float sub(float,float);
    float product(float,float);
    float divide(float,float);
    int main(int argc, char *argv[])
    {  
     float n1,n2;    
     char sym,choice;
                
     printf("This Program is a program for calculator\n");
     printf("Please input two numbers like this:4+5 or 4*5\n");
        start_again: 
              
     scanf("%f%c%f",&n1,&sym,&n2);    
     if(sym=='+')       
        printf("\n%f",add(n1,n2));  
     if(sym=='-')       
        printf("\n%f",sub(n1,n2)); 
     if(sym=='*')     
        printf("\n%f",product(n1,n2));   
     if(sym=='/')      
         printf("%f",divide(n1,n2));   
     printf("\nDo you wish to continue[y/n]\n");
     input:
        scanf("%s",&choice);
     
        if(choice=='n'||choice=='N')
        {    
            printf("Good Luck!!ByeBye!~\n");
            goto end;   
        }
        else if(choice=='y'||choice=='Y')
        { 
            printf("Please Contiue:\n");
            goto start_again;
        }
     else if(choice!='n'||choice!='N'||choice!='y'||choice!='Y')
        {
            printf("Wrong code!\n");
            printf("Input again:\n");
            goto input;
        }    
        end:
        system("PAUSE"); 
        return 0;
    }
    float add(float m1,float m2)
    {    return(m1+m2);}
    float sub(float m1,float m2)
    {    return(m1-m2);}
    float product(float m1,float m2)
    {    return(m1*m2);}
    float divide(float m1,float m2)
    {    return(m1/m2);}
    welcome to give some advice!!
     
    Last edited by a moderator: Oct 3, 2010
  9. Harish723

    Harish723 New Member

    Joined:
    Oct 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Ok its alright
     
  10. zaster

    zaster New Member

    Joined:
    Oct 9, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Okay, I understand its meant to be a bare bones sort of program, but still you should include a check for division by 0.
     
  11. Deucel

    Deucel New Member

    Joined:
    Feb 14, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Washington State
    Thanks, and it is good, but...
    To give the program some readability:

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
      float n1,n2,s,d,p,q;
      printf("\n Enter two numbers:  ");
      scanf("%f %f", &n1, &n2);
      s=n1+n2;
      d=n1-n2;
      p=n1*n2;
      q=n1/n2;
      printf("\n Sum        %9.2f",s);
      printf("\n Difference %9.2f",d);
      printf("\n Product    %9.2f",p);
      printf("\n Quotient   %9.2f",q);
      getch();
      return(0);
    }
     
    Last edited by a moderator: Feb 14, 2011
  12. Naveed Marwat

    Naveed Marwat New Member

    Joined:
    Feb 16, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
  13. alexsmth114

    alexsmth114 New Member

    Joined:
    Mar 7, 2011
    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    0
    Excellent post, couldn't have asked for more!!..
     
  14. sura

    sura Banned

    Joined:
    Aug 4, 2011
    Messages:
    47
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    India,Tamil Nadu.
    very good
    i also like this ..
    its good...
     
  15. rioeduardo

    rioeduardo New Member

    Joined:
    Feb 18, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for sharing.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice