473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sum of consecutive positive integers

5 New Member
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer"

thanx in advance
Aug 31 '06 #1
6 8933
rgb
37 New Member
one solution might be:

step 1: do a loop of 1+2+3+4+5+... until your SUM is greater than the INPUT and stop (or exit) the loop

step 2: do a loop of SUM-1-2-3-4-... until your VALUE is equal to INPUT (which is the answer) OR less than the INPUT (which is no answer)

note: you need to store 2 values, last added (i will call it y) and last subtracted+1 (i will call it x) for your final string answer doing x+(x+1)+(x+2)+. .. until you reach y.
Aug 31 '06 #2
rgb
37 New Member
correction on step 1: the SUM should be equal or greater than (not equal only) and if it's equal (you got the answer), else (SUM is greater than INPUT) do step 2.
Aug 31 '06 #3
D_C
293 Contributor
Interesting problem, and the solution is less than 30 lines of code. I would like to say that there will always be a trivial solution. N = sum(1,N) - sum(1,N-1). Surprisingly, the trend seems to be that this only happens when N is a power of 2.
Expand|Select|Wrap|Line Numbers
  1.  1 = 1
  2.  2 = 2
  3.  3 = 1+2
  4.  4 = 4
  5.  5 = 2+3
  6.  6 = 1+2+3
  7.  7 = 3+4
  8.  8 = 8
  9.  9 = 2+3+4
  10. 10 = 1+2+3+4
  11. 11 = 5+6
  12. 12 = 3+4+5
  13. 13 = 6+7
  14. 14 = 2+3+4+5
  15. 15 = 1+2+3+4+5
  16. 16 = 16
  17. 17 = 8+9
  18. 18 = 3+4+5+6
  19. 19 = 9+10
  20. 20 = 2+3+4+5+6
  21. 21 = 1+2+3+4+5+6
  22. 22 = 4+5+6+7
  23. 23 = 11+12
  24. 24 = 7+8+9
  25. 25 = 3+4+5+6+7
  26. 26 = 5+6+7+8
  27. 27 = 2+3+4+5+6+7
  28. 28 = 1+2+3+4+5+6+7
  29. 29 = 14+15
  30. 30 = 4+5+6+7+8
  31. 31 = 15+16
  32. 32 = 32
  33. 33 = 3+4+5+6+7+8
  34. 34 = 7+8+9+10
  35. 35 = 2+3+4+5+6+7+8
  36. 36 = 1+2+3+4+5+6+7+8
Aug 31 '06 #4
pixcee2000
7 New Member
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer"

thanx in advance
Nov 23 '06 #5
sumanjha
4 New Member
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer"

thanx in advance


Question is really good ,One Soln is given below . Run and check with diff input.

#include<stdio. h>
int main()
{
int num,sum1=0,inde x=1,sum2=0;
printf("Enter a no(>0) ");
scanf("%d",&num );
if(num <= 0 )
{
printf("Wrong Input\n");
return 0;
}
for(index;;inde x++)
{
sum1=sum1+index ;
if(sum1>=num)
break;
}
if(sum1 == num )
{
if(num == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp=1;temp<=in dex;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0;
}
for(int i=1;i<index;i++ )
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp= i+1;temp<=index ;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0 ;
}
}
printf("NO ANSWER\n");
return 0 ;
}
Nov 23 '06 #6
sumanjha
4 New Member
Question is really good ,One Soln is given below . Run and check with diff input.

#include<stdio. h>
int main()
{
int num,sum1=0,inde x=1,sum2=0;
printf("Enter a no(>0) ");
scanf("%d",&num );
if(num <= 0 )
{
printf("Wrong Input\n");
return 0;
}
for(index;;inde x++)
{
sum1=sum1+index ;
if(sum1>=num)
break;
}
if(sum1 == num )
{
if(num == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp=1;temp<=in dex;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0;
}
for(int i=1;i<index;i++ )
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
printf("NO ANSWER\n");
return 0 ;
}
printf("Num(%d) = ",num );
for(int temp= i+1;temp<=index ;temp++)
printf(" %d +",temp);
printf("\b \n");
return 0 ;
}
}
printf("NO ANSWER\n");
return 0 ;
}

Hi All ,

Above Code will not work for some specific input.

odd number has two solution.
suppose num is 9 then "9=2+3+4" OR "9=4+5"

Run the following code and test it .

#include<stdio. h>
int main()
{
int num,sum1=0,inde x=1,sum2=0,Soln =0;
printf("Enter a no(>0) ");
scanf("%d",&num );
if(num <= 0 )
{
printf("Wrong Input\n");
return 0;
}
for(index;;inde x++)
{
sum1=sum1+index ;
if(sum1>=num)
break;
}
if(sum1 == num )
{
if(num == index)
{
printf("NO ANSWER\n");
return 0;
}
Soln++;
printf("Solutio n-%d : %d = ",Soln,num );
for(int temp=1;temp<=in dex;temp++)
printf(" %d +",temp);
printf("\b \n");
}

for(int i=1;i<index;i++ )
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
// printf("NO ANSWER\n");
break;
}
Soln++;
printf("Solutio n-%d : %d = ",Soln,num );
for(int temp= i+1;temp<=index ;temp++)
printf(" %d +",temp);
printf("\b \n");
break;
}
}

//if( Soln == 0 && num >= 9 && num/2 == (num-1)/2 ) //--> if U don't want more than one ,soln use this

if( num >= 9 && num/2 == (num-1)/2 ) //--> if U want more than one soln, use this
{
Soln++;
printf("Solutio n-%d : %d = %d + %d\n",Soln,num ,num/2,num/2+1);
return 0;
}
if(Soln==0)
printf("NO SOLUTION\n");
return 0 ;
}
Nov 23 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5550
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
19
11521
by: Johs | last post by:
I need to make some special action if 'a' and 'b' are both positive or both negative. Is there some inbuilt function to check this? Johs
2
1956
by: Michael21622 | last post by:
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5....
11
12390
by: xctide | last post by:
This project will give you more experience on nested for loops and user-defined functions. The original project came from an IT company’s programming test where they were asking for the most efficient code to solve one problem. For this assignment we only focus on the correctness of our code and will not consider code efficiency. ...
1
3767
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return statement).If eithere of the value is negative, the program prints "negative input" and terminates.Otherwise it prints the value the user entered....
6
2916
by: cesco | last post by:
Hi, say I have a vector like the following: vec = and I'd like to know via a function (for example, ConsecutiveIntegers(vec, N)) whether there are N consecutive integers. So, for example, if N=3 then ConsecutiveIntegers(vec, N) should return true because 1, 2, 3 and 5, 6, 7 are consecutive. If N=4, then
7
2856
by: kepano | last post by:
pls help me to write a program (in c) such that we input an integer x,where x>0. For x, the program has to convert it into the sum of positive integers (all posible!). for e.g. x =8 1. 8 2. 7 + 1 3. 6 + 2 4. 6 + 1 + 1 5. 5 + 3 6. 5 + 2 + 1 7. 5 + 1 + 1 + 1 8. 4 + 4 9. 4 + 3 + 1
63
5604
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
8
12921
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column where the values are integers). I can do this programmatically (using code and stored procedures), but it's not a neat solution and there are also...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.