Friday 4 March 2016

Solution of URI 1071 :: Sum of Consecutive Odd Numbers I


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,c=0;
    scanf("%d %d", &a, &b);
    if(a==b)
        printf("%d\n",c);
    else if(a<b)
    {
        for(a=a+1;a<b;a++)
        {
            if(a%2==1||a%2==-1)
                c+=a;
        }
        printf("%d\n",c);
    }
    else if(a>b)
    {
        for(b=b+1;b<a;b++)
        {
            if(b%2==1||b%2==-1)
                c+=b;
        }
        printf("%d\n",c);
    }
    return 0;
}

3 comments:

  1. I was tried to create a sorted list with value between from the input given, it's give the same answer. But my code still rated with Wrong Answer.

    ReplyDelete
    Replies
    1. with absolute value off course, to prevent minus value ruin everything.

      Delete
  2. #include

    int main()
    {
    int X, Y, temp, sum = 0;

    scanf("%d %d", &X, &Y);

    if (Y < X){
    temp = X;
    X = Y;
    Y = temp;
    }
    if (X %2 == 0)
    X++;
    else
    X+=2;

    while(X < Y)
    {
    sum = sum + X;
    X+=2;
    }
    printf("%d\n", sum);

    return 0;
    }

    ReplyDelete

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