0% found this document useful (0 votes)
2 views

whileloop java (2)

The document discusses control structures in programming, specifically focusing on iterative statements such as while, do-while, and for loops. It provides examples of how to use these loops to perform various tasks, including calculating the number of digits, summing digits, and displaying numbers in reverse order. Additionally, it covers arrays, explaining single and double-dimensional arrays, and includes examples for creating and manipulating them.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

whileloop java (2)

The document discusses control structures in programming, specifically focusing on iterative statements such as while, do-while, and for loops. It provides examples of how to use these loops to perform various tasks, including calculating the number of digits, summing digits, and displaying numbers in reverse order. Additionally, it covers arrays, explaining single and double-dimensional arrays, and includes examples for creating and manipulating them.

Uploaded by

shrutikreddy14
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Control Structures : Which are used to repeate set of statements n number of times.

The following are the different types of interative statements.

While
Do- While
for

While : As long as the given condition is true The specified statements are going
to execute when it is false the control passed to end of the while. We can specify
this loop when the number of repetition are not known to the user.
while(expr)
{
----;
----;
----;
}
Display Number of digits for a given number
class wh1
{
public static void main(String [ ] x)
{
int n,c=0;
n=Integer.parseInt(x[0]); 45 123 7654 4 0
while(n>0)
{
n=n/10; 0
c++; c=1+1
}
System.out.println("The number of digits are " + c);
}
}
Display sum of the digits for a given number
class wh1
{
public static void main(String [ ] x)
{
int n,s=0,r;
n=Integer.parseInt(x[0]); 123
while(n>0)
{
r=n%10; 10)123(12 10)12(1 10)1(0
s=s+r; 6 120 10 0
n=n/10;
}
System.out.println("The sum of digits is " + s); 6
}
}

Display numbers in reverse order.


class wh1
{
public static void main(String [ ] x)
{
int n;
n=Integer.parseInt(x[0]); 15
while(n>0)
{
System.out.println(n); 15 14 13 12 11
n--;
}
}
}

Ex 4:
class wh1
{
public static void main(String [ ] x)
{
int n,i=2;
n=Integer.parseInt(x[0]); 10
while(i<=n)
{
System.out.println(i); 2 4 6 8 10
i=i+2;
}
}
}

Ex 5:
class wh1
{
public static void main(String [ ] x)
{
int n,i=1
n=Integer.parseInt(x[0]); 5
while(i<=n)
{
System.out.println(i); 1 3 5
i+=2;
}
}
}

Do while : Which also an iterative statement through which we can repeat set of
statements
until the given condition is false. when it is false control will be passed to end
of the while.
Do
{
----;
----;
----;
}while(expr);

class wh1
{
public static void main(String [ ] x)
{
int n,s=0; 45
n=Integer.parseInt(x[0]); 10)45(4 )4(0
do 40 0
{
r=n%10; 5
s=s*10+r; 54
n=n/10;
}while(n>0);
System.out.println("The reverse of a number is " + s); 54
}
}

class wh1
{
public static void main(String [ ] x)
{
int n,s=0,k;
n=Integer.parseInt(x[0]); 153 = 1*1*1 + 5*5*5 + 3*3*3 =153
k=n; 153
do
{
r=n%10;
s=s+r*r*r; 27 + 125 +1=153
n=n/10; 1

}while(n>0);
if(s==k)
System.out.println("Amstrong Number");
else
System.out.println("Not Amstrong number");

}
}

class wh1
{
public static void main(String [ ] x)
{
int n,s=0; 44
n=Integer.parseInt(x[0]);
int k=n; 44
do
{
r=n%10; 4
s=s*10+r; 44
n=n/10;
}while(n>0);
if(s==k)
System.out.println("Palindrome" + s);
else
System.out.println("Not Palindrome" + s);
}
}
Ex 4:
class wh1
{
public static void main(String [ ] x)
{
int n,s=0,f=1;
n=Integer.parseInt(x[0]); 59 = 5+9 5*9 =45
int k=n; 59
do
{
r=n%10; 10)59(5 10)5(0
s=s+r; 9 = s=9+5 =14
f=f*r; = f=9*5=45
n=n/10;
}while(n>0);
if(s+f==k)
System.out.println("Special Number" + k);
else
System.out.println("Not Special Number" + k);
}
}

