use c program Write a program that will read five values fro

use c program

Write a program that will read five values from the keyboard (you must use a loop) and store them in an array of type float with the name \"amounts\". Create two arrays of five elements of type long with the names \"dollars\" and \"cents\". Store the whole number part of each value in the \"amounts\" array in the corresponding element of \"dollars\" and the fractional part of the amount as a two-digit integer in \"cents\" (e.g., 2.75 in \"amounts\"[1] would result in 2 being stored in \"dollars\"[1] and 75 being stored in \"cents\"[1]). Output the values from the two arrays of type long as monetary amounts (e.g., $2.75).

Solution


/*
C program that prompts user to enter five amount
values and prints its dollars and cents to console.
*/
//program.c
#include <stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
   //set constant for SIZE
   const int SIZE=5;
   int i;
   //declare array variables
   float amounts[SIZE];
   long dollars[SIZE];
   long cents[SIZE];  
  
   printf(\"Enter amounts of money to convert to dollars and cents\ \");

   for(i=0; i<SIZE; i++)
   {
       //prompt for user amount
       printf(\"Enter amount %d: \", i+1);
       scanf(\"%f\", &amounts[i]);
   }

   for (i=0; i<SIZE; i++)
   {
       //get dollars from amount,truncate fractional
       dollars[i]=(int)amounts[i];
       //get fractional part
       double decimal = 100*(amounts[i]-dollars[i]);
       //round to long
       cents[i]= round(decimal);
       //print dollars and cents
       printf(\"Dollars : %ld cents : %ld\ \",dollars[i],cents[i]);
   }

   getch();
   return 0;
}

------------------------------------------------------------------------------------------------------------------------

Sample Output:

use c program Write a program that will read five values from the keyboard (you must use a loop) and store them in an array of type float with the name \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site