C - Write program to Calculate the reciprocal of each value read from user

Requirements

Write a program that will read five values of type double from the keyboard.

Store them in an array.

Calculate the reciprocal of each value.

Store reciprocal in a separate array

Output the values of the reciprocals and calculate and output the sum of the reciprocals.

Hint

The reciprocal of value x is 1.0/x

Demo

#include <stdio.h>

int main(void)
{
  const int nValues = 5;               // Number of data values
  double data[nValues];                // Stores data values
  double reciprocals[nValues];
  double sum = 0.0;                    // Stores sum of reciprocals

  printf("Enter five values separated by spaces: \n");
  for (int i = 0; i < nValues; ++i)
    scanf("%lf", &data[i]);

  printf("You entered the values:\n");
  for (int i = 0; i < nValues; ++i)
    printf("%15.2lf", data[i]);
  printf("\n");//from   w ww  . j  a va 2 s.c om

  printf("\nThe values of the reciprocals are:\n");
  for (int i = 0; i < nValues; ++i)
  {
    reciprocals[i] = 1.0 / data[i];
    printf("%15.2lf", reciprocals[i]);
  }
  printf("\n\n");

  for (int i = 0; i<nValues; i++)
  {
    sum += reciprocals[i];              // Accumulate sum of reciprocals
    if (i > 0)
      printf(" + ");
    printf("1/%.2lf", data[i]);
  }
  printf(" = %lf\n", sum);
  return 0;
}

Result

Related Exercise