Professional Documents
Culture Documents
else
{
System.out.println(a+" is odd");
}
}
}
-------------------------------------------------------------------loop->when we want to execute the statement multiple times till condition is tru
e then
------we keep statement inside the loop
there are three types of loop
1-while loop
2-do while loop
3-for loop
for loop->
--------- syntex->for(inetialization ; condition ; increment)
{
----------------}
ex->wap to print the counting up to 50
class Demo
{
public static void main(String []args)
{
for(int i=1;i<=50;i++)
{
System.out.println(i);
}
}
}
ex->waap to print the counting up to given number
class Demo
{
public static void main(String []args)
{
int a =Integer.parseInt(args[0]);
for(int i=1;i<=a;i++)
{
System.out.println(i);
}
}
}
ex->wap to print the table of any number
class Demo
{
public static void main(String []args)
{
int n =Integer.parseInt(args[0]);
for(int i=1;i<=10;i++)
{
int c=n*i;
System.out.println(n+"*"+i+" = "+c);
}
}
}
--------------------------------------------------------------nested loop->loop inside the loop is called nested loop
ex->wap to print the piramid triangle(Imp)
class Demo
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
System.out.print(" ");
}
for(int k=0;k<=i;k++)
{
System.out.print(" * ");
}
System.out.println();
}
}
}
------------------------------------------------------------------break->it is a keyword that provide the termination from the current loop
countinue->it is a keyword that provide the skip of the particular statement fro
m the current loop
ex->wap to find the number is prime or not
class Demo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime Number")
;
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}
----------------------------------------------------------------------------------------Array in java->1-Normally, array is a collection of similar type of elements tha
t have ---------------contiguous memory location.
2-in Java, array is an object the contains elements of similar data type.