Java program The sum of squares of the first n odd natural n
Java program. The sum of squares of the first n odd natural numbers is the following computation: 1^2 + 3^2+ 5^2+ ……………..+ (2n-1)^2 =(n(2n-1)(2n+1))/3 1. Create a package name home.work1 2. Create a class named OddNumbers under the above package with the mail method 3. Write the static method sumWithLoop, that takes the number n as its argument. The code for this method should use the formula shown on the right hand side of the above equation. The method returns the computed sum. 4. Write the static method sumWithoutLoop, that takes the number n as its argument. The code for this method should use the formula shown on the right hand side of the above equation. The method returns the computed sum. 5. The code in the main method should do the following: a. Prompt the user for a string input value for the number. b. Convert the string to an integer and store it in the variable named inputNumber. You can assume that the user enters a valid integer number. c. Invoke the method, sumWithLoop, and store the result into the variable, result1. d. Invoke the method, sumWithLoop, and store the result into the variable, result2. e. Print the two results to the console. f. Using an if-else statement, show that the two values are indeed the same. In the else part, show that your code is incorrect. g. Using a message dialog, show the summary of the above values to the user. See the sample input and output below. Please help for this Java program
Solution
package home.work12
class OddNumers{
 public static sumWithLoop(int n)
 { float sum = 0;
 for(i=1;i<=n;i++)
 {
 sum = (float)sum + power(((2*i)-1),2);
 }
return sum;
}
public static sumWithoutLoop(int n)
 {
 return (n*(2*n-1)*(2*n+1))/3;
 }
 public static void main(String args[])
 {
 float result1, result2;
 String str = null;
 System.out.println(\"Enter the string value of number\");
 Scanner s = new Scanner(System.in);
 str = s.nextLine();
 inputNumber = Interger.parseInt(str);
result1 = sumWithLoop(inputNumber);
 result2 = sumWithoutLoop(inputNumber);
System.out.println(\"The sum of number with loop is \" + result1);
System.out.println(\"The sum of number without loop is \" + result2);
if(result1==result2)
 {
 System.out.println(\"Result is same via both methods\");
 }
 else
 {
 System.out.println(\"Your code is incorrect\");
 }
}
 }


