addemup.c /* addemup.c -- four kinds of statements */ #include <stdio.h> int mai
ID: 3883388 • Letter: A
Question
addemup.c
/* addemup.c -- four kinds of statements */
#include <stdio.h>
int main(void) /* finds sum of first 20 integers */
{
int count, sum; /* declaration statement */
count = 0; /* assignment statement */
sum = 0; /* ditto */
while (count++ < 20) /* while incrementing count… */
{
sum = sum + count; /* statement */
}
printf("sum = %d ", sum);/* function call */
return 0;
}
}Do Ch.5 PE 5 (modified and enhanced over the book)
•Modify the addemup.c program to not use a fixed 20 iterations, but ask the user how many days (iterations) to use, read that user input into a variable, and print out the running total every day up through that number of days.
•Furthermore, keep the running total as an int, but cast it to a double in the printf() statement, and print it as dollars and cents right aligned, 8 spaces wide.
•Example program output:
Enter the number of days to add dollars: 4
At day 1, the sum= $ 1.00
At day 2, the sum= $ 3.00
At day 3, the sum= $ 6.00
At day 4, the sum= $ 10.00
Explanation / Answer
#include <stdio.h>
int main(void)
{
int count, sum; /* declaration statement */
count = 0; /* assignment statement */
printf("enter the number of days to enter dollar:")
scanf("%d ",&count); //user input
sum = 0;
for(int i=1;i<=count;i++) /* while incrementing count… */
{
sum = sum + count;
printf("at day %d,the sum= $%8d ",i,(double)sum); //first we take for days,then we align using 8d to align 8
// digits to right and then we cast int sum to (double) sum
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.