program..how?

How do i make a program to find out the average of 'n' numbers?

Comments

  • #include
    #include
    void main()
    {
    float i,n,m,sum,avg;
    clrscr();
    printf("How many numbers are there?
    ");
    scanf("%f",&n);
    for(i=1;i<=a;i++)
    {
    printf("enter the number
    ");
    scanf("%f",&m);
    sum=0;
    sum=sum+m;
    }
    avg=sum/n;
    printf("The average is:
    %f",avg);
    getch();
    }

    BUT I DONT QUITE GET THE REQUIRED OUTPUT.

    PLEASE HELP ME OUT IN THIS
  • [code]
    : #include
    : #include

    : void main()
    : {
    [red]int i,n; // loop counter and count should be integer[/red]
    : float m,sum,avg;
    : clrscr();
    : printf("How many numbers are there?
    ");
    : scanf("%[red]d[/red]",&n);

    : sum=0[red].0[/red]; [red]// Moved this out (main problem)
    // because you erase the sum on every loop iteration[/red]

    : for(i=1;i<=[red]n[/red];i++)
    : {
    : printf("enter the number
    ");
    : scanf("%f",&m);
    : sum=sum+m;
    : }

    : avg=sum/[red](float)[/red]n; [red]// It is better to cast an integer
    // when using it in float point calculations[/red]
    : printf("The average is:
    %f",avg);
    : getch();
    : }
    : [/code]
    : BUT I DONT QUITE GET THE REQUIRED OUTPUT.
    :
    : PLEASE HELP ME OUT IN THIS
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories