java Control statement looping programming example

§  Control statement looping

Q.1 type of loop:

1.    do -  while

2.    while

3.    for

    4.for – each

o   rules of loop:

1.    initialization

2.    condition

3.    statement

4.    increment/ decrement

§  structure of do-while loop:-

do   {

//statement (increment/decrement)

}

while (condition);

Q.p] write a java program which will print your name 100 time using do while loop?

Program:-

class  NamePrint  {

public static void main(String  args[])   {

int  n=100;

int  x=0;

do {

System.out.println(“programming Khata”);

X ++;

}

while  (x<=n);

}

}

v while  loop

§  Syntex   of while loop:-

while (condition)   {
//statement (increment /decrement)

}

Q.p] write a java program to print 1 to 100 using while loop?

Program:-

class  WhileLoopExample  {

public static void main(String  args[])   {

int  n=1;

while ( n <=100)     {

System.out.println(n);

 n ++;

}

}
}

Q.p] write a java program to print 50 to 100 using loop?

Program:-

class  WhileLoopExampl  {

public static void main(String  args[])   {

int  n=51;

while ( n <=51)     {

if (n%2 ==0)   {

System.out.println(n+ “the number is prime”);

}

else   {

System.out.println(n+ “the number is not prime”);

}

 n ++;

}

}
}

  


Post a Comment

0 Comments