You are on page 1of 40

Structural Programming

in
Java
 
  

College of Science
Department of Computer












........

@
@

@
@

bbu@b@bi@m@@
bbu@b@bi@m
@ q@bbi@@b@@@@bbu@baq@b
@b@)@bbr@@a@i@b@@o)
@a@b@@a@@pjbm@ @i@i@n)
@@b)b@@i@bu@bbbr@i@@w)@b2u@ni
@aq@m@bb@b@ab@bi@@b)b@@@a3i
NNb)@
@@@@bi@i@bnq@)i@aq@i
@HTextPadI@bi@a@i@nq@abbmr@
@b@3@@@HJDKI@bi@@)@bm@b@
@)@i@o@)@bbu@bi@@a@@@pb
@m@q@@pb@m@@mr@@b
@HMachine Language I

For more background material:


1) http://java.sun.com
Java Homepage
2) http://java.sun.com/docs/books/tutorial Java Online Tutorial,

@ @i@nq@bbu@b@b@b@i@@q
@@N@b)bi@a@@a@i@bb@bbu@an)
@a@i@@pb@q@o)@ai@@b@a3a@@@Z
@@N)@bbn@@@a3@@abm@o@bm
1- Class: is a blueprint that defines the variables and
the methods common to all objects of a certain kind.
Example:.
Class Ex1
{
i@)bq@o@b@@b@a3@@
}
@@
@i@b@ni@@a)@b@@@@@oq@i@o@bi@@n)@@Z
@@N@m
@@System.out.println(Hi every body);
@a)@n)3@@a)@@@@n)@i@i@b@@2@@n)@@b@
@@@@@m@)@i@a
public static void main(String[] args)
@Nbbu@b@i@i@o)@aq@@bi@i@@a@pb@@bn
class Ex1
{
public static void main(String[] args)
{
@@System.out.println(Hi every body);
}
}

@@
@@
@a3@@@i@o@m@a)@b@@a3@@ni@pjm@Z)
@b@)@@@@b)bi@a@@a@abbu@b@@o@m
@@N
@@
3- Attribute (Variable): is the data variable that is
declared inside the class in a various figure.
@@
@i@@am@b@a@a@m@Int@i@a@@ma@o@bi@am@b@i@Int
@@@ab@@n)@i@pa@)@@
@bi@@Na@@abu@i@@bi@p@b@i@b@Float@@Double@
@a@
@oq@@bm@@m@i@o@bi@Char@
n)@@b@nq@@mb@b@oq@@@m@i@o@bi@String @
Example:. Run this program see and note
class Ex1
{
public static void main(String[] args)
{
int a;
@@a=5;
float b;
@@b=5.5f;
double c;
@@c=2.25;
char x;
@@x=g;
String s;
S=Hi every body;
System.out.println(the result of a= +a);
System.out.println(the result of b= +b);
System.out.println(the result of c= +c);
System.out.println(the result of x= +x);
System.out.println(the result of s= +s);
}
}

@a@bm)@a@)@a)@b@@@@Za
@@a@b@@@@i@pjbm@bm@
4- Method : is the function that is declared inside the
class and related the class attributes.
Example:.
class Ex1
{
public void method()
{
@@)i@@@
}
}
@@i@@Li@bi@b@am@2@@a@@o@Zvq
@o)@@a)@@b@@R
5- Object: is a software bundle of variables and rlated
methods.
@@ba@bm@bn@@b)bi@am@mba@ba@@o@@@OO@Sjm
NNaUi@b)

@@Zba@
@bb@a@@a@H\n , \t , \b , \\ , \I@@bi@b@a)@aq@mb M1
@@@NNa)@)@@@o@bi@@i@@)bi

Example:
class Ex1
{
public static void main ( String[] args )
{
System.out.println ("Hello\n\tWorld\n!" );
}
}
World \ n
World \ t
.

@@

@@

@@i@b@ab)q@@m@bn
Ex.) Write a java program to output the following
@@message.

Ex.) Write a java program to output the following message.

MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

Ex.5) Write a java program to output the


@@following figure
@@

@@

@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
2- Arithmetic Operators:
+
*
/
%

use for addition


use for subtraction
use for multiplication
use for division
use for division remainder

