In: Computer Science
Java Class
Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n= sc.nextInt();
System.out.print("Enter a positive
number: ");
System.out.print("List of n odd integer: ");
int j=1,i=1,sum=0;
while(i<=n)
{
sum=sum+j;
System.out.print(j+" ");
j=j+2;
i=i+1;
}
System.out.print("\nsum of n odd integer : "+sum);
}
}