112. JAVA Program to Compute the 1+3+5+7+9+ …….+n Series.
How does this program work?
- This program is used to find the Sum of the given series 1+3+5+7+9+ …….+n using java.
- The integer entered by the user is stored in variable n i.e., last term for the given series.
- By using for loop we can find the sum of series for odd numbers upto nth term.
Here is the code
//To Print Series of odd numbers for nth Series 1+3+5+7+9+ …….+n
import java.util.Scanner;public class odd
{
public static void main(String []args)
{
int n=0,i=0;
Scanner obj= new Scanner(System.in);
System.out.print("Enter value n : \n");
n = obj.nextInt();
System.out.println("The odd series is:");
for ( i=1; i< n; i++ )
{
if ( i%2!=0 )
System.out.print(i+" ");
}
System.out.println();
}
}