For Loop Program

Hello Java Beginner,

Now time is coming for good coding in for loop program.

In this blog, I will give you two programs on for loop with output. Then after I give you tutorial on same topic, so you can easily understand the for loop with practically.

First Program is on multiplication table using “for loop and command line argument”.

For Loop Program: Multiplication table using

class loop2

{

          public static void main(String args[])

          {

                   int i;

                   int n = Integer.parseInt(args[0]);

                   for(i=1;i<=10;i++)

                   {

                             System.out.println(n+”*”+i+”=”+(n*i));

                   }

}

}

Output:

Command line Value is : 5

5*1 = 5

5*2 = 10

5*3 = 15

5*4 = 20

5*5 = 25

……….

……….

5*10 = 50

 

Second Program is on “Find even number from 1 to N”.

Program on For Loop.

class loop3

{

          public static void main(String args[])

          {

                   int i;

                   int n = Integer.parseInt(args[0]);

                   for(i=1;i<=n;i++)

                   {

                             if(i%2==0)

                             {

                                       System.out.println(i);

                             }

                   }

}

}

Output:

Command line Value is: 10

2

4

6

8

10

 

This program will help you to understand basic structure of for loop. You can also try this program in “while and do..while loop”.

List Of Tutorial on For, While and Do..while Loop.

NoTitle
1Find sum of 1 to N numbers using for  loop
2Find sum and average of N to 1 Number using while loop
3Find multiplication table with odd number using do..while loop

Example

6*1 = 6

6*3 = 18

6*5 = 30

….

6*9 = 54

 

If you have problem on this blog, you can directly comment on it.

788total visits,5visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image