class ex1
{
public static void main(String[] args)
{
int x=5;
int y=10;
int z ;
z=x+y
System.out.println("The addition is " +z);
z=x-y ;
System.out.println("The subtraction is " +z);
z=x*y ;
System.out.println("The multiplication is " +z);
z=x/y ;
System.out.println("The divition is " +z);
z=x%y ;
System.out.println(" the reminder is" +z);
}
}

@@b@q@@HM@M@I@b@HKKI@b@@a3 // 
@b@pb@b@@@a@@@bb@@a@pb@bq
@i pb@@@@
@@@@@int x=5;

x=x+1;

@ma@a@@a@X@a@b2@@bbq@vq@abm)@X@@i@Sjm
@)i@@@mbu@am@a@@b)b@mb@i@i@@i@i@X@@bn
@@i@@@ x++@@)i@am@@@@@x=x+1;

1.
2.
3.
4.
5.
@@ 6.

Progarm
int x=5;
x++
++x
x=x-1;
x--;
--x ;

Result
x=5
x=6
x=7
x=7-1=6
x=5
x=4
@@
@@i@bb@Sjm@@)i@)@aq@

3- Using String:

@i@oi@ui@o)@i@n)3@@@i@q@@n@b@nq@@b@nq@@ni@Y)
@NNNoi@m@Y)@
@@bi@@@bbq@a@bbu@b@@Y)@b@bi@m@@i@)@aq@
@
class ex1
{
public static void main(String[] args)
{
String @s=Hi , this is the first program;
Systen.out.println(s);
}
}
The output of this program is
Hi , this is the first string program
@

@@pa@b@pjbm@b@b@@i@bi@a@@am@a2Y)@b@bi@mb
a2Y)@
@@Z@@i@a2Y)@@a@b@nq@@i@o@bi@@@@CharAt
@@_H5I@b@Rq@@i@)i@q
char c=s1.charAt(6);@
Z@@i@a2Y)@@a@b@@@i@o@bi@@@@@@@Substring @
@@_H13L18I@2Y)@l)@i@)i@aq
String s2=s1.substring(13,18);@
@i@2Y)@@Sa@i@obi@@@@@@@@@Length
_2Y)@@@@i@)i@q@
s1.length()
@ia@a@nq@o2jm@mb@@i@i@aaq@@@i@ab)q@@@am@bn
@3@pi@@i@@a@@L@oaUi@b@q@i@2Y)@i
@@@@String s1="Hi , this is first string";@@@@@@@b2Y)@
@@
mb@@b@q@i@2Y)@i@ia@a@@@
String

H i

index

,
2

h i

11

12

13

14

15

16

17

18

19

20

21

22

23

24

@
@)@bb)q@q@i@aq@@
class ex1
{
public static void main(String[] args)
{
String s1="Hi , this is first string";@@@ \\ this is the string
char c=s1.charAt(6);
\\ to find character number 6
String s2=s1.substring(13,18);
\\ to find substring 13,18
System.out.println(s1);
System.out.println(c);
System.out.println(s2);
System.out.println(s1.length());
}
}@
aq@@HbI@mrmb@@
1. Hi , this is first string
2. H
3. First
4. 25

\\ to find length of string

R@i@a)bi@)@@@bmibi@@)@b)q@ @@b@
Write a java program to subtract tow numbers.
class ex2
{
public static void main (String[] rags)
{
int a=5;
int b=3;
int sub;
sub=a-b;
System.out.println(subtract = +sub);
}
}

---------------------------------------------------------------Write a java program to print out this shape?

class ex1
{
public static void main(String[] args)
{
int x=5;
int a=0;
int b=0;
x++;
System.out.println ("Line 1: " + "x = "+x + " a = "+a + " b = "+b);
x=x+2;
System.out.println ("Line 2: " + "x = "+x + " a = "+a + " b = "+b);
a=2 * ++x;
System.out.println ("Line 3: " + "x = "+x + " a = "+a + " b = "+b);
b=10* x;
System.out.println ("Line 4: " + "x = "+x + " a = "+a + " b = "+b);
a=3+ x++;
System.out.println ("Line 5: " + "x = "+x + " a = "+a + " b = "+b);
}}

What is the output of this program? Why?


Public class StringTest
{
public static void main(String[] a){
String s1=Hello;
String s2=World;
String s;
s=s1+s2;
System.out.println(s);
}
}
The output is : Hello World
Because the program creates two strings and then concatenates them and assigns the result in a
another string.

What is the result of this code?


String s1=Hello World;
String s2=s1.substring(0,5);
The result is : Hello

What is the result of this code?


String s=Hello World;
System.out.println(s.length());
The result is : 11

What is the result of this code?


String s1=UHD, s2=UHDS;
(s1.equals(s2));
The result is : false because s1 is not equal s2

Flow Chart:
@i@oi@o)@@q@ma@@i@aq@S3i@@ni
@aq@i@3@@oam@aq@@S)
Noi@b)b@p@aq@Sn3a@pb@oi@

Flow I@@o@bi@@abi@i@@@a@@a
@@N@bi@aHChart
Some of the common symbols used in flowcharts are
@@shown below:

@@@@@@
@@@@
@
@@
@

@@@

@@

Example: draw a flow chart to sum two numbers and print the result and
the numbers.

import java.util.Scanner;
public class Sumtwonumbers
{
public static void main ( String[ ] a)
{
int x,y,z;
Scanner Myinput = new Scanner( System.in );
x =Myinput.nextInt();
y =Myinput.nextInt();
z=x+y;
System.out.println("x="+x + " y="+ y + " z=" +z);
}
}

-----------------------------------------------------Example: draw a flow chart to determine whither an input number odd or


even.


)(Input
) (output


) (Input




1. Scanner
2. JOPtionpain
3. BufferReader

)@)@@pra@i@prmb@a@@aq@@@i@ma@@@n)3@)@Z@(Scanner)@b
@@@aq@i@@b@@n)3
@@
@@@import java.util.Scanner; @)@@@@b@S)@q@Z@
@i@oni@p@@b@@i@@m@b)@Z
;)@@Scanner name=new @Scanner (System.in
@

@i@@om@@b)@i@i@b@@aaq@@@a3@@Z@)@
A=name.nextInt(); \\ if variable is integer
B=name.nextLine(); \\ if variable is String
C=name.nextDouble(); \\ if variable is double
D=name.nextFloat(); \\ if variable is float
@oi@3@p@mb@@i@pra@i@2@bbq@@b@pq@pb@a@i@b)b@aq@bn
NNNNi@bb@Sjm@
@
OOWrite a java program to subtract tow numbers.
class ex2
{
public static void main (String[] rags)
{
int a=5;
int b=3;
int sub;
sub=a-b;
System.out.println(subtract = +sub);
}
}

@
Write a java program to subtract tow input numbers.
import java.util.Scanner;
class ex2
{
public static void main (String[] rags)
{
int A;
int B;
int sub;
Scanner m=new Scanner(System.in);
System.out.println(Enter first number);
A=m.nextInt();
System.out.println(Enter Second number);
B=m.nextInt();
sub=A-B;
System.out.println(subtract = +sub);
}}

@
@
@

Exe:: Write a java program for ask to input your name


public class Ex16
{
public static void main(String[] a){
String myname;
Scanner theinput = new Scanner( System.in );
System.out.print( Enter your name ?);
myname=theinput.nextLine();
System.out.println( My name is : + myname);
}
}
M@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Ex. 17) write a program to ask the user to enter his name, age, and college and print
them on the screen.
Import java.util.Scanner;
class input
{
public static void main(String[] args)
{
int A,B,C;
Scanner m =new Scanner(System.in);
System.out.println(Enter your name);
A=m.nextInt();
System.out.println(Enter your age);
B=m.nextInt();
System.out.println(Enter your college);
C=m.nextInt();
System.out.println( your name is +A);
System.out.println( your age is +B);
System.out.println( your college is +C);
}}

@
@Relational Operators:@@
a@b@@abq@b@b@i@aaq@S)@mb@@bmb@a@ @
NNma@3@bmi@n@i@a@@a@
@
@
@
@
@
@
@
@
@
@
M@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM@
@

