473,548 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sum of positive integers

4 New Member
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
10. 4 + 2 + 2
11. 4 + 2 + 1 + 1
12. 4 + 1 + 1 + 1 + 1
13. 3 + 3 + 2
14. 3 + 3 + 1 + 1
15. 3 + 2 + 2 + 1
16. 3 + 2 + 1 + 1 + 1
17. 3 + 1 + 1 + 1 + 1 + 1
18. 2 + 2 + 2 + 2
19. 2 + 2 + 2 + 1 + 1
20. 2 + 2 + 1 + 1 + 1 + 1
21. 2 + 1 + 1 + 1 + 1 + 1 + 1
22. 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
Result should be displayed.

thanx in advance
Nov 27 '07 #1
7 2855
Meetee
931 Recognized Expert Moderator Contributor
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
10. 4 + 2 + 2
11. 4 + 2 + 1 + 1
12. 4 + 1 + 1 + 1 + 1
13. 3 + 3 + 2
14. 3 + 3 + 1 + 1
15. 3 + 2 + 2 + 1
16. 3 + 2 + 1 + 1 + 1
17. 3 + 1 + 1 + 1 + 1 + 1
18. 2 + 2 + 2 + 2
19. 2 + 2 + 2 + 1 + 1
20. 2 + 2 + 1 + 1 + 1 + 1
21. 2 + 1 + 1 + 1 + 1 + 1 + 1
22. 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
Result should be displayed.

thanx in advance
Help in what sense? What have you tried so far? Please post your efforts here so we can help you. Just apply simple mathematical logic and try to convert it into C conventions.

Please read posting guidelines.

Regards
Nov 27 '07 #2
kepano
4 New Member
I have problem with converting my idea into C/c++ conventions, because now I begin my adventure with this language.
I know that to split integer into two parts you have to use something like:

b=1;
while(a!=0, a>=b)
{
a--;
printf("%d + %d\n", a, b);
b++;
}

so if we could split number a, and b again it should work, but I think it won't work because it will be problem with order of numbers, and many more.

The second idea I have is to initiate chart with growing size, in with we first put our number( size=1) then size=2 and split our X, then size 3 in which we firstly split number to two first "columns" and in 3th place would be "1". When it's done we add 1 to the 2nd number (-1 in 1st), and than program should try add to the 3th as lond as 3th=<2nd, than we again add 1 to 2nd, and the same with 3th, the same should work with as long as we obtain "1" in everywhere.
I think the second solution should work, but it comlicated and I have problem with how to write it. I thinking about it from a few days, and I have no idea how to do it.
You could help me even by sugestin me other way of solwing this problem. And sorry for my english!
Nov 27 '07 #3
Ganon11
3,652 Recognized Expert Specialist
I would try using a recursive function to find all the integers adding up to the int value passed. You would call it with 8, and it would first print 8, then 7 and whatever integers added up to 1 (i.e. 1), then 6 and whatever integers added up to 2 (i.e. 2 and 1 + 1), etc etc. Your base condition would be when the number is 1, the sum of integers resulting in it is simply 1.

After this, all that remains is fancy printing.
Nov 27 '07 #4
kepano
4 New Member
But what with that exemple:
18. 2 + 2 + 2 + 2

in this case we don't have 1.
Nov 28 '07 #5
oler1s
671 Recognized Expert Contributor
Hmm, yes, so your algorithm clearly can't rely on there being a 1 at the end, right?

Look, you've just dumped your question here, and walked away. Finding out the algorithm is part of being a programmer and doing the homework assignment. We aren't going to to tell you what the algorithm is. So get to work. Sit down with pencil and paper and come up with ideas.
Nov 28 '07 #6
Ganon11
3,652 Recognized Expert Specialist
But what with that exemple:
18. 2 + 2 + 2 + 2

in this case we don't have 1.
This is actually included in the recursive solution - I leave it up to you to show that it is. Think of the similarity with 19. 2 + 2 + 2 + 1 + 1 (i.e. (2 + 2 + 2) + 2 and (2 + 2 + 2) + 1 + 1)
Nov 28 '07 #7
kepano
4 New Member
Ok Ganon11, you are right.
I found this:
http://www.site.uottawa.ca/~ivan/F49-int-part.pdf
and I will try to use it.
Nov 28 '07 #8

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

Similar topics

19
2407
by: Harshan | last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why the one less at positive Range ? (compared to the negative side..!) I came to know that it uses 2's compliment internally for storing the negative numbers . why it uses 2's compliment but not the ones compliment for storing the negative numbers ?
11
13102
by: gouqizi.lvcha | last post by:
Hi, All: I wonder what is the smallest positive double numbers in C in 32 bit CPU? Rick
6
8933
by: shaveta | 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....
7
5549
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
3
3643
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return statement).If either of the values is negative,the progra prints"Negative input" and terminates.Otherwie, it prints the values that the user enters. After...
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....
63
5603
by: deepak | last post by:
Hi, Can someone give the standard function which can create positive integer value in C? Thanks, Deepak
1
3027
by: Cyprus106 | last post by:
I'm trying to show some data, and for a complicated reason, all of the integers in a particular field are negative. They're converted to positive in the program that uses them, but I'm using a SELECT statement to show the numbers, and I need them to be shown as positives (and realistically I'd love to show a "$" in front of them, but one battle at...
0
7444
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
7954
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...
0
7805
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5085
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
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.