Complementary task for topic: 1

M Nemeth · 2023-08-29 15:21:04.603220'

scanf: float I.

scanf: float I.

Task: Calculate the product of three floating-point numbers:
Write a C program that prompts the user to enter three floating-point numbers using scanf() and calculates their product. Display the product using printf().

Hint: scanf() works like printf(), but you need a & sign before the variable.
Later you will understand that is needed because the scanf needs a place to STORE the variable, not the value of the variable, & sign is an operator that gives us the memory address. This address is needed to be passed to scanf() so scanf can SAVE the read value there. (and so the value can change)

Solution
#include 

int main() {
    float num1, num2, num3;
    
    printf("Enter the first floating-point number: ");
    scanf("%f", &num1);
    
    printf("Enter the second floating-point number: ");
    scanf("%f", &num2);
    
    printf("Enter the third floating-point number: ");
    scanf("%f", &num3);
    
    float product = num1 * num2 * num3;
    
    printf("The product of %.2f, %.2f, and %.2f is %.2f.\n", num1, num2, num3, product);
    
    return 0;
}



Explanation
In this example, the program uses printf() to prompt the user to enter three floating-point numbers. The scanf() function is used to scan the input from the user and store it in the num1, num2, and num3 variables. The program calculates the product of the three numbers and stores it in the product variable. Then, it uses printf() to display the product in the specified format.
< < previous    next > >