×
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
1803    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.
Aristotle
Educating the mind without educating the heart is no education at all.
Aristotle
366
23.83
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     30778
28/06/2017     25550
02/08/2017     22003
26/05/2018     19717
15/05/2017     19354
06/07/2017     17901
21/04/2018     16863
01/08/2017     16539
01/07/2017     14551
25/04/2018     13616
softetechnologies