for : Which also an iterative number we can repeate set of statements n number of
times.
This loop we can specify when we know the number of repetations. It is of 3 types.

1. Simple for
2. For with infinite
3. For with comma operator.

1. Simple For : We have to specify the initial value, final value and increment
specified in single statement. The loop terminates automatically when the initial
value reaches its final value.

for(initial value; final value; increment/decrement)


{
----;
----;
----;
}

class for1
{
public static void main(String [ ] x)
{
int n,i;
n=Integer.parseInt(x[0]); 4 factors for a given number. 4 1 2 4
for(i=1;i<=n;i++)
{ 4%4
if(n%i==0)
System.out.println(i); 1 2 4
}
}
}

class for2
{
public static void main(String [ ] x)
{
int n,i,c=0;
n=Integer.parseInt(x[0]); 5
for(i=1;i<=n;i++)
{ 5%5
if(n%i==0)
c++; c=1+1
}
if(c==2)
System.out.println("Prime number");
else
System.out.println("Not Prime number");
}
}

class for3
{
public static void main(String [ ] x)
{
int n,i,c=0;
n=Integer.parseInt(x[0]); 6 1 2 3 28 1 2 4 7 14
for(i=1;i<n;i++)
{ 6
if(n%i==0)
c=c+i; 1+2=3+3=6
}
if(c==n)
System.out.println("Perfect number");
else
System.out.println("Not Perfect number");
}
}

Nested for : within for we can specify another for and so on. for each and every
outer for inner for will repeat n times. outer for is for number of rows and inner
for is for number of columns.

for(initial value; final value; increment)


{
for(initial value; final value; increment)
{
----;
----;
----;
}
}

class nfor
{
public static void main(String [ ] x)
{
int n,i,j;
n=Integer.parseInt(x[0]); 3
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(" * "); * * *
}
System.out.println(); * * *
} * * *
}
}

class nfor1
{
public static void main(String [ ] x)
{
int n,i,j;
n=Integer.parseInt(x[0]);
for(i=1;i<=n;i++)3
{
for(j=1;j<=i;j++)
{
System.out.print(" * ");*
}
System.out.println() ; * *
} * * *
}

class nfor
{
public static void main(String [ ] x)
{
int n,i,j;
n=Integer.parseInt(x[0]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(i*j + " "); 1 2 3
}
System.out.println(); 2 4 6
} 3 6 9
}
}

ex:
1 1 1 * * * 3 3 3 123 1 2 3
1 2 2 2 2 3 * * 2 2 1 2 4 5
1 2 3 3 3 3 4 5 6 * 1 1 6

For with comma operator : Some times we may have more number of initial values and
increments depends on single condition instead of specifying them separately, we
can specify them in single statement separated with comma operator.

Syn : for(initial value1,initial value 2,initial value 3; final


value ;inc1,inc2,inc3)
----;
----;
----;
}

The advantage of this loop is reduces program size.

class for3
{
public static void main(String [ ]x) 1+2+3+4+5=15
{
int n,s,i;
n=Integer.parseInt(x[0]); 5
for(i=1,s=0;i<=n;s+=i,i++); s=1+2+3+4+5
System.out.println("The sum is"+ s);
}
}

class for4
{
public static void main(String [ ]x) 1*2*3*4*5
{
int n,s,i;
n=Integer.parseInt(x[0]);
for(i=1,s=1;i<=n;s*=i,i++);

System.out.println("The factorial is"+ s);


}
}

Infinite for loop: Some times we may not have any initial value, final value and
increment in such case the loop will repeated infinite times. The will be
terminated by force with break statement.

Syn :
for(;;)
{
----;
---;
----;
if(expr)
break;
}

To find sum of given +ve no.

class for4
{
public static void main(String [ ]x)
{
int n, s=0,i=0;
for(;;)
{
n=Integer.parseInt(x[i]); 5 15 -1
if(n<0)
break;
else
s=s+n; s=5 20
i++;
}
System.out.println("The sum of positive numbers is"+s); 20
}
}

