0% found this document useful (0 votes)
28 views2 pages

939314

តុុុ

Uploaded by

hengsamnang7077
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views2 pages

939314

តុុុ

Uploaded by

hengsamnang7077
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

C calculator program using functions

Write a program in c to create a calculator with basic operations using functions. Simple calculator program in c using switch case and functions. Write a c program to design the scientific calculator using built-
in functions. Write a program to implement simple calculator using functions in c. Write a c program for calculator application using functions. Calculator program in c using functions. Menu driven calculator
program in c using functions. C program to make a calculator using functions. Scientific calculator using built-in functions in c program.

This program creates a simple menu-driven calculator using switch statements and functions. ```c #include void add(double num1, double num2) { printf("%.1lf + %.1lf = %.1lf\n", num1, num2, num1 + num2); } void subtract(double num1, double num2) { printf("%.1lf - %.1lf = %.1lf\n", num1, num2, num1 - num2); } void multiply(double num1,
double num2) { printf("%.1lf * %.1lf = %.1lf\n", num1, num2, num1 * num2); } void divide(double num1, double num2) { if (num2 != 0) { printf("%.1lf / %.1lf = %.1lf\n", num1, num2, num1 / num2); } else { printf("Error! Division by zero is not allowed.\n"); } } int main() { char op; double num1, num2; printf("Enter an operator (+, -, *, /): "); scanf("
%c", &op); printf("Enter two operands: "); scanf("%lf %lf", &num1, &num2); switch (op) { case '+': add(num1, num2); break; case '-': subtract(num1, num2); break; case '*': multiply(num1, num2); break; case '/': divide(num1, num2); break; default: printf("Error! Operator not found.\n"); } return 0; } ``` Simple Calculator Program using Switch Case
and Functions The purpose of this program is to create a simple calculator that can perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The user inputs two numbers and an operator (+, -, *, /), and the program calculates the result based on the selected operation. Here's the code: ```c #include float
add(float num1, float num2); float sub(float num1, float num2); float mult(float num1, float num2); float div(float num1, float num2); int main() { char op; float num1, num2, result = 0.0f; /* Print welcome message */ printf("WELCOME TO SIMPLE CALCULATOR"); printf("----------------------------"); printf("Enter [number 1] [+ - * /] [number 2]"); /* Input two
number and operator from user */ scanf("%f %c %f", &num1, &op, &num2); switch(op) { case '+': result = add(num1, num2); break; case '-': result = sub(num1, num2); break; case '*': result = mult(num1, num2); break; case '/': result = div(num1, num2); break; default: printf("Invalid operator"); } /* Print the result */ printf("%.2f %c %.2f = %.2f",
num1, op, num2, result); return 0; } float add(float num1, float num2) { return num1 + num2; } float sub(float num1, float num2) { return num1 - num2; } float mult(float num1, float num2) { return num1 * num2; } float div(float num1, float num2) { return num1 / num2; } ``` The program is designed to be user-friendly and easy to understand. It first
prints a welcome message, then prompts the user to input two numbers and an operator (+, -, \*, /). The program then uses a switch case statement to determine which arithmetic operation to perform based on the selected operator. Finally, the result of the calculation is printed out in a neat format, including the original numbers, the operator used,
and the calculated result. The article discusses the creation of a basic calculator program in C, focusing on arithmetic operations such as addition, subtraction, multiplication, and division. To expand its functionality, the program will incorporate advanced calculations like exponentiation and square root calculations. The tutorial aims to provide clear
explanations and code examples, making it accessible for both beginners and experienced programmers. The article outlines a step-by-step approach to building a simple calculator program in C: 1. Declare variables: number1, number2, answer, and operation. 2. Print a statement asking the user for two integers. 3. Request input by asking for
numbers 1 and 2. 4. Print options for addition, subtraction, division, multiplication, and other operators. 5. Take the user's operator preference. 6. Calculate the outcome and store it in the answer variable. 7. Display the solution. 8. Exit the application. The article presents three approaches to writing a simple calculator program in C: 1. Using if-else
statements 2. Using a switch-case statement 3. Using functions and switch-case The first approach, demonstrated through code examples, uses if-else statements to determine the chosen operator and perform the corresponding arithmetic operation. It handles invalid operators gracefully and terminates execution if an error occurs. and division will
be carried out according to whether the provided operator is -.* or /.* Since we cannot divide any integer by zero, we shall verify additional conditions in a division that number 2 is not zero. We prompt the user to enter a valid operator if the requested operator is not one of the available operators. We can see from the result that we started by taking
the operator input, which is -.* In the following step, we took the numbers 23 and 11, subtracted them, and then printed the result. The Simple Calculator Program in C using the Switch Case. In this program, the user is prompted to enter an operator (+, -, *, or /) and two numbers. The switch case statement is used to determine the chosen operator
and perform the corresponding arithmetic operation. Similar to the if-else version, the program checks for division by zero when the division operator is chosen. Finally, the result of the calculation is displayed on the screen. The program handles invalid operators gracefully and terminates execution if an error occurs. Let’s see how to write code for a
calculator program in C. Calculator Program Using Switch #include int main() { int number1, number2; float answer; char op; printf("Enter the operation to perform(+, -, *, /) "); scanf("%c", &op); printf("Enter the first number: "); scanf(" %d", &number1); printf("Enter the second number: "); scanf(" %d", &number2); switch(op){ //addition case '+':
answer = number1 + number2; printf(" %d + %d = %f", number1, number2, answer); break; //substraction case '-': answer = number1 - number2; printf(" %d - %d = %f", number1, number2, answer); break; //multiplication case '*': answer = number1 * number2; printf(" %d * %d = %f", number1, number2, answer); break; //division case '/': if
(number2 == 0) { printf(" Divisor cannot be zero. Please enter another value "); scanf("%d", &number2); } answer = number1 / number2; printf(" %d / %d = %.2f", number1, number2, answer); break; default: printf(" Enter valid operator "); } return 0; } Output Enter the operation to perform(+, -, *, /) - Enter the first number: 23 Enter the second
number: 11 23 - 11 = A Simple Calculator Program in C is created using functions. The program prompts the user to enter an operation (+, -, *, /) and two numbers. It then uses a switch case statement to determine the chosen operator and call the appropriate function. The `main` function asks the user for the operation, reads the input operator with
`scanf`, and then uses a `switch` statement to perform the requested action based on the operator entered. For addition, subtraction, multiplication, and division, separate functions (`addition()`, `subtraction()`, `multiplication()`, `division()`) are defined in the program. Each function prompts the user for two numbers, performs the calculation, and
displays the result. The program handles invalid operators by prompting the user to enter a valid operator. It also checks for division by zero and asks the user to provide another value if the divisor is zero. Your programming journey, the knowledge and techniques gained from building a calculator program can be applied to various programming
projects. In conclusion, this calculator program in C serves as an excellent exercise to sharpen your coding skills and solidify your understanding of programming fundamentals. **Frequently Asked Questions** Q1: What is a calculator program in C? A calculator program in C is a software application that allows users to perform basic arithmetic
calculations using the C programming language. Q2: What are the fundamental concepts involved in building a calculator program in C? Building a calculator program in C involves understanding concepts such as user input, control flow, arithmetic operations, and function implementation. Q3: Can a calculator program in C handle more complex
mathematical operations? Yes, a calculator program in C can be expanded to handle more complex mathematical operations, including scientific functions, decimal numbers, or memory functions. Q4: How can I handle errors, such as division by zero, in a calculator program? To handle errors like division by zero, you can incorporate conditional
statements or error-checking mechanisms before performing division.

You might also like