Sunday, 27 March 2016

Solution of URI 1154 :: Ages


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int a,b=0;
    double c,d=0;
    while(1)
    {
        scanf("%d", &a);
        if(a<0)
            break;
        else
        {
            b+=a;
            d++;
        }
    }
    c=b/d;
    printf("%.2lf\n",c);
    return 0;
}

4 comments:

  1. here, what's the use of 'b'?

    ReplyDelete
  2. b will calculate the total sum.

    ReplyDelete
  3. What if d was an int instead of being double?

    ReplyDelete
  4. #include
    int main()
    {
    float n,a,sum=0;
    int count=0;
    scanf("%f",&n);
    while(n>=0)
    {

    count++;
    sum+=n;
    scanf("%f",&n);
    }
    a=sum/(float)count;
    printf("%.2f\n",a);
    return 0;
    }
    reply....

    ReplyDelete

Note: only a member of this blog may post a comment.