Ticker

6/recent/ticker-posts

Java (For loop)

FOR LOOP IN JAVA

INTRODUCTION
The For loop is a simplest loop among all the loops in Java language because its all the statements (Initialization, Increment/Decrement, Test condition) written only within one statement.



PROGRAM TO PRINT COUNTING 1 - 10
class for_loop
{
public static void main(String args[])
{
int i;
for(i=1; i<=10; i++)
{
System.out.println(i);
}

}
}


- - - PROGRAM OUTPUT - - -


PROGRAM TO PRINT REVERSE COUNTING 10 - 1
class for_loop
{
public static void main(String args[])
{
int i;
for(i=10; i>=1; i--)
{
System.out.println(i);
}

}
}


- - - PROGRAM OUTPUT - - -



PROGRAM TO PRINT MULTIPLICATION TABLE FOR A GIVEN NUMBER
import java.util.Scanner; //Import the Scanner class
class math_table
{
public static void main(String args[])
{
int i,n;
System.out.print("Enter any number: ");

//Method to accept input form the user at run time
Scanner in = new Scanner(System.in);
n=in.nextInt();

// Loop to print multiplication table
for(i=1; i<=10; i++)
{
System.out.println(n + " x " + i +" = " + (n*i));
}

}
}


- - - PROGRAM OUTPUT - - -
















Post a Comment

0 Comments