For Loops
Sum
int a,b;
b=0;
for (a=1;a<=10;a++)
{
System.out.printf("%d\n",a);
b=b+a;
}
System.out.printf("Sum of all number is - %d\n",b);
}
}
Counting of Even Number
int a;
for (a=1;a<=10;a++)
{
if(a%2==0)
{
System.out.printf("%d\n",a);
}
}
System.out.printf("Total even numbers are - %d\n",a/2);
}
}
Sum of Even Numbers
int a,b;
b=0;
for (a=1;a<=10;a++)
{
if(a%2==0)
{
System.out.printf("%d\n",a);
b=b+a;
}
}
System.out.printf("Sum of all number is - %d\n",b);
}
}
How to Find Prime Numbers
int x=3;
int b=0;
int a;
for(a=x;a>=1;a--)
{
if(x%a==0)
b++;
}
if(b>2)
{
System.out.printf("%d %d = Composite number \n ",x,b);
}
else
{
System.out.printf("%d %d = Prime number \n",x,b);
}
}
While Loop
Counting of Even Numbers
int x = 1;
System.out.println("Even numbers");
while(x<=10){
if (x%2==0)
System.out.println(x);
x++;
}
System.out.printf("Total number of Even numbers are = %d\n",x/2);
}
}
Something Basic
int a=1;
while (a<=10)
{
System.out.printf("%d\n",a);
a++;
}
}
Print Even Numbers
int x = 1;
System.out.println("Even numbers");
while(x<=10){
if (x%2==0)
System.out.println(x);
x++;
}
}
}
"In ascending Order"
Do while
Number Factorial using BigInteger class
import java.math.BigInteger;
public class BigIntegerOf100
{
public static void main(String []args){
int i= 2000;
BigInteger j= new BigInteger("1");
do {
j = j.multiply(BigInteger.valueOf(i));
i--;
} while (i>1);
System.out.println(j);
}
}
Print numbers in Ascending or Descending order
public class DowhileAScendingOrDescending {
public static void main(String[] args) {
int b='d';
int i = 1;
int j = 10;
if (b=='a')
{
do{
//if(b=='a')
System.out.printf("%d\n",i);
i++;
} while (i<=10);
System.out.println("In ascending Order");
}
else if (b=='d')
{
do{
if(b=='d')
System.out.printf("%d\n",j);
j--;
} while (j>=1);
System.out.println("In descending Order");
}
else
{
System.out.println("Invalid Input");
}
}
}
Count of Even Number
int i = 1;
int j = 0;
do{
if(i%2==0)
System.out.printf("%d\n",i);
if(i%2==0)
j++;
i++;
}while(i<=6); {
System.out.printf("Total count of even numbers = %d\n",j)
}
}
}
1 to 10 printing
int i = 1;
do{
System.out.printf("%d\n",i);
i++;
} while(i<=10);
}
10 to 1 printing
int i = 10;
do{
System.out.printf("%d\n",i);
i--;
} while(i>=1);
}
Even numbers printing
int i = 1;
do{
if(i%2==0)
System.out.printf("%d\n",i);
i++;
} while(i<=10);
System.out.println("Even Numbers");
}
}
Sum of even numbers
int i = 1;
int j = 0;
do{
if(i%2==0)
j=j+i;
if(i%2==0)
System.out.printf("%d\n",i);
i++;
} while(i<=10);
System.out.printf("Total sum of even numbers = %d\n",j);
}
Factorial of number less than 27
int i = 16;
long j= 1;
int k= i;
do{
j=i*j;
i--;
} while(i>=1);
System.out.printf("Factorial of %d is = %d\n",k,j);
}
}
Ascending or Descending (With else if and else also)
int b='d';
if(b=='a')
{
int a = 1;
while(a<=10){
System.out.printf("%d\n",a);
a++;
}
System.out.println("In Ascending Order");
}
else if (b=='d')
{
int a = 10;
while(a>=1){
System.out.printf("%d\n",a);
a--;
}
System.out.println("In Descending Order");
}
else
{
System.out.println("Invalid Input");
}
}
}
Nested Loop
Star Printlng(1)
int a,b;
for(a=5;a>=1;a--)
{
for(b=5;b>=a;b--)
{
System.out.printf("*");
}
System.out.printf("\n");
}
}
Star Printing Backwards
int a,b;
for(a=1;a<=5;a++)
{
for(b=5;b>=a;b--)
{
System.out.printf("*");
}
System.out.printf("\n");
}
}
Counting with Nested Loop
int a,b;
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
System.out.print(b);
}
System.out.printf("\n");
}
}
}
Nested Loop Counting Backward
int a,b;
for(a=5;a>=1;a--)
{
for(b=1;b<=a;b++)
{
System.out.print(b);
}
System.out.printf("\n");
}
}
}
Nested Loop Opposite Counting
int a,b;
for(a=5;a>=1;a--)
{
for(b=5;b>=a;b--)
{
System.out.print(b + " " );
Thread.sleep(1000);
}
System.out.printf("\n");
Thread.sleep(1000);
}
}
Character Pattern with Nested Loop 😊
int a,b;
for(a=97;a<=101;a++)
{
for(b=97;b<=a;b++)
{
System.out.print((char)b);
}
System.out.printf("\n");
}
}
int a,b;
for(a=65;a<=69;a++)
{
for(b=69;b>=a;b--)
{
System.out.print((char)b);
}
System.out.printf("\n");
}
}
}
int a,b;
for(a=69;a>=65;a--)
{
for(b=69;b>=a;b--)
{
System.out.print((char)b);
}
System.out.printf("\n");
}
}
}
int a,b;
for(a=65;a<=69;a++)
{
for(b=65;b<=a;b++)
{
System.out.print((char)b);
}
System.out.printf("\n");
}
}
}
int a,b;
for(a=81;a>=77;a--)
{
for(b=77;b<=a;b++)
{
System.out.print((char)b);
}
System.out.printf("\n");
}
}
Pyramid printing
public class SomethingNewLOL {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
int a,b;
for(a=1;a<=5;a++)
{
for(b=5;b>=a;b--)
{
System.out.printf(" ");
Thread.sleep(500);
}
for(b=1;b<=a;b++)
{
System.out.printf(" *");
Thread.sleep(1000);
}
System.out.printf("\n");
}
Number Pyramid
int a,b;
for(a=1;a<=5;a++)
{
for(b=5;b>=a;b--)
{
System.out.printf(" ");
}
for(b=1;b<=a;b++)
{
System.out.print(" " + a);
}
System.out.printf("\n");
}
For Inside While Loop for Star Printing
int a=1;
int b=1;
while(a<=5)
{
for(b=1;b<=a;b++)
{
System.out.printf("*");
}
a++;
System.out.printf("\n");
}
}
}
While inside Do while for Star Backward Printing
int a=5;
int b=1;
do{
while(b<=a)
{
System.out.printf("*");
b++;
}
a--;
b=1;
System.out.printf("\n");
}while(a>=1);
FOR loop inside Do While for backward star printing
int a=1;
int b;
do{
for(b=5;b>=a;b--)
{
System.out.print("*");
}
a++;
System.out.print("\n");
}while(a<=5);
}
DO while inside DO while
int a=1;
do{
int b=5;
do
{
System.out.print(" ");
b--;
}while(b>=a);
int c=1;
do{
System.out.print("*");
c++;
}while(c<=a);
a++;
System.out.printf("\n");
}while(a<=5);
DO while inside While for Backward Counting
int a=1;
int b=5;
while(a<=5)
{
do{
System.out.print(b);
b--;
}while(b>=a);
b=5;
a++;
System.out.printf("\n");
}
DO while inside For Loop
DO while inside For
int a;
int b=1;
for(a=1;a<=5;a++)
{
do{
System.out.print(b);
b++;
}while(b<=a);
b=1;
System.out.printf("\n");
}
Arrow Pattern
int a,b;
for(a=1;a<=6;a++)
{
for(b=6;b>=a;b--)
{
System.out.printf(" ");
}
for(b=1;b<=a;b++)
{
System.out.print(" *");
}
System.out.printf("\n");
}
int d=5;
do{
int c=1;
while(c<=4)
{
System.out.printf(" ");
c++;
}
c=1;
while(c<=3)
{
System.out.printf(" *");
c++;
}
d--;
c=1;
System.out.printf("\n");
}while(d>=1);
STAR – 2468 blank Pyramid - STAR
for(a=5;a>=1;a--)
{
for(b=1;b<=a;b++)
{
System.out.print("*");
}
for(b=4;b>=a;b--)
{
System.out.print(" ");
}
for(b=1;b<=a;b++)
{
System.out.print("*");
}
System.out.print("\n");
}