Boolean OperatorsZZ@
Java uses && for the logical and operator and || for the logical or operator. In
evaluating Boolean expressions, Java uses Boolean Logic principles.
@
@@L@ab@@n)i@pa@Ho)b@@o)I@@b@o)a@@ma@u@an)@i
@@abbu@b)@aq@b@@L@@b@o)a@oi@@@a3@@a@a@m@i@i@a3
N2i@n@@mb@ba@ @HAndI@i@o@bi@H&&Ib@@@HOrI@i@o@bi@@H||Ib
@
@
@

o2i@m@mb@bm@i@bb@)@i@a@b@@@

public class Ex19


{
public static void main(String[] a){
int x=4,y=5;
boolean f1,f2,f3,f4,f5;
f1=(x==y);
f2=(x!=y);
f3=(x>10 || y>1);
f4=(x>10 && y>1);
f5=(x>1 && y>1);
System.out.println(" f1 = " + f1);
System.out.println(" f2 = " + f2);
System.out.println(" f3 = " + f3);
System.out.println(" f4 = " + f4);
System.out.println(" f5 = " + f5);
}
}
-----------------------------------------------------------------------------------------------------------@Y)@i@i@mibi@)@ab@
public class Ex20
{
public static void main(String[] a){
String s1=Aso;
String s2=Mr.Aso;
String s3;
boolean f;
s3=s2.substring(3,6);
f=(s1.equals(s2));
System.out.println(s1=s2 + f);
f=(s1.equals(s3));
System.out.println(s1=s3 + f);
}
}

(if-else statement):
There is a Java statement that chooses between two alternative actions.
@@@ab)@aq@m@bb@@@bbu@b@2@@an)@OOZ@bM@@ma if-else
@u@@o@bi@@@i@@b3i@ma@u@u@n)3@@ni@R@i
@a@i@o)a@@u@@i@HelseI@i@nb@@o@@i@a@i@o)a@
@@NNN@Hif-elseI@R@a@@o@@u@@@HelseI@i@)@n
The general form of the if-else-statement is as follows:
if (Boolean-expression)
yes-statement
else
no-statement

@Y2@Hif-elseI@R)a3i@@i@3@mibi@mb@@i@@3abi@@@bi@bn
@p@100@bm@nb@q)@n@m@iL@@bi@am@bi@@@a@@@i
@@50@@m@ma@@50@i@b@@a)@b@)@oi@bbq@o3i@b@om@q
@o3i@b@@oamb@a@i@q@pY@@50@@i@o3i@b@@oam@a@i@q@mb@b
@@NNN@3@bu@n)3@@aUi@i@Sjm@abbu@@aq@i@b@@bi@bn@NN
@@
@@
class ifelsestatement
{
public static void main(String[] args)
{
int x= 100;
if (x>=50)
System.out.println(You can buy thist price);
else
System.out.println(You can not buy thist price);
}
}@
--------------------------------------------------------------------------------------------------------@@
@@

@oN@bu@b@aUi@i@Sjm@bn@ 42@i@b@ garde@@i@ a@aq@@Sjm


@@NNNNN@HelseI@b@HifI@u
@42@ @ @ @ 50@ i@ nb@ m@ @ o@ a@ @ @ @ i@ a@ HifI@ i
@@Yi@@o@a@@@@HelseI@i@)@n@@n)a@@oNb@HifI@u@ma
@@@@NNNoN@@HelseI@u@ma@50
@@
@@
class student
{
public static void main(String[] args)
{
int garde =42;
if (grade >=50) {
System.out.print(The student has passed);
System.out.println(Good);
}
else {
System.out.print( Student has failed);
System.out.println(Bad);
}
}}@
@
@

@ab@n)3@@@mb@mb@@@@bm@a@bm)@a@i@Rq@if-else@OOSjm
@@N@i@H)@I@@)@@i@i@a)
@@
@@
@@
@@
@@
@@
@@
@@
@@
@@
@@
@@

@@
@@

(if-else statement)@@@@@@@mibi@)@a@b@b)q@
What is the output of this program?

Whats the output?


public class output2
{
public static void main ( String[] args)
{
int x=49;
if (x>=50)
System.out.println(Passed);
else
System.out.println (Failed);
}
}
------------------------------------------------------------------------------------------

Whats the output?


public class output3
{ public static void main ( String[] args)
{
int x=18;
x++;
if (x++<=18)
System.out.println(Teenager);
else System.out.println(Adult);
}}

