×
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
3356    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.
Nelson Mandela
Education is the most powerful weapon which you can use to change the world.
Nelson Mandela
304
55.7
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43021
01/01/2018     36304
25/06/2018     34795
28/06/2017     34409
02/08/2017     32758
01/08/2017     27280
06/07/2017     27080
15/05/2017     26678
14/07/2017     22286
21/04/2018     20937
softetechnologies