×
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
3495    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.
Swami Vivekananda
A nation is advanced in proportion to education and intelligence spread among the masses.
Swami Vivekananda
1423
59.47
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     44336
01/01/2018     36726
25/06/2018     36300
28/06/2017     34765
02/08/2017     33320
01/08/2017     27720
06/07/2017     27482
15/05/2017     27080
14/07/2017     22796
11/09/2018     21441
softetechnologies