Write a Java program to display the n terms of odd natural number and their sum


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. import java.util.Scanner;
  2. public class JavaExcercise {
  3.  
  4. public static void main(String[] args)
  5.  
  6. {
  7. int i,n,sum=0;
  8.  
  9. System.out.print("Enter number of terms: ");
  10. {
  11. Scanner in = new Scanner(System.in);
  12. n = in.nextInt();
  13. System.out.println ("\nThe odd numbers are :");
  14. for(i=1;i<=n;i++)
  15.  
  16. {
  17. System.out.println (2*i-1);
  18. sum+=2*i-1;
  19. }
  20. System.out.println ("The Sum of odd Natural Number upto " +n+" terms is: " +sum);
  21.  
  22. }
  23. }
  24. }

Result

java techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.