Thread: Sum of consecutive numers

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    22

    Sum of consecutive numers

    Sum of consecutive numers-1-png

    Here is what i am attempting to do:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int main()
    {
        int t,i,j,k;
        scanf("%d", &t); // so test
    
    
        long int n[t+1], s=0;
        for(i=1; i<=t; i++)
            scanf("%li", &n[i]);
        for(i=1; i<=t; i++)
        {
            for(j=1; j<n[i]/2+1; j++)
            {
                for(k=j; k<=n[i]/2+1; k++)
                {
                    long int a[n[i]/2+1];
                    s = s+k;
                    a[k] = k;
                    if(s == n[i])
                    {
                        printf("%li = ", n[i]);
                        for(k=j; k<=n[i]/2; k++)
                            printf("%li + ", a[k]);
                        break;
                    }
                    if(s > n[i])
                        break;
    
    
    
    
                }
    
    
            }
            if(s> n[i])
                printf("IMPOSSIBLE");
            printf("\n");
    
    
        }
    
    
        return 0;
    }
    Can u show me where i was wrong or give me some suggestions?
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the algorithm that you have in mind?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. consecutive numbers
    By SDH in forum C Programming
    Replies: 27
    Last Post: 01-22-2013, 05:24 AM
  2. Consecutive one's problem
    By Samyx in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2009, 02:45 PM
  3. consecutive number
    By stewie1986 in forum C Programming
    Replies: 3
    Last Post: 12-03-2007, 03:53 PM
  4. consecutive characters
    By stewie1986 in forum C Programming
    Replies: 1
    Last Post: 12-01-2007, 08:02 AM
  5. First Consecutive composites Help
    By ch4 in forum C Programming
    Replies: 10
    Last Post: 11-23-2007, 04:55 PM