Science, asked by shubham9258, 8 months ago

Write a for loop to increment index variable by 2 in each iteration in java​

Answers

Answered by Nidhi2503
12

for( i=0; i≤n ; i= i+2)

hope so it may help you

please mark me as the Brainliest answer

Answered by codiepienagoya
7

Loop to increment index variable by 2

Output:

12

Incrementing values by 2:  

0

2

4

6

8

10

 Explanation:

The program to this question can be given as  follows:

Program:

//util package

import java.util.*; //import package to take user input.

public class index //defining class index

{

//main

public static void main(String[] x) //defining method

{

Scanner odb= new Scanner(System.in); //creating scanner call object.

int k,n;//defining integer variable

n=odb.nextInt(); //input by user

System.out.println("Incrementing values by 2: "); //print value

for(k=0;k<n;k=k+2) //loop to use increment value by 2

{

System.out.println(k); //print value

}

}

}

Description of program:

  • In the above java program code, a class index is defined, inside this class the main method is defined, in this method first scanner class object is created then integer variable "k and n" is declared, in which variable n is used to take input from user and variable k use to print incremented values.
  • In the next line, for loop is declared that starts from 1 and ends when the value of i is less than n and inside this loop the index value of variable i is increment by 2.
  • Inside the loop, the print function is used, which uses variable i to prints its increment value.

Learn more:

  • Index value increment program:  https://brainly.com/question/14409826
  • What is index value: https://brainly.in/question/3293275  
Similar questions