-----------------------------------------------------------------------------------------------------------

Whats the output3


public class output4
{
public static void main ( String[] args)
{
int x=60;
if (x>=50)
{
System.out.println(Passed);
System.out.println(The value is greater than 50);
}
else
{
System.out.println (Failed);
System.out.println(The value is less than 50);
}
}
}

----------------------------------------------------------------------------------------

Whats the output?


public class output5
{
public static void main ( String[] args)
{
int x=79;
if (x<50)
System.out.println(Failed);
else if (x>=50 && x<60)
System.out.println(Passed);
else if (x>=60 && x<70)
System.out.println(Mediam);
else if (x>=70 && x<80)
System.out.println(Good);
else if (x>=80 && x<90)
System.out.println(V.Good);
else if (x>=90 && x<100)
System.out.println(Excelent);
}
}
------------------------------------------------------------------------------------------Whats the output?

import java.util.Scanner;
public class output6
{
public static void main ( String[] args)
{
int grade;
Scanner input = new Scanner ( System.in );
System.out.print ( "Please enter a grade: " );
grade = input.nextInt();
If (grade >= 60)
System.out.println ( "Passed" );
}
}

-----------------------------------------------------------------------------------------

Switch statement:
The if-else statement can be cumbersome when you have to deal with multiple
selections. The switch-statement is another kind of Java statement that also
implements multi-way branches.

! 
  
     
 Switch statement
. &$ (Switch)
  %(-$ ) *+, )  $ ' (% &$ (if-else)
..%!(+$  6- ' ( ( $ 3, )(012 ) )/
.:* )  +, 8 

@^)@b@bii@bn@L@a@Rq@i@b@@aa@X@nb@a)@aq@@Z@3
@i@ai@baj@)@
@@NR
)@a@aq@@i@HYesI@a@a@i@i@b@X@@Z@
@@R
)@aq@@@ @HNoI@a@@i@i@b@X@@Z@@
@bbq@@i@(Invalid)@a@@@a@@2vi@m@nq@i@i@b@X@@Z)@
..R)@aq
@@

The for RepetitionZZ

Repetition structures allow us to repeat some action or set of actions.


This is useful if we would like to execute an action more than once, until a condition
becomes true or while a condition is true.
@@Hbu@I@@bi@obi@2@@a@i@bi@3@ma@(for Loop)
@@NNNN@a@@b@a
@@@oi@@i@oi@a@ni@@@b@bm@@b@oni@@@i
@a3@@oi@a@aq@i@i@b@@ob@@@@55]10K9K8K7K6K5K4K3K2K1
N@@i@i@b@bbq@@bm@@@ia@i@b@bbq@i@m
class addition
{
public static void main(String[] rags)
{
int a=1; int b=2; int c=3; int d=4; int e=5; int f=6; int g=7; int h=8; int i=9; int j=10;
int sum =a+b+c+d+e+f+g+h+i+j;
System.out.println(sum);
}}

@a@bm@@am@a3@@bm@ m@i@@b)bi@@Hfor LoopI@b@bii@i


@@@i@i
class forloop
{
public static void main(String[] rags)
{
int z=0;
for (int i=1;i<=10;i++)
z=z+i;
System.out.println(z);
}}
@
Zaq@3@
@@@@i@pb@b@HaI@@for (int i=1;i<=10;i++)@n)3@
int i=1; pb@rn)@@b@@b@ma@
i<=10;@@@@@on)@10@b@n@@b@
i++ Z@@@@@@@pb@b@b@@bu@@b@

i++
1
2
3
4
5
6
7
8
9
10
11

i<=10;

pb@b@t@@oaUi@@i@bi@m@
@
Stop or continue loop
z=z+i;

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No

continue
continue
continue
continue
continue
continue
continue
continue
continue
continue
Stop loop

Z=0+1 =1
Z=1+2 =3
Z=3+3 =6
Z=6+4 =10
Z=10+5 =15
Z=15+6 =21
Z=21+7 =28
Z=28+8 =36
Z=36+9 =45
Z=45+10 =55
Z=55

@i@@bi@@m@m@bn@
class ex21
{
public static void main(String[] rags)
{
int f=1;
for(int i=1;i<5;i++)
{
f=f*I;
System.out.println(The final result is = +f);
}
}}

