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

Estimated read time 3 min read
  • 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

8Comments

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. […]

+ Leave a Comment