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;
}
}

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.

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.

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

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.

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.

Edited 5 Years Ago by bibiki: n/a

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.

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..

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.

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);

This article has been dead for over six months. Start a new discussion instead.