@n)3@ @ babu@ bm@ aq@ b@ b@ a)@ a@ @ @ i@ bi@ @ @ Sjm


@@ i@ aq@ a@ b@ a@ aq@ @ System.out.println(Hello World );
@bm@b@bm@i@q@a@@@a@aq@@i@pa@bq@q@bb
@n)@a@pai@b@q@bbm@vm@bm@bn@pb@@ @ )@@pa@bq@q
@n@q@q@@bvm@@@bn@mb@@a@t@a@@oq@
NN@a@q@a@b@a@pai@bbq

------------------------------------------------------------------------------------------------------

10@bm@1@a@bmu@b@@@i@o@bi@aq@

@
@
@b)@a@bm@mb@aUi@u@@@
@@)i@u@@m@i@pu@b@m@ab)q@mb
if (i%2 ==0)
System.out.println(even numbers is +i);
@)i@u@@m@@bm@b@aa@mb
if (i%2 !=0)
System.out.println(even numbers is +i);
@@N9K7K5K3K1@ma@ni@b@bbq@oi@i@10@bm@1@a@bm@b@@@aa@mb
NNN@i@
int z=0;
if (i%2 !=0)
z=z+I;
System.out.println(the result is +z);

Double loop (Multiple loop):


@mbq@b@amb@i@mb@a@@i@bi@q@@mb@@a)@aq@mb@bu
@@@2@Hn@NNNN@@L@@L@bn)@I@@b@
@@mb@bi@bm@q@@@@aq@@bi@a@bn@

pbb@@t@@oaUi@bm@bi@m@@
i++
1
2
3
4
5

I<=4
yes
yes
yes
yes
No

Go to the next loop or not?


go
go
go
go
Stop, dont go to the next
loop

Print J++
12345
12345
12345
12345
12345
12345
12345
12345

Z@aq

bi@m@@

i++
1
2
3
4
5

I<=4
yes
yes
yes
yes
No

Go to the next loop or not?


go
go
go
go
Stop, dont go to the next
loop

J=5-i
J=5-1 =4
J=5-2 =3
J=5-3 =2
J=5-4 =1
Finql result

Print j-4321
321
21
1
4321
321
21
1

i@bi@@m@m@
@

Factorial
@a@b@120@]@1J2J3J4J5@mb@5@b@a@@i@b@a@b@@ma@bnb
@6@]@1J2J3@mb@3@b@HSI@
@@i@t@b@bii@2i@@b@bYb@am@abbu@b@
@@NNN@a@2i@@5@bnb@ob@@@@i
@i@am@a@a@aq@mb@aUi@@@i@@b@@@m@a3@Z@
@mb@@@@ni@a@a@i@a@@@i@i@b@a3@oi@a
@@a@i@a@@)@i@i@b@a3@oi@pbi@@@bi@aq
)@@n@
@@5@bm@1@b@@)@q@Z@
@a@m@@aa3@@b@@pb@b@aq@@ai@@)@b@Z)
o2j@
class factorial
{
public static void main (String[] args)
{
int f=1;
for (int i=1;i<=5;i++)
f=f*i;
System.out.println(final result is = +f);
}
}
i++
1
2
3
4
5
6

I<=5
yes
yes
yes
yes
yes
no

Continue or stop loop


continue
continue
continue
continue
continue
stop

NNbi@m@@
f=f*i
Final resul
f=1*1
f=1
f=1*2
f=2
f=2*3
f=6
f=6*4
f=24
f=24*5
f=120
Final result is = 120

Ex.1 Write a Java program to print the following output through using loop?.@

Ex.2 Write a program to sum a series, such as + 1/3 + + +1/10 ?@


Ex.3 Write a program to sort three input numbers?@
@
Ex.4 Write a program to find factorial 8 ?
Ex.5 Write a program to factorial 8/5 ?

Ex.6 Write a program to find the result of the following serious ?


@
@
@
@
@
@
Ex.7 Write a program to factorial !(8-3)/!5 ?
@
@
@
@
@
@
@
@
@

