Addition of Consecutive Digits in a given Number Program

Addition of Consecutive Digits in a given Number Program

In this blog we are going to discuss about Addition of Consecutive Digits in a given Number Program . They are illustrated very well below :

______________________________________________________________________________________

Hi,

In this post, we are going to discuss Addition of Consecutive Digits in a given Number program.

Add numbers by below Method

We can take n numbers, For Example

We can take 5, for this, we should take 1-5.  Now, we need to add the consecutive digits in the given number 5.

(1+2)+(2+3)+(3+4)+(4+5)  = 24

Addition of Consecutive Digits in a given Number – Program:

public class Program {

public static void main(String[] args) {

int result=0,i,a=0;

System.out.print(“Enter the number of n value”+” “);

int num;

Scanner scan = new Scanner(System.in);

num = scan.nextInt();

for(i=1;i<num;i++){

int n=i;

a= n+(++n);

result=result+a;

}

System.out.println(“Total of n number is”+” ” +result);

}

}

Also there is a java run program given below : 

// Java program to print a consecutive sequence
// to express N if possible.
class GfG
{
    // Print consecutive numbers from
    // last to first
    static void printConsecutive(int last, int first)
    {
        System.out.print(first++);
        for (int x = first; x<= last; x++)
            System.out.print(" + "+x);
    }  
      
    static void findConsecutive(int N)
    {
        for (int last=1; last<N; last++)
        {
            for (int first=0; first<last; first++)
            {
                if (2*N == (last-first)*(last+first+1))
                {
                    System.out.printf(N + " = ");
                    printConsecutive(last, first+1);
                    return;
                }
            }
        }
        System.out.print("-1");
    }
      
    // main function
    public static void main (String[] args) 
    {
        int n = 12;
        findConsecutive(n);
    }
}