Programming in C 312303 Manual Practical No.3 arithmetic operators to solve arithmetic operations answer
Hey, students welcome to our site, in this blog post we are going to share programming in c manual practical no.3 in which we answer all questions related to implementing C programs using arithmetic operators to solve given arithmetic operations.
This practical is useful for students to understand arithmetic expressions. Students will be able to use arithmetic expressions in the C program.
Theoretical Background of Programming in C 312303 Manual Practical No.3
Algorithms are like step-by-step instructions for solving a problem. Variables are like containers that hold different values, while constants are values that stay the same. Arithmetic operators are symbols that help us do math in a computer program, like adding, subtracting, multiplying, and dividing.
What are C Arithmetic Operators?
The C arithmetic operators are the symbols that are used to perform mathematical operations on operands. There are a total of 9 arithmetic operators in C to provide the basic arithmetic operations such as addition, subtraction, multiplication, etc.
Types of Arithmetic Operators in C
The C Arithmetic Operators are of two types based on the number of operands they Work.
These are as follows:
• Binary Arithmetic Operators
• Unary Arithmetic Operators
Exercise
Write a program in C for:
1. Celsius to Fahrenheit conversion.
Answer:
#include <stdio.h>int main() {float celsius, fahrenheit;// Get Celsius temperature from userprintf(“Enter temperature in Celsius: “);scanf(“%f”, &celsius);// Convert Celsius to Fahrenheitfahrenheit = (celsius * 9.0 / 5.0) + 32;// Print the resultprintf(“%.2f degrees Celsius is equal to %.2f degrees Fahrenheit.n”, celsius, fahrenheit);return 0;}
2. Area calculation of rectangle
Answer:
#include <stdio.h>int main() {float length, width, area;// Prompt for inputprintf(“Enter the length of the rectangle: “);scanf(“%f”, &length);printf(“Enter the width of the rectangle: “);scanf(“%f”, &width);// Calculate areaarea = length * width;// Print the areaprintf(“The area of the rectangle is: %.2f square unitsn”, area);return 0;}
3. Area calculation of circle with PI value constant and #define function.
Answer:
#include <stdio.h>#define PI 3.14159 // Define PI constantint main() {double radius, area;// Prompt user for radiusprintf(“Enter the radius of the circle: “);scanf(“%lf”, &radius);// Calculate area using PI constantarea = PI * radius * radius;// Print the areaprintf(“Area of the circle: %.2lfn”, area);return 0;}
Algorithm
- Start: Begin program execution.
- Define PI: Use the #define directive to define a constant named PI with the value of pi (approximately 3.14159).
- Declare Variables: Declare a floating-point variable radius to store the circle’s radius.
- Read Input: Prompt the user to enter the radius value using printf and read the input using scanf.
- Calculate Area: Calculate the area of the circle using the formula area = PI * radius * radius.
- Print Output: Use printf to display the calculated area of the circle with a descriptive message.
- End: Terminate the program execution.
Flow Chart:
+—————–+
| Start |
+—————–+
|
v
+—————–+
| Define PI (3.14159) | (#define PI)
+—————–+
|
v
+—————–+
| Prompt for Radius |
| “Enter radius: ” |
+—————–+
|
v
+—————–+
| Read Input (scanf) |
| %f |
+—————–+
|
v
+—————–+
| Calculate Area |
| area = PI * r * r |
+—————–+
|
v
+—————–+
| Print Area (printf)|
| “Area: %.2f” |
+—————–+
|
v
+—————–+
| End |
+—————–+
Practical Related Questions
I. Write an error message given by C compiler during program compilation if you use %d to read float variable.
Answer:
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘float’ [-Wformat=]
II. Write a program to print square of given number.
Answer:
#include <stdio.h>int main() {int num;// Prompt user for inputprintf(“Enter a number: “);scanf(“%d”, &num);// Calculate squareint square = num * num;// Print the squareprintf(“The square of %d is %dn”, num, square);return 0;}
III. Evaluate the following expressions and show their hierarchy.
G=big/2+big*4/big -big + abc / 3;
( abc =2.5, big=2, assume G to be an float)
Answer:
- Expression:
G = big / 2 + big * 4 / big – big + abc / 3;
- Variable Values:
abc = 2.5 (float)
big = 2 (int)
G (assumed to be float)
Hierarchy and Evaluation:
Divisions and Multiplication (Left to Right Associativity):
big / 2 (integer division, since big is int): 2 / 2 = 1 (becomes float due to assignment to G)
big * 4 / big: 2 * 4 / 2 = 4 (becomes float due to assignment to G)
Subtraction and Addition (Left to Right Associativity):
The entire expression is evaluated from left to right:
1 (from big / 2) + 4 (from big * 4 / big): 1 + 4 = 5
5 – big: 5 – 2 = 3
3 + abc / 3: 3 + 2.5 / 3 = 3 + 0.833333… ≈ 3.833333…
Final Result:
G ≈ 3.833333…
IV. Convert the following mathematical formula into appropriate C statements.
X=-b (b*b)24ac/2a
Answer:
#include <stdio.h>#include <math.h> // For pow() functionfloat calculate_X(float a, float b, float c) {float X;// Calculate the square of bfloat b_squared = pow(b, 2.0);// Calculate the complete expression in the numeratorfloat numerator = -b * b_squared * 24.0 * c;// Calculate the denominatorfloat denominator = 2.0 * a;// Calculate X by dividing numerator by denominatorX = numerator / denominator;return X;}int main() {float a, b, c, result;// Prompt user for input valuesprintf(“Enter values for a, b, and c: “);scanf(“%f %f %f”, &a, &b, &c);// Call the calculate_X functionresult = calculate_X(a, b, c);// Print the resultprintf(“The value of X is: %.2fn”, result);return 0;}
Conclusion
We successfully answered all questions of Programming in C 312303 Manual Practical No.3 and write all c programming codes with the help of teachers.