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 --------------------------------------------");...

  • How do I make this program run the same using a do while loop instead of...

    How do I make this program run the same using a do while loop instead of the for loop? I'm supposed to replace the for loop with a do-while. Also, how would i replace it with a while loop? #include void main(void) { int n, sum, product, i; printf("n?\n"); scanf("%i", &n); printf("You entered %i\n", n); sum = 0; product = 1; for (i=1; i<=n;i++) { if((i%3!=0) && (i%5!=0)){ sum = sum + i; product = product * i;}      ...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • 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...

  • Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and...

    Write a MATLAB program that implements the algorithm designed in the Topic 1 "Non-Linear Flowchart" and "Creating a Flowchart" assignments previously implemented in C. Compare and contrast the C and MATLAB versions of your codes. convert this to a matlab program #include <stdio.h> int main() { int a; printf("Enter first number: "); scanf("%d", &a); int b; printf("Enter second number: "); scanf("%d", &b); int limit; printf("Enter limit: "); scanf("%d", &limit); printf("\nFirst number: %d\n", a); printf("Second number: %d\n", b); int c; c...

  • 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...

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