Array:
An array is a container object that holds a fixed number of
values of a single type. The length of an array is established
when the array is created. After creation, its length is fixed.
This section discusses arrays in greater detail.
@o@@am@bma@@b@mbq@@Xmb@b@
@@@@@I@@ni@@b@@u@@R@i
Tow diamentional MM@@@@@One diamentional array
@@NNmb@3@@@@a@@@H@@array
@10@@mb@q@@@@@@ni@oi@@)@@i@
@@@HI@om@bq@bb@)@b@10@@ni@@Ub)@ma@HIb
@@j@2Y)@b@b@o@@pb@a@@@b@HIb
2i@2Y)@b@bi@fani@bm@i@a@@@b@oi@a@a@
@abbu@@@@aq@@@@m@a@bn
@
@

@
@
@
@

class Anarray
{
public static void main(String[] args)
{@
int [] Arr ;
\\ decleration of an array@@@@@m@
Arr=new int [4]; \\ allocate memory of 4 integer numbera@@@i@@4@@Na@
Arr[0]=10;
\\ initialize of index 0 @@)@b@@i@@ba@@@@
Arr[1]=15;
\\ initialize of index 1 @b@@i@@ba
Arr[2]=18;
\\ initialize of index 2 @b@@i@@ba
Arr[3]=11;
\\ initialize of index 3 @@)@b@@i@@ba@
System.out.println(element at index 0 = +Arr[0]);@
System.out.println(element at index 1 = +Arr[1]);@
System.out.println(element at index 2 = +Arr[2]);@
System.out.println(element at index 3 = +Arr[3]);
}}@
@
@i@@bv@a@b@o)@i@@bbq@a@m@bui@bu@@a)@aq@@i@Sjm
@aq@b@@bn@L@pb@b@@@oaUi@bm@b@ba@a@mb@3@i@bm@@aa
@@@i@oi@bi@t@b@oi@amb@i@a)i@@bb@@b@i@@)
NNpbi@oq@bv@i@@o32i@a@)i@@HaI @
class Anarray
{
public static void main(String[] args)
{
int [] Arr ={10,15,18,11}; \\ decleration an intialization
for (int i=0;i<Arr.length;i++)
System.out.println(Arr[i]);
}
}
aq@i@3@@@
Index of array
0
1 2 3
Element of array 10 15 18 11

@)@@bi@a@@aNa@i@@10L@@@@a@aq@@OOSjm
@i@bbu@b@amb@@m@b@m@a@m@b@b@o@bm@oi
@mrmb@)@mb@3@i@L@m@b@@b@@@i@pa@)@@ab
NNNNi@bi@
@

@
@
@
@
@
@
@
@
@
@
@
M@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM@
@
In this program, the indexes are multiplied and put into the array.
@aq@@L@@i@@mi@bbq@a@Y@i@b@aaq@@
@@
@
@
@
@
@
@
@
@
@
@
@

i++
0
1
2
3
4
5
6
7
8
9
10

i<10
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no

NNNbi@m@@
n[i]=i*i
Stop or continue loop
continue
n[0]=0*0
continue
n[0]=1*1
continue
n[0]=2*2
continue
n[0]=3*3
continue
n[0]=4*4
continue
n[0]=5*5
continue
n[0]=6*6
continue
n[0]=7*7
continue
n[0]=8*8
continue
n[0]=9*9
stop

Final result
0
1
4
9
16
25
36
49
64
81

@
------------------------------------------------------------------------------------------------------------@
@
@@@)@ba3@@
@

Ex.43 Write a program to copy the Array A={22,33,44,55,66,77,88,99} to Array B


(empty) and print the Array B.@

Ex.44 A={2,3,5,7,9} and B={8,7,5,3,1}, Write a program to sum the Array A and
Array B and put them into Array C.@

Ex.45 in the Ex.44 Write a program to concatenate Array A and Array B and put into
Array D.@

Ex.46 Write a program to reverse the elements in Array A={22,33,44,55,66,77,88,99}. @


Ex.47 Write a program to find the Max number in the Array A={12,3,4,5,16,7,8,9}.@
Ex.48 Write a program to insert the characters in the following text into an array and
print the result. Here, we use the string and array together
@
@
@
@
@
@
@
@




:

/

2011/03/03

Prepared by: Ary Jafer Rahman


Second Stage
College of Science
Department of Computer
E-Mail : aryjafer@gmail.com
website: www.uhd.ueuo.com

You might also like