VISIT GITHUB TO SEE MY PROJECTS GO

POSITIVE NUMBER | ISC COMPUTER 2024 | survnor.blogspot.com

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

 

A positive natural number,(for e.g. 27 can represent as follows:- 2+3+4+5+6+7=27 or 8+9+10=27 or 13+14=27). Where every row represents a combination of consecutive natural number which add up to 27.

Write a program which inputs a positive natural number N and print the possible consecutive number combination which when added give N.

 

ALGORITHM

 

STEP 1:- Start.

STEP 2:- Declare consecutive as a class. Now declare N, sum, k, i, j as variables of type int. Initialize sumß0.

STEP 3:- Declare consecu() as a method with return type void.

STEP 4:- Now input a positive natural number from user and store it into N.

STEP 5:- The outer loop ‘i’ store from 1 this loop is generates the starting number of consecutive number combination series up to N/2+1 up to N/2+1 and incremented by 1 in each iteration

STEP 6:- Initialize sumß0. First inner loop checks if the number combination is possible with that starting number. If a series is found, which is checked just outside the first loop. First inner loop, initialized by ‘i’ and iterate up to N/2+1 and incremented by 1 in each step.

STEP 7:- Now sum of the series consecutively, sumßsum+j, also checked the sum value is equal with original number or not, if yes then break.

STEP 8:- Now check j<=N/2+1 or not. If yes the second inner loop will perform. Second inner loop ‘k’ assigned by ‘i’ and iterate up to ‘j’ and incremented 1 in each step.

STEP 9:- Second inner loop prints the consecutive number combination by using ‘k’.

STEP 10:- Now close outer loop method consecu. Now main() and create object ob of class consecutive. Now call the method consecu through object ob within main().

STEP 11:- Now close the main() and class consecutive.

STEP 12:- Stop.

 

 

 

 

 

 

 

 

 

 

SOURCE CODE

 

import java.util.*;

class consecutive{

    int N,i,j,k,sum=0,c=0;

    Scanner sc=new Scanner(System.in);

    void consecu(){

        System.out.println("ENTER THE NUMBER:");

        N=sc.nextInt();

        for(i=1;i<(N/2)+1;i++){

            sum=0;

            for(j=i;j<=(N/2)+1;j++){

                sum=sum+j;

                if(sum==N)

                break;}

            if(j<=N/2+1){

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

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

                System.out.println();

                c++;}}

        System.out.println("FREQUENCY:"+c);}

    public static void main(){

        consecutive ob= new consecutive();

        ob.consecu();}}

 

 

 

 

 

 

 

 

 

 

 

VARIABLE DESCRIPTION

 

S.L.No.

Name

Data Type

Description

1

N

Int

Use to accept positive number from user.

2

i,j,k

int

Used as a loop variable.

3

sum

int

Used to store the sum of the consecutive positive number.

 

INPUT AND OUTPUT


Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.