Consecutive Sum

// Consecutive sum of natural numbers

class Consq_Sum
{
public static void main(int n)
{
int i;
for(i=1;i<n;i++)
{
check(i,n);
System.out.println();
}
}

public static void check(int i, int n)
{
int copy,k,sum=0,flag=0;
copy=i;
while(sum<n)
{
sum=sum+i;
if(sum==n)
{flag=i;
break;
}
i++;
}
if(flag!=0)
{
for(k=copy;k<=flag;k++)
System.out.print(k+" , ");

}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s