Wednesday 13 April 2016

Solution of URI 1159 :: Sum of Consecutive Even Numbers


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

2 comments:

  1. #include
    int main()
    {
    int i,n,j,sum=0;
    scanf("%d",&n);
    while(n!=0)
    {
    if(n%2==0)
    n=n;
    else
    n=n+1;
    for(j=1;j<=5;j++)
    {
    sum+=n;
    n=n+2;
    }
    printf("%d\n",sum);
    n=0;
    sum=0;
    scanf("%d",&n);
    }
    return 0;
    }



    reply.....

    ReplyDelete
  2. #include
    using namespace std;

    int main(){
    int a,sum=0,size = 1;
    for(int i;~i;++i){
    cin>>a;
    if(a!=0){
    if(a%2==0){
    while(size <= 5){
    if(a%2==0){
    sum+=a;
    size++;
    }
    a++;
    }
    }
    else{
    while(size <= 5){
    a++;
    if(a%2==0){
    sum+=a;
    size++;
    }
    }
    }
    cout<<sum<<'\n';
    size = 1;
    sum = 0;
    }
    else if(a==0)
    break;
    }
    }

    ReplyDelete

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