We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

sum combiantion

the following program is to print all possible sum combinations of a number
for example
6=1+5,2+4,3+3,1+1+4,1+2+3
12=3+9,4+8,5+7,.......,1+1+2+3+5
but i m not getting any output...maybe there's a problem with proper ending of loops
pls help

import java.io.*;
public class sumpermutations
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int input,i,j,d=0,s=0,num=0,d1=0;
System.out.println("ENTER NUMBER");
input=Integer.parseInt(br.readLine());
for(i=1;i<=99999;i++)//for loop till the highest 5 digit number
{
while(i!=0)//to find the sum of digits of each number
{
num=i;
d=i%10;
s+=d;
i/=10;
}
if(s==input)//checks if the sum is equal to the input number
{
if(istrue(num))//function to check if the digits of num are in ascending order
{
d1=num%10;
System.out.print(d1+"+");//if so print the digits which is the sumcombination of the input number
num/=10;
}
}
}
}

public boolean istrue(int n)
{
int i,j,k=0,di=0,c=0;boolean res=false;int arr[]=new int[6];
while(n!=0)
{
di=n%10;
c++;
arr[k]=di;//store the digits in array
n/=10;
k++;
}
for(i=0;i<arr.length-1;i++)
{
if(arr[i]<arr[i+1])//checks if the array is in ascending order
{
res=true;
return res;//if so then return true
}
else//else false
{
res=false;
return res;
}
}
return res;
}
}
4
Contributors
11
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
12
Views
desert564
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

your main method is wrongly written, in the first place.

public static void main(String[] args)

because I find it hard to read through code that uses no indentation I haven't read it all, so you perhaps might have other problems as well.

bibiki
Posting Whiz
365 posts since Nov 2009
Reputation Points: 42
Solved Threads: 31
Skill Endorsements: 0

i am sorry but i haven't been taught this at school

desert564
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

but i m not getting any output

What shows on the console when you try to execute this program?
There must be an error message.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Ezzaral
null
Moderator
16,109 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 27

there's actually no error message ..the program compiles but then keeps on asking for input values

desert564
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Check your logic to see if where it goes that there is nothing printed when it goes there. If there is a path it can take without anything printing, check to see why.

Add some println statements to show the values of variables as they are changed and to show the execution flow. Then you will get something printed out.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Norm,
what he has is a diverging code (I hope I said it correctly). he never gets out of the while loop that's why it outputs nothing... it does not ask for input. it just while-loops.

UPDATE: My bad. I mistakenly thought of num = i as i = num, AND derived wrong conclusions.

bibiki
Posting Whiz
365 posts since Nov 2009
Reputation Points: 42
Solved Threads: 31
Skill Endorsements: 0

If he puts some printlns in the infinite loop he will see that.
Then hopefully he'll wonder why the loop is forever and look at the code to see why.
By printing out the variables used in the loop he could get a clue.
Otherwise, we'll have to suggest where and what for him to look at.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

i didnt get the error...but the for loop here is basically printing the sum of digit of all numbers within the loop range..it doesn't include any printing...and i didn't understand what you meant by printing out of loop..

desert564
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

and i didn't understand what you meant by printing out of loop..

it's good to keep track of how your variables change. to do that, you can use System.out.println(variable_to_be_tracked).

you should be able now to decide what variable you want to track and decide where to put your println statements. hope this is of any help.

bibiki
Posting Whiz
365 posts since Nov 2009
Reputation Points: 42
Solved Threads: 31
Skill Endorsements: 0

If you are printing out more than one variable, put an id label on the print out so you know what value it is that was printed:
System.out.println("variable_to_be_tracked=" + variable_to_be_tracked);

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0886 seconds using 2.73MB