Write a program that will read five values from the keyboard 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:
#include <stdio.h>
#include <math.h>
int main()
{
int i, j;
float amounts[5];
printf("Enter numbers as dollar amounts: ");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
float ipart;
for (i=0; i<5; i++) {
cents[i] = (int) roundf(modff(amounts[i], &ipart)*100.0f);
dollars[i] = (int) ipart;
if(cents[i]==100){
dollars[i]++;
cents[i]=0;
}
}
for (i=0; i<5; i++) {
printf(" $%ld.", dollars[i]);
if(cents[i]<10)
printf("0%ld", cents[i]);
else
printf("%ld", cents[i]);
}
return 0;
}
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.