input n and print all combinations from 1 to n whose sum is 0.

Write a java program to input n and print all the combinatios from 1 to n whose sum is 0.



Examples :

Input   N: 3

 Output.  :
-1      -2       3
 1       2      -3
Number if combinations =  2


Input   N: 4

 Output.  :
-1       2       3      -4
 1      -2      -3       4
Number if combinations =  2


Input   N: 5

 Output.  :

NO combinations possible Sorry

Computer generated outputs


coding:-


import java.util.*;
class truthtable
{
  public static void main(String args[])
  {    Scanner sc=new Scanner(System.in);
       System.out.print("Input\tN: ");
       int n=sc.nextInt();
       System.out.println();
       System.out.println(" Output.  :");

      int  l=(int)Math.pow(2,n);
      int a[][]=new int[l][n];
      int p=n-1,d=0;
        for(int i=0;i<n;i++)
        {
             d=(int)Math.pow(2,p);int c=1;
              for(int j=0;j<l;j++)
              {
                  if(c>(2*d))
                  c=1;
                  if   (c<=d)
                  a[j][i]=-1*(i+1);
                  else
                 a[j][i]=i+1;
                 c++;
              }
            p--;
        }
       
      int x=0;
        for(int i=0;i<l;i++)
        {   int s1=0;
             for(int j=0;j<n;j++)
           s1=s1+a[i][j];
if(s1==0)
{x++;for(int j=0;j<n;j++)
              {if(a[i][j]<0)
System.out.print(a[i][j]+"\t");
else
System.out.print(" "+a[i][j]+"\t");
}
           System.out.println();
}
         }

if(x>0)
{System.out.println("Number if combinations =  "+x);
}
else
System.out.println("NO combinations possible Sorry");
    }
}



/*

Outputs are :

Input   N: 3

 Output.  :
-1      -2       3
 1       2      -3
Number if combinations =  2


Input   N: 4

 Output.  :
-1       2       3      -4
 1      -2      -3       4
Number if combinations =  2


Input   N: 5

 Output.  :
NO combinations possible Sorry


Input   N: 6

 Output.  :
NO combinations possible Sorry

Input   N: 7

 Output.  :
-1      -2       3      -4       5       6      -7
-1      -2       3       4      -5      -6       7
-1       2      -3      -4       5      -6       7
-1       2       3       4       5      -6      -7
 1      -2      -3      -4      -5       6       7
 1      -2       3       4      -5       6      -7
 1       2      -3      -4       5       6      -7
 1       2      -3       4      -5      -6       7
Number if combinations =  8

Input   N: 8

 Output.  :
-1      -2      -3      -4       5       6       7      -8
-1      -2      -3       4      -5       6      -7       8
-1      -2       3      -4      -5      -6       7       8
-1      -2       3       4       5       6      -7      -8
-1       2      -3       4       5      -6       7      -8
-1       2       3      -4      -5       6       7      -8
-1       2       3      -4       5      -6      -7       8
 1      -2      -3       4      -5       6       7      -8
 1      -2      -3       4       5      -6      -7       8
 1      -2       3      -4      -5       6      -7       8
 1       2      -3      -4      -5      -6       7       8
 1       2      -3       4       5       6      -7      -8
 1       2       3      -4       5      -6       7      -8
 1       2       3       4      -5      -6      -7       8
Number if combinations =  14

*/

Comments

Popular Posts