Number Series Program in Java


import java.util.Scanner;

public class NumberSeries
{
    public static void main(String[] args)
    {
        int n, sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of terms=");
        n = sc.nextInt();
        for (int i = 1; i <= n; i++)
        {
            sum = sum + i;
            if (n == i)
            {
                System.out.println(i);
            }
            else
            {
                System.out.print(i + "+");
            }
        }
        System.out.println("Sum of series=" + sum);
    }
}


Output:

Enter number of terms=5
1+2+3+4+5
Sum of series=15
b. tech. bca icse java java tutorials learn java mca programs