Loop or Control Structure Statement in Java:-
It is used to enumerate large statements of data using a single repeatable block. Loop is also called control Structure.
It is used to solve range based problem, for example, we want to display marks of 5000 students.
Loop is mostly used in Array and Collection type variable.
Loop uses three statements for program execution
Forward Backward
initialization :- i= min i=max
Condition :- i<=max i>=min
Iteration i++ i--
Type of Loop:-
1) Entry Control:-
First Check Condition Then Execute Statement
1.1) while:-
This loop will work when the condition will be true, while loop can be used to create a finite and infinite based program both.
but we mostly prefer infinite based program using while because for loop provide simple structure to implement the finite based program.
finite while loop:-
Syntax of While Loop:-
but we mostly prefer infinite based program using while because for loop provide simple structure to implement the finite based program.
finite while loop:-
Syntax of While Loop:-
initialization;
while(condition)
{
Statement;
increment;
}
example
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
infinite
int i=1;
while(true)
{
if(i>10)
break;
System.out.println(i);
i++;
}
Program of While Loop to calculate a table to understand while loop properly:-
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int num,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
i=1;
while(i<=10)
{
System.out.println(num+ "*" + i + "=" + (num*i));
i++;
}
System.out.println("Total number of step "+i);
}
}
WAP to create a table of any number using infinite Loop:-
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int num,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
i=1;
while(true)
{
System.out.println(num+ "*" + i + "=" + (num*i));
if(i>10)
break;
i++;
}
// System.out.println("Total number of step "+i);
}
}
1) Complex structure hence it can not be managed in nested sequence easily.
2) take more process time as compare to for loop
1.2 for -
using this we can contain all statements using for statements, it provides a simple syntax structure as compared to a while and do-while loop.
for loop is mostly used to create a finite condition-based program.
1 2 4
for(init; condition; iteration)
{
3
statement;
}
finite for loop example:-
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int num,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
for(i=1;i<=10;i++)
{
System.out.println(num+ "*" + i + "=" + (num*i));
}
// System.out.println("Total number of step "+i);
}
}
Infinite for loop example:-
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int num,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
i=1;
for(;;)
{
System.out.println(num+ "*" + i + "=" + (num*i));
if(i>10)
break;
i++;
}
// System.out.println("Total number of step "+i);
}
}
Write a program to calculate factorial of any entered number?
import java.util.Scanner;
class Fact
{
public static void main(String args[])
{
int num,i,f=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
for(i=1;i<=num;i++)
{
f=f*i; #f = 120
}
System.out.println("result is "+f);
}
}
Nested For Loop:- We will write more than one for loop using a nested sequence, it Contains a collection of outer for loop and multiple inner for loop.
for(init;condition;iteration)
{
for(init;condition;iteration)
{
Statement;
}
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
.....................................................................................................
I j
1 5
2 4
3 3
4 2
5 1
Solution:-
class NestedLoop
{
public static void main(String... args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=6-i;j++)
{
System.out.print(j+ " ");
}
System.out.println();
}
}
}
......................................................
A B C D E
A B C D
A B C
A B
A
...............................................
The solution of This Program:-
class NestedLoop
{
public static void main(String... args)
{
for(int i=69;i>=65;i--)
{
for(int j=65;j<=i;j++)
{
System.out.print((char)j+" ");
}
System.out.println();
}
}
}
a b c d
A B C
a b
A
The solution to this program:-
class NestedLoop
{
public static void main(String args[])
{
for(int i=69;i>=65;i--)
{
for(int j=65;j<=i;j++)
{
if(i%2!=0)
System.out.print((char)(j)+" ");
else
System.out.print((char)(j+32)+" ");
}
System.out.println();
}
}
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.........................................................................
1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
...................................................................................
A a B b C
A a B b
A a B
A a
A
The solution to this program:-
class NestedLoop
{
public static void main(String ram[])
{
for(int i=1;i<=5;i++)
{
char ch=65;
for(int j=5;j>=i;j--)
{
if(j%2!=0)
System.out.print((char)ch +" ");
else
{
System.out.print((char)(ch+32)+" ");
ch++;
}
}
System.out.println();
}
}
}
............................................................................1 2 3 4 5
5 4 3 2
1 2 3
5 4
1
.....................................................................................
2)Exit Control:-
First, execute the loop then check the condition
2.1) do-while
Syntax of do-while
initialization;
do
{
Statement;
Increment;
}while(condition);
WAP to print 1 to 10?
class Dowhile
{
public static void main(String args[])
{
int num, i;
i=1;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
WAP to print the table of any entered number?
import java.util.Scanner;
{
public static void main(String args[])
{
int num, i;
i=1;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
WAP to print the table of any entered number?
import java.util.Scanner;
class Dowhile
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num,i;
System.out.println("enter number to print table");
num=sc.nextInt();
i=1;
do
{
System.out.println(num*i);
i++;
}while(i<=10);
}
}
WAP to print factorial of any entered number?
import java.util.Scanner;
class Dowhile
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num,i,f=1;
System.out.println("enter number to print fact");
num=sc.nextInt();
i=num;
do
{
f=f*i;
i--;
}while(i>=1);
System.out.println("Factorial is "+f);
}
}
ASSIGNMENT:-
1) WAP to calculate the sum of one digit positive number?
2) WAP to reverse pin code?
3) WAP to find the max digit is Pincode?
4) WAP to check prime?
class Checkprime
{
public static void main(String args[])
{
int num=6,c=0;
int i=1;
while(i<=num)
{
if(num%i==0)
c++;
i++;
}
if(c==2)
System.out.println("prime");
else
System.out.println("not prime");
}
}
Same program using for loop?
class Checkprime
{
public static void main(String args[])
{
int num=6,c=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
c++;
}
if(c==2)
System.out.println("prime");
else
System.out.println("not prime");
}
}
Optimize Program to Check Prime Number?
class Checkprime
{
public static void main(String args[])
{
int num=6,c=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
c++;
break;
}
}
if(c==0)
System.out.println("prime");
else
System.out.println("not prime");
}
}
Another way to create a prime number?
import java.util.Scanner;
class Checkprime
{
public static void main(String args[])
{
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number to check prime");
num=sc.nextInt();
int count=0,i;
for(i=2;i<num;i++)
{
if(num%i==0)
break;
}
if(num==i)
System.out.println("Prime");
else
System.out.println("Not prime");
}
}
import java.util.Scanner;
class Checkprime
{
public static void main(String args[])
{
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number to check prime");
num=sc.nextInt();
int count=0,i;
for(i=2;i<num/2;i++)
{
if(num%i==0)
break;
}
if(num/2==i)
System.out.println("Prime");
else
System.out.println("Not prime");
}
}
5) WAP to print Fibonacci series?
0,1,1,2,3,5,8,13
Assignment of While Loop?
Assignment of While Loop?
1) WAP to check prime number using an infinite loop?
2) WAP to print A to Z using a while loop?
3) WAP to count total even and odd number in pin code using an infinite loop?
Nested For Loop Assignments:-
A B C D E
B C D E
C D E
D E
E
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2
3 5
7 11 13
17 19 23 29
................................
a e i o u
a e i o
a e i
a e
a
.................................
*
* * *
* * * * *
* * *
*
......................................................................................................................................................
Break and Continue Statement:-
https://www.shivatutorials.com/2019/09/break-and-continue-statement-in-java.html
..............................................................................................................................
..............................................................................................................................
12345
ReplyDelete2345
345
45
5
class pp
{
public static void main(String args[])
{
for(int i=1; i<=5; i++)
{
for(int k=3; k<=i+1; k++)
{
System.out.print(" ");
}
for(int j=i; j<=5; j++)
{
System.out.print(j);
}
System.out.println(" ");
}
}
}
/*1 2 3 4 5
ReplyDelete1 2 3 4
1 2 3
1 2
1*/
class pp
{
public static void main(String args[])
{
int stc=5,spc=0;
for(int i=1; i<=5; i++)
{
for(int j=1; j<=stc; j++)
{
System.out.print(j);
}
for(int k=1; k<=spc; k++)
{
System.out.print(" ");
}
stc--;
System.out.println(" ");
}
}
}
/*A B C D E
ReplyDeleteA B C D
A B C
A B
A*/
class pp
{
public static void main(String args[])
{
int stc=69,spc=0;
int j;
for(int i=1; i<=5; i++)
{
for( j=65; j<=stc; j++)
{
char c=(char)j;
System.out.print(c);
}
stc--;
System.out.println(" ");
}
}
}
ReplyDeletepackage Pattern;
/**
A B C D E
a b c d
A B C
a b
A
* @author HP
*/
public class Pattern3 {
public static void main(String[] args) {
int i,j,count;//
for(i=69;i>=65;i--)
{
for(j=65;j<=i;j++)
{
if(i%2==0)
{
j=(char)(j+32);
System.out.print((char)j+" ");
j=j-32;
}
else
System.out.print((char)j+" ");
}
System.out.println("");
}
}
}
12345
ReplyDelete5432
123
54
1
package corejava;
public class Loop {
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
if(i%2!=0)
System.out.print(6-j);
else
System.out.print(j);
}
System.out.println(" ");
}
}
}
12345
ReplyDelete5432
123
54
1
public class Loop {
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
if(i%2!=0)
System.out.print(6-j);
else
System.out.print(j);
}
System.out.println(" ");
}
}
}
WAP to calculate the sum of one digit positive number?
ReplyDeleteimport java.util.Scanner;
public class DigitSum {
public static void main(String[] args) {
int m,n,sum = 0;
Scanner sc =new Scanner(System.in);
System.out.println("enter number");
m =sc.nextInt();
while(m>0)
{
n=m%10;
sum=sum+n;m=m/10;
}
System.out.println("sum of digit" + sum);
}
}
WAP to reverse pin code?
ReplyDeleteimport java.util.Scanner;
public class ReversePinCode {
public static void main(String[] args) {
int num=0;
int renum=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter number");
num= sc.nextInt();
while( num != 0 )
{
renum= renum*10;
renum= renum+num%2;
num =num/10;
System.out.println(" " + num);
}
}
}
WAP to print A to Z using a while loop?
ReplyDeletepublic class Alphabets {
public static void main(String[] args) {
char t='a';
while(t<='z')
{
System.out.println(t);
t++;
}
}
}
WAP to count total even and odd number in pin code?
ReplyDeletepublic class CountEvenOddNumber {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("enter pin code" );
n=sc.nextInt();
if(n%2==0)
{
System.out.println("the number " + n +"even");
}
else
{
System.out.println("the number" + n +"odd");
}
}
}
import java.util.Scanner;
ReplyDeleteclass table
{
public static void main(String[]arg)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter num");
int num=sc.nextInt();
int i=0;
while(i<=10)
{
System.out.println(num+"*"+i+"="+(num*i));
i++;
}
System.out.println("total step of i"+i);
}
}
import java.util.Scanner;
ReplyDeleteclass fact
{
public static void main(String[]arg)
{
int f=1;
Scanner sc=new Scanner(System.in);
System.out.println("enter the value");
int num=sc.nextInt();
for(int i=1; i<=num; i++)
{
f=f*i;
System.out.println(f);
}
}
}
class loop
ReplyDelete{
public static void main(String[]arg)
{
for(int j=5; j>=1; j--)
{
for(int i=1; i<=j; i++)
{
System.out.print(i);
}
System.out.println();
}
}
}
class loop
ReplyDelete{
public static void main(String[]arg)
{
for(int i=69; i>=65; i--)
{
for(int j=65; j<=i; j++)
{
System.out.print((char)j+"");
}
System.out.println();
}
}
}
class loop1
ReplyDelete{
public static void main(String[]arg)
{
for(int i=69; i>=65; i--)
{
for(int j=65; j<=i; j++)
{
if(i%2!=0)
System.out.print((char)j+ " ");
else
System.out.print((char)(j+32)+ " ");
}
System.out.println();
}
}
}
class loopdowhile
ReplyDelete{
public static void main(String[]arg)
{
int num, i;
i=1;
do
{
System.out.println(i);
i++;
}
while(i<=10);
}
}
class forloop3
ReplyDelete{
public static void main(String[]arg)
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
class looppp
ReplyDelete{
public static void main(String[]arg)
{
for(int i=1; i<=5; i++)
{
for(int j=5; j>=i;j--)
{
if(i%2!=0)
System.out.print(6-j);
else
System.out.print(j);
}
System.out.println();
}
}
}
import java.util.Scanner;
ReplyDeleteclass primeno
{
public static void main(String[]arg)
{
int temp=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the num...");
int num=sc.nextInt();
for(int i=2; i<num-1; i++)
{
if(num%i==0)
{
temp=temp+1;
}
}
if(temp==0)
{
System.out.println(num+" is primeno");
}
else
{
System.out.println(num+" is not primeno");
}
}}
import java.util.Scanner;
ReplyDeleteclass primenowhileloop
{
public static void main(String[]arg)
{
int temp=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the num...");
int num=sc.nextInt();
int i=1;
while(i<=num)
{
if(num%i==0)
temp++;
i++;
}
if(temp==2)
System.out.println(num+ " is a primeno");
else
System.out.println(num+ " is not a primeno");
}
}
class Fibonacciseries
ReplyDelete{
public static void main(String[]arg)
{
int a=0, b=1,c;
for(int i=1; i<=10; i++)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}
class primenumberusinganinfiniteloop
ReplyDelete{
public static void main(String[]arg)
{
int num=11, t=0;
int i=1;
while(i<=num)
{
if(num%i==0)
t++;
i++;
}
if(t==2)
System.out.println("prime");
else
System.out.println("notprime");
}
}
WAP to print A to Z using a while loop?
ReplyDeleteclass P
{
public static void main(String[]arg)
{
int a=65;
System.out.print((char)a+" ");
int i=1;
while(i<=25)
{
a=a+1;
System.out.print((char)a+" ");
i++;
}
}
}
A B C D E
ReplyDeleteB C D E
C D E
D E
E
class P
{
public static void main(String[]arg)
{
int c=64,d;
for(int i=1; i<=5; i++)
{
for(int j=i; j<=5; j++)
{
d=c+j;
System.out.print(" "+(char)d+" ");
}
System.out.println();
for(int k=1; k<=i; k++)
{
System.out.print(" ");
}
}
}
}
Nitesh bamotriya
ReplyDeleteimport java.util.Scanner;
class Table
{
public static void main(String args[])
{
int num,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
i=1;
while(i<=10)
{
System.out.println(num+ "*" + i + "=" + (num*i));
i++;
Nitesh bamotriya
ReplyDeleteimport java.util.Scanner;
class Fact
{
public static void main(String args[])
{
int num,i,f=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
for(i=1;i<=num;i++)
{
f=f*i; #f = 120
}
System.out.println("result is "+f);
Nitesh bamotriya
ReplyDeleteimport java.util.Scanner;
class Fact
{
public static void main(String args[])
{
int num,i,f=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number for table");
num=sc.nextInt();
for(i=1;i<=num;i++)
{
f=f*i; #f = 120
}
System.out.println("result is "+f);
Nitesh bamotriya
ReplyDeleteimport java.util.Scanner;
class Checkprime
{
public static void main(String args[])
{
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number to check prime");
num=sc.nextInt();
int count=0,i;
for(i=2;i<num;i++)
{
if(num%i==0)
break;
}
if(num==i)
System.out.println("Prime");
else
System.out.println("Not prime");
}
import java.util.*;
ReplyDeleteclass Infinite
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int num= sc.nextInt();
while(num!=0)
{
num=num+num;
System.out.println(num);
}
}
}
import java.util.*;
ReplyDeleteclass Fact
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int num= sc.nextInt();
int fact=1;
for(int i=1;i<=num;i++)
{
fact=fact*i;
System.out.println(fact);
}
}
}
class exampleloop
ReplyDelete{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
OUTPUT:-
1
12
123
1234
12345
class exampleloop
ReplyDelete{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
OUTPUT:=
1
22
333
4444
55555
class exampleloop
ReplyDelete{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
output:=
12345
1234
123
12
1
/*Prime number */
ReplyDeleteimport java.util.Scanner;
class Prime_number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int number;
number = sc.nextInt();
int count=0;
int i=1;
while(i<=number)
{
if(number%i==0)
{
count++;
}
i++;
}
if(count==2)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
class Patt
ReplyDelete{
public static void main(String[] args)
{
int i,j,count;//
for(i=69;i>=65;i--)
{
for(j=65;j<=i;j++)
{
if(i%2==0)
{
j=(char)(j+32);
System.out.print((char)j+" ");
j=j-32;
}
else
System.out.print((char)j+" ");
}
System.out.println("");
}
}
}
----------------
class pp
{
public static void main(String args[])
{
int stc=69,spc=0;
int j;
for(int i=1; i<=5; i++)
{
for( j=65; j<=stc; j++)
{
char c=(char)j;
System.out.print(c);
}
stc--;
System.out.println(" ");
}
}
}
---------------
class Patt
{
public static void main(String args[])
{
int i,j;
for (i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if (j>=i)
{
System.out.print(j);
}
}
System.out.println("");
}
}
}
-----------------
------
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .