JAVA Programming for School
Our dedicated followers,
The admin has noticed misuse of the contents of the page. Somebody is using it directly in their projects and hence we have decided to cease posting any new programs. The page was meant to help you UNDERSTAND the subject better not SHORTEN your homework. We are there to clear your doubts and provide you the algorithm of your program (and in special cases even the program) but only via personal messages.
Yours faithfully,
Admin
Sorry for this delay.
Write a program in Java to accept a number n (between 1 and 9) and print all possible combinations of '+' and '-' of all natural numbers till n such that their sum is 0.
Eg:
Input: n=7
Output:
-1-2+3-4+5+6-7=0
-1-2+3+4-5-6+7=0
-1+2-3-4+5-6+7=0
-1+2+3+4+5-6-7=0
+1-2-3-4-5+6+7=0
+1-2+3+4-5+6-7=0
+1+2-3-4+5+6-7=0
+1+2-3+4-5-6+7=0
Hint: Logic is to generate a table of (n * 2^n) and fill each row with its binary equivalent of its index. That gives you all the possible combinations of pluses and minuses.
====================================================
Source Code:
class Sequence
{
boolean arr[][];int n;
void bin(int a)
{
n=a;
int b= (int)Math.pow(2,n);
arr= new boolean[b][n];
int cnt=0;
for (int i=0;i=0;j--)
{
if (r%2==1)
arr[i][j]=true;
else
arr[i][j]=false;
r/=2;
}
}
}
void printSum(int a)
{
this.bin(a);
int s=0;int x=1;
int b= (int) Math.pow(2,a);
String str="";
for (int i=0;i
Tomorrow is the day..........Indian Council of Secondary Education (I.C.S.E.) Exam fro Computer Applications. Best of Luck to all our friends. We hope we could contribute something to your success..........
Click here to claim your Sponsored Listing.
Category
Website
Address
Kolkata