next


Java Online Compiler

For Loop In java

for:

'for' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
for(initilization;condition;increment/decrement)
 {
 ...
 ...
 }
 
In the above syntax :
initilization is a starting value
condition is a ending value
increment/decrement is incrementation or decrementation
Display 1 to 10 Values Using For Loop
For Loop

class Main
{
 public static void main(String args[])
 {
  int i;
  for(i=0;i<=10;i++)
  {
  System.out.println(i);
  }
 }
}

Display 1 to N Values Using For Loop

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=0;i<=n;i++)
  {
  System.out.println(i);
  }
 }
}

Display A To Z Using For Loop

class Main
{
public static void main(String args[])
{
char ch;
for(ch='A';ch<='Z';ch++)
{
System.out.print(ch);
}
}
}

Factorial Number

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,fact=1;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=1;i<=n;i++)
  {
  fact=fact*i;
  }
  System.out.println("Factorial of "+n+ "is " +fact);
 }
}

Factorial of N Numbers

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,fact=1;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=1;i<=n;i++)
  {
  fact=fact*i;
  System.out.println("Factorial of "+n+ "=" +fact);
  }
 }
}

Sum Of Natural Numbers
                      
import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,sum=0;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=1;i<=n;i++)
  {
  sum=sum+i;
  }
   System.out.println("Sum OF "+n+ " Numbers Is "+sum);
 }
}

Sum Of Even Numbers

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,sum=0;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=1;i<=n;i++)
  {
  if(i%2==0)
  {
  sum=sum+i;
  }
  }
   System.out.println("Sum OF "+n+ " Numbers Is "+sum);
 }
}

Sum of Odd Numbers

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,sum=0;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
  for(i=1;i<=n;i++)
  {
  if(i%2!=0)
  {
  sum=sum+i;
  }
  }
   System.out.println("Sum OF "+n+ " Numbers Is "+sum);
 }
}

Sum Of Even Or Odd Numbers Based On The N value

import java.util.*;
class Main
{
 public static void main(String args[])
 {
  int i,n,sum=0;
  Scanner s=new Scanner(System.in);
   System.out.println("Enter N Value");
   n=s.nextInt();
   if(n%2==0)
   {
  for(i=1;i<=n;i++)
  {
  if(i%2==0)
  {
  sum=sum+i;
  }
  }
   System.out.println("EvenSum OF "+n+ " Numbers Is "+sum);
}
else
{
for(i=1;i<=n;i++)
  {
  if(i%2!=0)
  {
  sum=sum+i;
  }
  }
   System.out.println("OddSum OF "+n+ " Numbers Is "+sum);

}
 }
}

Nested For Loop

import java.util.*;
class Main
{
public static void main(String args[])
{
int  row,col,i;
Scanner s=new Scanner(System.in);
System.out.println("enter row value");
row=s.nextInt();
for(i=1;i<=row;i++)
{
for(col=1;col<=i;col++)
{
System.out.print(col);
}
System.out.println();
}
}
}