C

C program to Calculate Addition, Subtraction, Product, Quotient2 min read

In the world of programming, performing fundamental arithmetic operations is a common task. In this article, we’ll walk through a simple C program that takes two floating-point numbers as input and computes their sum, difference, product, and quotient. Let’s dive into the code and explore each step.

Code Structure




The program starts by including the standard input-output header file, <stdio.h>, and features the main function where the core logic resides. Variables are declared to store user input (num1 and num2) and the results of arithmetic operations (sum, diff, prod, and quot).

Variable Declaration

We declare floating-point variables to store the user input and the results of arithmetic operations.

User Input

The program prompts the user to enter two floating-point numbers using printf and captures the input using scanf.

Arithmetic Operations

Arithmetic operations are then performed on the entered numbers – addition, subtraction, multiplication, and division.

Division Check

To prevent division by zero, a conditional statement checks if the second number is zero before performing division.

Output

Finally, the program prints the results of the arithmetic operations with a precision of two decimal places.

Conclusion

In conclusion, this C program provides a foundation for performing basic arithmetic operations. Understanding the code structure, variable declaration, user input, and the logic behind arithmetic operations is crucial for anyone starting their programming journey. Feel free to run the program with different inputs to observe the results firsthand. Happy coding!

YouTube

Leave a Comment