Writ a program that prints a row of integers from 1 to N followed by their cumul
ID: 3620700 • Letter: W
Question
Writ a program that prints a row of integers from 1 to N followed by theircumulative sum and average for all values of N from 1 to 5;
the sum and average must be computed by your program;
each number should be separated by a tab ' ';
print a colon ':' before printing the sum;
use extra tabs ' ' to align the sum and average columns;
each line should end with a semicolon ';'.
Your output should look like this:
.......................................................................
1 : 1 1.000000;
1 2 : 3 1.500000;
1 2 3 : 6 2.000000;
1 2 3 4 : 10 2.500000;
1 2 3 4 5 : 15 3.000000;
Explanation / Answer
please rate - thanks I got the impression the sum and average columns should be lined up with sum & average lined up #include <stdio.h>#include<conio.h>
int main()
{int n,sum,i,j;
double average;
printf("Enter a number: ");
scanf("%d",&n);
printf("Numbers");
for(i=0;i<n;i++)
printf(" ");
printf("sum average ");
for(i=1;i<=n;i++)
{sum=0;
for(j=1;j<=i;j++)
{ printf("%d ",j);
sum+=j;
}
for(j=i;j<n;j++)
printf(" ");
average=sum/(double)i;
printf(":%d %f; ",sum,average);
}
getch();
return 0;
}
------------------------------------- with sum & average not lined up
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.