Arithmetic operations such as addition, subtraction, multiplication, and division C program

  • First, we declare two integer variables num1 and num2 to store the two numbers to be operated upon.
  • Then, we declare a float variable result to store the result of the arithmetic operation.
  • Next, we declare a character variable operator to store the arithmetic operator to be used.
  • We then use the printf function to prompt the user to enter two numbers and an operator.
  • The scanf function is used to read the input values for the two numbers and the operator.
  • Next, we use a switch statement to perform the arithmetic operation based on the value of the operator variable.
  • In each case of the switch statement, we perform the respective arithmetic operation and print the result using the printf function.
  • If the value of the operator variable is not one of the four arithmetic operators (+, -, *, /), the default case is executed and an error message is displayed.
  • Finally, the return statement is used to return a value of 0 to indicate that the program has run successfully.

Arithmetic operations such as addition, subtraction, multiplication, and division C program
Arithmetic operations such as addition, subtraction, multiplication, and division C program
#include <stdio.h>

int main()
{
  int num1, num2;
  float result;
  char operator;

  printf("Enter two numbers: ");
  scanf("%d %d", &num1, &num2);

  printf("Enter an operator (+, -, *, /): ");
  scanf(" %c", &operator);

  switch(operator)
  {
    case '+':
      result = num1 + num2;
      printf("%.2f\n", result);
      break;

    case '-':
      result = num1 - num2;
      printf("%.2f\n", result);
      break;

    case '*':
      result = num1 * num2;
      printf("%.2f\n", result);
      break;

    case '/':
      result = (float) num1 / num2;
      printf("%.2f\n", result);
      break;

    default:
      printf("Invalid operator\n");
      break;
  }

  return 0;
}
admin https://study-from-here.com

Digital Marketing Consultants and Social Media Marketing Expert with over 3 years of rich experience in various Branding, Promotions, business directories, On pages and off page optimization, Link building Advertising, Research, paid advertisement, content writing and marketing

You May Also Like

More From Author

25Comments

Add yours
  1. 1
    Asian wedding photographer in London

    […] Finally, don't forget to enjoy your day! The more relaxed and natural you look in your photos, the more they will capture the happy memories of your wedding day. written by the owner of study from here he wrote amazing article on addition subtraction multiplication division program in c […]

  2. 2
    Asian wedding photographer in Leicester - phillynes

    […] Once your wedding photos are ready, be sure to take the time to review them with your photographer. This is the perfect opportunity to discuss any concerns you have and to make sure that you are happy with the final results. By taking the time to review the photos, you can be sure that you will love your wedding photos for years to come. written by the owner of study from here he wrote amazing article on addition subtraction multiplication division program in c […]

  3. 6
    The Connection Between Addition, Subtraction, Multiplication, Di – Best in Business .App

    […] Sensible Functions and Actual-World Impression:Addition, subtraction, multiplication, and division in C aren’t simply theoretical ideas; they’ve sensible functions and real-world affect. These operations are on the coronary heart of quite a few software program functions, algorithms, simulations, and mathematical fashions that clear up actual issues and enhance lives. Figuring out that one’s work has tangible results and contributes to the betterment of society can present a deep sense of objective and happiness addition subtraction multiplication division program in c. […]

  4. 7
    The #1 Misconception About Addition, Subtraction, Multipl – Best in Business .App

    […] One other widespread false impression is assuming that division by zero will at all times end in an error or exception. In C, dividing a non-zero quantity by zero results in undefined conduct. The consequence can range relying on the compiler or system. It’s important to validate enter and implement applicable error dealing with to forestall division by zero eventualities addition subtraction multiplication division program in c. […]

  5. 9
    zoritoler imol

    I have to show my thanks to the writer just for bailing me out of this particular crisis. As a result of looking out throughout the world wide web and meeting solutions which were not helpful, I thought my entire life was done. Being alive minus the answers to the difficulties you’ve fixed all through your main blog post is a crucial case, as well as the kind which might have negatively damaged my career if I hadn’t discovered the website. Your personal natural talent and kindness in maneuvering every aspect was helpful. I don’t know what I would have done if I had not come across such a solution like this. I am able to at this moment relish my future. Thank you very much for your high quality and results-oriented help. I won’t think twice to endorse your web blog to anyone who should receive recommendations on this topic.

  6. 10
    Synthia Castanado

    Its like you learn my thoughts! You appear to know so much about this, like you wrote the ebook in it or something. I feel that you simply can do with some p.c. to pressure the message home a bit, but instead of that, that is great blog. A great read. I will definitely be back.

  7. 11
    click here

    Good – I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your client to communicate. Excellent task..

  8. 12
    Warung138

    Great ?V I should certainly pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related information ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, site theme . a tones way for your customer to communicate. Excellent task..

  9. 13
    Warung138

    I am really loving the theme/design of your blog. Do you ever run into any browser compatibility problems? A few of my blog readers have complained about my site not operating correctly in Explorer but looks great in Chrome. Do you have any advice to help fix this issue?

  10. 16
    Slotbom77

    I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  11. 18
    KKSLOT777

    Nice post. I was checking continuously this weblog and I’m inspired! Extremely helpful information specially the last phase 🙂 I care for such info much. I was seeking this particular info for a very lengthy time. Thanks and best of luck.

  12. 19
    KKSLOT777

    I have not checked in here for some time as I thought it was getting boring, but the last few posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

  13. 20
    Kedai138

    I’ll right away seize your rss feed as I can’t to find your email subscription link or e-newsletter service. Do you have any? Please allow me understand in order that I may subscribe. Thanks.

  14. 25
    On The Fly

    I haven¦t checked in here for a while because I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

+ Leave a Comment