---------------------------
Array : An array is block of memory space where we can able store multiple elements
of similar data types. The array can be specified with square brackets and can be
created with new operator. The advantage is multiple elements can be stored with
single variable. The disadvantage is only one type of information can be specified.

The arrays can be divided into 2 types.


int a,b;
int a[ ] = new int[n];

1. Single Dimension Array 2. Double Dimension Array

1. Single dimension Array : In the single dimentsion array each and every element
can be specified with unique single address start with zero.

datatype variable[ ] = new datatype[size];


class Ar
{
public static void main(String [ ] x)
{
int a[ ] = new int [5]; 0 1 2 3 4
int i; 45 22 67 72 98
for(i=0;i<5;i++)
a[i]=Integer.parseInt(x[i]);
System.out.println("The entered elements are ");
for(i=0;i<5;i++)
{
System.out.println(a[i]); java Ar 45 22 67 72 98
}
}
}

class Ar
{
public static void main(String [ ] x)
{
int a[] = new int [5]; 0 1 2 3 4
int i; 45 72 43 56 98
for(i=0;i<5;i++)
a[i]=Integer.parseInt(x[i]);
System.out.println("The entered elements are ");
for(i=4;i>=0;i--)
{
System.out.println(a[i]); 98 56 43 72 45
}
}
}

class Ar
{
public static void main(String [ ] x)
{
int a[ ] = new int [10];
int i;
for(i=0;i<10;i++) 12 18 22 45 65 34 67 78
a[i]=Integer.parseInt(x[i]);
System.out.println("The entered elements are ");
int s=0;
for(i=0;i<10;i++)
{
s=s+a[i]; s=45+72+43
}
System.out.println("The sum is " + s);
}
}
Double dimension array: The memory space will be divided into 2 parts first part
represents row second part represents column. example Matracies.
0 1
0 1
int a[ ][ ]= new int[2][2]; 00 01
Accept 2 X 2 matrix display the same. 0 1 2
class Dar 1 3 4
{ 1 0 1 1
public static void main(String [ ] x)
{
int a[ ][ ]= new int[2][2];
int i,j,k=0;
for(i=0;i<2;i++) java Dar 1 2 3 4
{
for(j=0;j<2;j++)
{ 11 1 2
a[i][j]=Integer.parseInt(x[k]); 00 01
k++; k=3
} 3 4
} 10 11
for(i=0;i<2;i++)
{
for(j=0;j<2;j++) 1 2
{
System.out.print(a[i][j]+ " "); 3 4
}
System.out.println();
}
} java Dar 1 2 3 4
}
Transpose of 2 X 2 matrix
class Dar2
{
public static void main(String [ ] x);
{
int a[][]= new int[2][2];
int i,j,k=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++) 1 2
{ 00 01
a[i][j]=Integer.parseInt(x[k]); 3 4
k++; 10 11
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{ 11
System.out.print(a[j][i]+ " "); 1 3
}
System.out.println(); 2 4
}
}
}
madhu.jayaveer@gmail.com
A + AT
class Dar
{
public static void main(String [ ] x);
{
int a[ ][ ]= new int[2][2];
int i,j,k=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=Integer.parseInt(x[k]); 0 0 01
k++; 1 2
}
} 3 4
for(i=0;i<2;i++) 10 11
{
for(j=0;j<2;j++) 2 5
{ 1 1 1 1
System.out.print(a[i][j]+a[j][i] +" "); 5 8
}
System.out.println();
}
}
}
sum of two 2 X 2 matrix
class Dar4
{
public static void main(String [ ] x)
{
int a[][]= new int[2][2];
int b[][]= new int[2][2];
int i,j,k=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=Integer.parseInt(x[k]);
k++;
}
} java dar4 1 2 3 4

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=Integer.parseInt(x[k]);
k++;
}
} java dar4 1 2 3 4
A
B
for(i=0;i<2;i++) 1 2
5 6 6 8
{
for(j=0;j<2;j++) 3 4 7
8 10 12
{
System.out.print(a[i][j]+b[i][j]+ " ");
}
System.out.println();
}
}
}

You might also like