Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Calculate the Average Value of Two Items in C

write a simple C program that can accept two item's weight and number of purchases (floating point values) and calculate their average value.

If you are learning C programming, you might have encountered some problems that require you to perform some calculations involving floating point values. Floating point values are numbers that can have a decimal part, such as 1.5, 3.14 or 0.01. For example, how do you calculate the average value of two items that have different weights and number of purchases?

In this program, I will show you how to write a simple C program that can accept two item's weight and number of purchases (floating point values) and calculate their average value. You will also learn some basic concepts of C programming, such as variables, data types, operators, input/output and comments.

What are the steps to calculate the average value of two items in C?

To calculate the average value of two items in C, we need to follow these steps:

  • Declare five floating point variables: `w1, w2, n1, n2 and avg.
  • Ask the user to enter the weight and number of purchases of the first item and store them in the variables w1 and n1.
  • Ask the user to enter the weight and number of purchases of the second item and store them in the variables w2 and n2.
  • Calculate the average value using the formula: (w1 * n1 + w2 * n2) / (n1 + n2) and store the result in the variable avg.
  • Print the result using the printf function.

C program

Now that we know the steps to calculate the average value of two items in C, let's write the C program using these steps. Here is the complete code:

#include <stdio.h>

int main()

{

    float w1, w2, n1, n2, avg;

    printf("Enter the weight and number of purchases of the first item: ");

    scanf("%f %f", &w1, &n1);

    printf("Enter the weight and number of purchases of the second item: ");

    scanf("%f %f", &w2, &n2);

    avg = (w1 * n1 + w2 * n2) / (n1 + n2); // calculate the average value

    printf("The average value of the items is %.2f\n", avg);

    return 0;

}

Explanation

Let's go through each line of code and understand what it does.

  1. The first line #include <stdio.h> is a preprocessor directive that tells the compiler to include the standard input/output header file. This file contains some functions that we will use later in our program, such as printf and scanf.
  2. The second line int main() is the main function of our program. Every C program must have a main function where the execution starts.
  3. The third line { marks the beginning of the main function body.
  4. The fourth line float w1, w2, n1, n2, avg; declares five floating point variables: w1, w2, n1, n2 and avg. A floating point variable can store a number that can have a decimal part, such as 1.5, 3.14 or 0.01. We use these variables to store the input and output values of our program.
  5. The fifth line printf("Enter the weight and number of purchases of the first item: "); uses the printf function to print a message on the screen. The message is enclosed in double quotes and ends with a semicolon. The message asks the user to enter the weight and number of purchases of the first item.
  6. The sixth line scanf("%f %f", &w1, &n1); uses the scanf function to read two floating point values from the user and store them in the variables w1 and n1. The %f is a format specifier that tells the function what type of value to expect. The & is an operator that gives the address of a variable. The function needs this address to store the value in that variable. The semicolon marks the end of a statement.
  7. The seventh line printf("Enter the weight and number of purchases of the second item: "); uses the printf function to print another message on the screen. The message asks the user to enter the weight and number of purchases of the second item.
  8. The eighth line scanf("%f %f", &w2, &n2); uses the scanf function to read two more floating point values from the user and store them in the variables w2 and n2.
  9. The ninth line avg = (w1 * n1 + w2 * n2) / (n1 + n2); calculates the average value using the formula: (w1 * n1 + w2 * n2) / (n1 + n2) and stores the result in the variable avg. The * is an operator that performs multiplication, the / is an operator that performs division, and the = is an operator that assigns a value to a variable. The tenth line printf("The average value of the items is %.2f\n", avg); uses the printf function to print the result on the screen. The function takes a format string and a list of values as arguments. The format string contains some placeholders that are replaced by the values in the list. For example, the first %f is replaced by the value of avg, and the %.2f specifies that the value should be rounded to two decimal places. The \n is a special character that represents a new line. It moves the cursor to the next line after printing.
  10. The eleventh line return 0; ends the main function and returns a value of 0 to indicate that the program ran successfully.
  11. The twelfth line } marks the end of the main function body.

Output

Enter the weight and number of purchases of the first item: 1.5 3

Enter the weight and number of purchases of the second item: 2.0 4

The average value of the items is 1.79

Enter the weight and number of purchases of the first item: 0.5 2

Enter the weight and number of purchases of the second item: 1.0 1

The average value of the items is 0.67

Enter the weight and number of purchases of the first item: 1.2 0

Enter the weight and number of purchases of the second item: 0.8 5

The average value of the items is 0.80

Conclusion

In this program, you learned how to write a C program that can accept two item's weight and number of purchases (floating point values) and calculate their average value. You also learned some basic concepts of C programming, such as variables, data types, operators, input/output and comments.

I hope you found this blog post useful and interesting. If you have any questions or feedback, please leave a comment below. Thank you for reading!



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld