Question

C program. Using do-while loop for this question. Question: write a program to calculate the average...

C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers.

output:

Enter the value of n : 18

The sum of first 18 numbers =171

The average of first 18 numbers = 9.00

my code couldn't give me the right avg and sum. Please help.

#include<stdio.h>

int main()
{
    int num;
    int count =0;
    int sum=0;
    float avg;

     printf("Enter the value of n: ");
    scanf("%d", &num);

    do
    {
        sum+= num;
        count++;


    } while(count<num);

    avg=sum/(float)num;

    printf("\nThe sum = %d",sum);

    printf("\nThe average= %f",avg);

    return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>

int main() {
    int num;
    int count = 0;
    int sum = 0;
    float avg;

    printf("Enter the value of n: ");
    scanf("%d", &num);

    do {
        sum += count;
        count++;
    } while (count <= num);
    avg = sum / (float) num;

    printf("\nThe sum of first %d numbers = %d", num, sum);
    printf("\nThe average of first %d numbers = %f", num, avg);

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C program. Using do-while loop for this question. Question: write a program to calculate the average...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float aver...

    Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that...

    need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that first prompts the user for an upper limit: Enter the number of entries: 5 € (user enters 5) After the user enters a number, the program should prompt the user to enter that many numbers. For example, if the user enters 5, then the program will ask the user to enter 5 values. Use a do-while loop to add the numbers. o With each...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers)...

    #include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...

  • I need the programming to be in language C. I am using a program called Zybook...

    I need the programming to be in language C. I am using a program called Zybook Prompt: Write a statement that outputs variable numObjects. End with a newline. Given: #include <stdio.h> int main(void) { int numObjects; scanf("%d", &numObjects); return 0; } What I did so far. I am not sure if its right tho? #include <stdio.h> int main(void) { int numObjects; scanf("%d", &numObjects); printf(" num Objects is "); printf("%d\n", userAge); return 0; }

  • Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...

    Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include <stdio.h> #define PI 3.14 typedef struct cylinder{    float r,h;    float areacircle=2*PI*r;    float areacylinder;    float volumecylinder;    areacylinder=2*PI*r*h+2*PI*r*r;    volumecylinder=PI*r*r*h; }; int main() {    float start,numcyls;    printf("How many cylinders do you want? Enter a positive integer.\n");    scanf("%f",&numcyls);    if(numcyls<1||ceilf(numcyls)!=numcyls)    while()    {printf("The number you have entered is not a positive integer. Please try again.\n");        printf("Remember,...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT