Computer Science, asked by krishnak6095, 3 months ago

Lab 2one
Write a program to find the sum of the 1st 10 odd natural numbers.​

Answers

Answered by atrs7391
0

Answer:

package com.company;

public class Main {

   public static void main(String[] args) {

       int sum = 0;

       for (int j = 0; j < 20; j++) {

           if (j%2==1){

               //System.out.println(j);

               //You can add the above line if you want to display the odd numbers too.

               int sum2 = j;

               sum2 = sum2+j;

               sum = sum2;

           }

       }

       System.out.println("The sum of first 10 odd natural numbers are: "+sum);

   }

}

Similar questions
Science, 1 month ago