Results 1 to 4 of 4

Thread: My first success with C

  1. #1
    OMF Member
    Join Date
    May 2013
    Location
    Bangladesh
    Posts
    147
    Thanks
    10
    Thanked 8 Times in 8 Posts

    My first success with C

    Hey guys, I learnt some programming basic from my brother and with that basic I had created a basic calculator using programming language C. I can't share the file with you cause I lost it.

  2. #2
    Level 3 alanmark's Avatar
    Join Date
    Apr 2013
    Location
    Cyber Space
    Posts
    392
    Thanks
    59
    Thanked 35 Times in 32 Posts

    Re: My first success with C

    Don't worry, I have found some codes from internet. I am sharing those codes. If have any problems regarding those code, just drop me a PM.


    This simple code calculates addition, subtraction, multiplication and division of two given numbers using switch command.

    Code:
    # include <stdio.h>
    int main()
    {
         char operator;
         float num1,num2;
         printf("Enter operator either + or - or * or divide : ");
         scanf("%c",&operator);
         printf("Enter two operands: ");
         scanf("%f%f",&num1,&num2);
         switch(operator)
         {
         case '+':
                  printf("num1+num2=%.2f",num1+num2);
                  break;
         case '-':
                  printf("num1-num2=%.2f",num1-num2);
                  break;
         case '*':
                  printf("num1*num2=%.2f",num1*num2);
                  break;
         case '/':
                  printf("num2/num1 = %.2f",num1/num2);
                  break;
         default:
    /* If operator is other than +, -, * or /, error message is shown */
                  printf("Error! operator is not correct");
                  break;
         }
         return 0;
    }
    
    I hope this code gonna help you to memories what you have learnt from your brother.

  3. #3
    Level 3 alanmark's Avatar
    Join Date
    Apr 2013
    Location
    Cyber Space
    Posts
    392
    Thanks
    59
    Thanked 35 Times in 32 Posts

    Re: My first success with C

    Just now I have found another code which is using while and if command. Sharing the code below:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float num1, num2;
        char operation;
        while (1) {
            scanf("%f%c%f", &num1, &operation, &num2);
            if (operation == '+')
                printf("%f\n", num1+num2);
            if (operation == '-')
                printf("%f\n", num1-num2);
            if (operation == '*')
                printf("%f\n", num1*num2);
            if (operation == '/')
                printf("%f\n", num1/num2);
        }
        return 0;
    }
    Enjoy the code as well
    Just now add a C++ code to make such a calculator over here: http://offlinemarketingforum.com/c-c...alculator.html, view the code if you are interested.
    Last edited by alanmark; 05-25-2013 at 03:32 AM.

  4. #4
    OMF Member
    Join Date
    May 2013
    Location
    Bangladesh
    Posts
    147
    Thanks
    10
    Thanked 8 Times in 8 Posts

    Re: My first success with C



    Actually I made the calculator a long time ago but I clearly remember that I didn't used switch operator. I think I used the if else command.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •