Friday 18 March 2016

Solution of URI 1101 :: Sequence of Numbers and Sum


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

14 comments:

  1. Replies
    1. 1 means true.
      For that, this while loop will always continue if I don't break it.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. //I THINK MY CODE IS LITTLE BIT MAKES SENSE
      #include

      using namespace std;

      int main()
      {
      int n , m , sum=0;
      while (cin >> m >> n)
      {
      sum=0;
      if (m<=0||n<=0){return 0;}
      else
      {
      if (m > n)
      {
      for (int i = n ; i <= m ; ++i)
      {
      cout << i << " ";
      sum+=i;
      }

      }

      else if(n>m)
      {
      for (int i = m ; i <= n ; ++i)
      {
      cout << i << " ";
      sum+=i;
      }

      }
      }

      cout << "Sum=" << sum <<endl;
      }

      }

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Why this code shows presentation error

    #include
    int main()
    {
    int M,N,i,a,sum;
    while(1)
    {
    scanf("%d%d",&M,&N);
    if(M<=0||N<=0)
    break;
    if(N>M)
    {
    a=M;
    M=N;
    N=a;
    }
    sum=0;
    for(i=N;i<=M;i++)
    {
    sum+=i;
    printf("%d ",i);
    }
    printf("sum=%d\n",sum);
    }
    return 0;
    }

    ReplyDelete
    Replies
    1. your print "sum" has a tiny "s", try to replace it to a big "s". like> Sum.

      Delete

  4. #include
    using namespace std;
    int main()
    {
    int a,b,sum=0;
    cin>>a>>b;
    if (a<=0 ||b<=0)
    {
    break ;
    }


    else if (a>b)
    {
    swap(a,b);
    }
    for (int i=a;i<=b;i++)
    {
    printf("%d ",i);

    sum=sum+i;
    }
    cout<<endl;
    printf("sum=%d\n",sum);




    }

    ReplyDelete
  5. #include

    int main()
    {
    int x,y,i,count =0;
    while(1)
    {
    scanf("%d%d",&x,&y);
    if(x<=0 || y<=0)
    {
    break;
    }
    else
    {
    if(x>y)
    {
    for(i=y;i<=x;i++)
    {
    printf("%d ",i);
    count=count+i;
    }
    printf("Sum=%d\n",count);
    }
    else if(x<y)
    {
    for(i=x;i<=y;i++)
    {
    printf("%d ",i);
    count=count+i;
    }
    printf("Sum=%d\n",count);
    }
    }
    }
    return 0;

    }




    why does it shows 70% wrong?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. #include
    #include

    int main(){
    int M, N,cont,
    soma = 0;

    while (1){
    scanf("%d %d", &M, &N);
    if (M==0 || N==0)
    break;

    else if(M>N){
    for(cont=N; cont<=M; cont++){
    soma=soma+cont;
    printf("%d ", cont);
    }
    printf("Sum=%d\n" ,soma);

    }else if (M<N){
    for(cont=M; cont<=N; cont++){
    soma=soma+cont;
    printf("%d " ,cont);
    }
    printf("Sum=%d\n" ,soma);
    }
    soma=0;

    }
    return 0;
    }


    This shows 10% WRONG, WHY ??????

    ReplyDelete
    Replies
    1. The first if is missing M<=0 AND N<=0

      Delete
  8. Why this is wrong (55% error)

    #include
    #include

    int main () {

    int x,y,a,b, soma=0, fim;

    do{

    scanf("%d%d", &x, &y);

    a=x;
    b=y;

    if(x>y){
    a=y;
    b=x;
    }

    if(a!=0 && b!=0 && a>0 && b>0){

    do{

    printf("%d ", a);
    soma+=a;
    a++;


    }while(a<b+1);

    printf("Sum=%d\n", soma);
    soma=0;
    }

    else
    fim=0;

    }while(fim!=0);

    system("pause");
    }

    ReplyDelete

  9. package URI_Problem;

    import java.util.Scanner;

    public class prob_1101 {
    public static void main(String[] args) {
    int x,y, temp, sum=0, i;
    Scanner in = new Scanner(System.in);
    while(true){
    x = in.nextInt();
    y = in.nextInt();
    if(x==0 || y==0){
    break;
    }
    else if(x>y){
    {
    temp = x;
    x = y;
    y = temp;
    for (i = x; i <= y; i++) {
    System.out.print(i+" ");
    sum+=i;
    }
    System.out.println(" Sum="+sum);
    }
    }
    }
    }
    }

    ReplyDelete

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