Computer Science, asked by sam00000027, 1 year ago

write this program in java

Attachments:

ramarekha2005: Which class are you in?
sam00000027: class x
ramarekha2005: OK sorry I don't know.

Answers

Answered by QGP
7
Hey There!!


Here's your code:

import java.util.Scanner;
public class Sum_Series
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();
       
        System.out.println();
       
        int k=1;
        int sum=0;

        for(int i=1;i<n;i++)
        {
            k=i;
            sum=0;

            for(int j=i;j<n;j++)
            {
                sum += j;
                if(sum==n)
                {
                    for(int z=i;z<j;z++)
                    {
                        System.out.print(z+"+");
                    }
                    System.out.print(j);
                    System.out.println();
                    break;
                }
            }

        }

    }
}


Hope it helps
Purva
Brainly Community

Answered by anindyaadhikari13
3

Question:-

A positive natural number, (for example, say 27) can be represented as follows,

2+3+4+5+6+7=27

8+9+10=27

13+14=27

where every row and column represents a combination of consecutive natural numbers which add up to 27.

Write a program to input a positive natural number and print the possible consecutive number combination which add up to given number.

Code:-

import java.util.*;

class Java

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int s;

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

{

s=0;

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

{

s+=j;

if(s==n)

{

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

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

System.out.print(j+"="+n+"\n\n");

break;

}

}// end of inner loop.

}// end of outer loop

}// end of main()

}//end of class

Similar questions