39.3k views
4 votes
For this task you have the chance to show what you’ve learned about implementing repetition in code. You are required to create a program that uses the while loop structure.

Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.

Make use of the while loop repetition structure to implement the program.

1 Answer

4 votes

Step-by-step explanation:

#include <stdio.h>

main(){

int x,c=0,sum=0;

float a;

printf("enter a number (don't enter -1)\\");

scanf("%d",&x);

while(x!=-1){

c++;

sum+=x;

printf("enter a number(enter -1 to calculate the average)\\");

scanf("%d",&x);

}

a=float(sum)/c;

printf("the average is %.2f ",a);

}

User Default Picture
by
5.6k points