×
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
2636    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.
Sri Sri Ramakrishna Paramahamsa
It is easy to talk on religion, but difficult to practice it.
Sri Sri Ramakrishna Paramahamsa
3625
37.16
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     34102
28/06/2017     30586
26/05/2018     29417
02/08/2017     28490
25/06/2018     24448
15/05/2017     24273
01/08/2017     22969
06/07/2017     22777
21/04/2018     19043
14/07/2017     17966
softetechnologies