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);

}

}