Computer Science, asked by 54saraswatisingh, 2 months ago

Write a Java program to input a no. print all unique possible combinations of continuous nos. whose sum is equal to the given no.

for ex- Number=15
15=1+2+3+4+5
15=4+5+6
15=7+8​

Answers

Answered by kamalrajatjoshi94
0

Answer:

Program:-

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

System.out.print("Enter a number : ");

int n=in.nextInt();

int sum=0,j=0;

System.out.println("The possible combinations:");

for(int i=1;i<n;i++)

{

sum=i;

j=i+1;

while(sum<n)

{

sum=sum+j;

j++;

}

if(sum==n)

{

for(int k=i;k<j;k++)

{

if(k==i)

System.out.print(k);

else

System.out.print("+" + k);

}

System.out.println();

}

}

}

}

Refer to the attachment for outputs:-

Attachments:
Similar questions