Definition
for Loop is a control statement used to repeat a block of code multiple times.
Typically used when number of iterations is known.
syntax:
for (initializer; condition; updater) {
//body
}
Initialization → set starting value
Condition → loop continues while true
Update → executed after each iteration
Allowed variations:

Example 1: Printing natural numbers

Example 2: Printing odd numbers
public class sample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if(i%2!=0)
System.out.print(i + " ");
}
}
}
output:
1 3 5 7 9
Example 3: Printing multiples of 3
public class sample {
public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
if(i%3==0)
System.out.print(i + " ");
}
}
}
Output:
3 6 9 12 15
Example 4: Multiples of 3 and 5
public class sample {
public static void main(String[] args) {
for (int i = 1; i <= 50; i++) {
if(i%3==0&& i%5==0)
System.out.print(i + " ");
}
}
}
output:
15 30 45
Example 5: Multiples of 3 or 5
public class sample {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
if(i%3==0|| i%5==0)
System.out.print(i + " ");
}
}
}
Output:
3 5 6 9 10 12 15 18 20
Example 5:Divisors and number of divisors of given number
public class sample {
public static void main(String[] args) {
int givenNumber=63;
int count=0;
for (int i = 1; i <= givenNumber; i++) {
if(givenNumber%i==0) {
System.out.print(i + " ");
count++;
}
}
System.out.println();
System.out.println(" Number of divisors: "+count);
}
}
Output:
1 3 7 9 21 63
Number of divisors: 6
Example 6: Prime numbers
public class ForLoopPrimeNumber {
public static void main(String[] args) {
int input=997;
if(input<=0)
System.out.println(input+" is Not a prime number ");
for(int i=2;i<input;i++) {
if(input%i==0)
{
System.out.println(input+" is Not a prime number ");
break;
}
else
{
System.out.println(input+" is a prime number ");
break;
}
}
}
}
output:997 is a prime number
Example 7: Reverse a positive number
public static void main(String[] args) {
int givenNumber=987654321;
int temp=givenNumber;
int reverse=0;
for( ;givenNumber>0; givenNumber=givenNumber/10 )
{
int getLastDigit=givenNumber%10;//4
reverse=reverse*10+getLastDigit;
}
System.out.println("The reverse of "+temp+" is "+reverse);
}
}
Output:
The reverse of 987654321 is 123456789
Example:8 Count number of digits and sum of digits
public class CountDigitsForLoop {
public static void main(String[] args) {
int givenNumber=16789;
int temp=givenNumber;
int sum=0;
int count=0;
for( ; givenNumber>0; givenNumber=givenNumber/10 ) {
int getLast=givenNumber%10;
sum=sum+getLast;
count++;
}
System.out.println("The number of digits in "+temp+" is "+count);
System.out.println("The sum of digits in "+temp+" is "+sum);
}
}
Output:
The number of digits in 16789 is 5
The sum of digits in 16789 is 31
Example 9:Finding whether the number is palindrome or not
public class PalindromeForLoop {
public static void main(String[] args) {
int givenNumber=13318;
int temp=givenNumber;
int isPalindrome=0;
for(temp=givenNumber; temp>0; temp=temp/10 ) {
int lastDigit=temp%10;
isPalindrome=isPalindrome*10+lastDigit;
}
if(isPalindrome==givenNumber)
System.out.println(givenNumber+" is a palindrome number");
else
System.out.println(givenNumber+" is NOT a palindrome number");
}
}
output:
13318 is NOT a palindrome number
Top comments (0)