×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Kaprekar number
Write a program to display the possible sum Consecutive numbers to developed a given number. - Java
2359    Arnab De    19/06/2018

Write a program to display the possible sum Consecutive numbers to developed a given number.

Accept a number from the user and put into a loop to find the Consecutive Numbers and store the numbers in a string. Now print the string a result.

import java.util.*;
class Consecutive
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number:");
        int num = sc.nextInt();
        int sum;
        String str;
        for(int i=1;i<num;i++)
        {
            sum=0;
            str="";
            for(int k=i;k<num;k++)
            {
                sum += k;
                if(str.equals(""))
                {
                    str += k;
                }
                else
                {
                    str += "+" + k;
                }
                if (sum==num)
                {
                    System.out.println(str);
                }
                else if(sum>num)
                {
                    break;
                }
            }
        }
    }
}
softetechnologies

Enter the number:
15
1+2+3+4+5
4+5+6
7+8

Kaprekar number
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
Unthinking respect for authority is the greatest enemy of truth.
Albert Einstein
2369
32.45
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     33208
28/06/2017     29018
02/08/2017     26444
26/05/2018     25946
15/05/2017     22743
06/07/2017     21166
01/08/2017     20872
21/04/2018     18372
25/06/2018     17110
14/07/2017     15996
softetechnologies