Home | | Sitemap ||Page number :18

Example 6.

// A program to print the following pattern /* 54321 5432 543 54 5 /* // author Nitish Upreti

class pat6 { public static void main(String args[])

{ int c,c1; for(c=1;c<=5;c++) {

for(c1=5;c1>=c;c1--)

{ System.out.print(+c1); } System.out.println(); }

} }

Example 7.

/* A program to print the following pattern

as

class pat7 { public static void main(String args[]) {

int c,c1,c2,c3; for(c=1;c<=5;c++) {

for(c1=5;c1>=c;c1--)

{ System.out.print(" "); }

for(c2=1;c2<=c;c2++)

{ System.out.print("*"); } System.out.println(); } } }

Example 8

/* A program to print the following pattern

ZBBBBBBBB AZBBBBBBB AAZBBBBBB AAAZBBBBB AAAAZBBBB AAAAAZBBB AAAAAAZBB AAAAAAAZB AAAAAAAAZ */

// author Nitish Upreti

class arr_pat { int c,c1,mid=1; char arr[]={'A', 'Z','B'};

arr_pat() { for(c=1;c<=9;c++) { for(c1=1;c1<=9;c1++) { if(c==c1) { System.out.print(arr[1]); mid=0; if(mid==0) System.out.print(arr[2]); } else

System.out.print(arr[0]); } mid=1; System.out.println(); } } }

class xyz { public static void main(String args[]) { arr_pat obj=new arr_pat(); } }

Exercises

Q1. What does conditional execution of a program mean?

Q2. Explain the use of ‘if’ statement with an example.

Q3. How is the “and” (&&) operator used with ‘if’ statement?

Q4. Design a menu based calculator using ‘if’ statement.

Q5. State the difference between ‘if’ and ‘switch’ statement.

Q6. What is the difference between while and do-while loop?

Q7. What advantage does ‘for’ loop holds over ‘while’ loop?

Q8. Describe ‘break’ and ‘continue’ statement.

Q9. State the use of counters in loop.

Q10. Write a program that prints the following patterns on the screen

asa