Computer Applications

The sum of first n odd natural numbers can be calculated as n2, where n is the number of odd natural.
For example,
Sum = 1 + 3 + 5 + 7; number of odd natural = 4
Therefore, Sum = 42 = 16
Write a program to input number of odd natural terms and display sum of the given series:
(a) 1 + 3 + 5 + ………………. + 29 + 31
(b) 1 + 3 + 5 + 7 + ………………. + 47 + 49

Java

Java Math Lib Methods

11 Likes

Answer

import java.util.*;

public class KboatCalculateSum
{
    
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of odd natural terms: ");
        int num = in.nextInt();
        
        System.out.println("Number of odd natural terms = " + num);
        
        double sum = Math.pow(num,2); 
        System.out.println("Sum = " + sum);
        
    }
}

Variable Description Table

Program Explanation

Output

Answered By

6 Likes


Related Questions