OOPM JAVA - program to display pattern A A B 1 A B C 2 2 A B C D 3 3 3

 Experiment No. 03

Aim: Write a program to display following patterns
          A
       A B 1
    A B C 2 2
 A B C D 3 3 3


Objective: To make use of different types of control statements and loops.

Outcome: Students have used nested for loop to implement and solved above problem.

Theory: 

Loops are as valuable control statements in programming as are the conditional statements. Loops are used for performing repetitive tasks. Read more about how loops work. For loop is the most commonly used and popular loop amongst all three loops present. The reason behind its popularity lies in the simplicity. For loop is the easiest loop to begin programming with loops. 


Syntax of for loop:

For (initialization ; condition ; update )

{

    //Body of loop

}


Parts of the for loop

For loop generally contains four parts: 

  • First is the initialization. Here we initialize the counter variables.
  • Second is the condition. Condition part contains one or more Boolean expressions that determines whether the loop will further repeat or not. If the condition satisfies (condition is true) then the statement/s under body of loop will be executed else the loop terminates.
  • Third is the body of loop. Body of loop generally contains one or more statement/s that are executed if the loop condition is true.
  • And last is the updation. Updation part generally contains expressions to update the loop counter. Updation part is sometimes also referred as Increment/Decrement part.


The while and do-while Statements

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) {

     statement(s)

}

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.


The Java programming language also provides a do-while statement, which can be expressed as follows:

do {

     statement(s)

} while (expression);


The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once


Algorithm:

  1. Start.

 2. Initialize ch='A' , ch1='Q',i=1

 3. while i<=4

 3.1 j=1

 3.2 while j<=4-i

    3.2.1 Display space

    3.2.2 j++

  3.3 j=1

  3.4 while j<=i;            

     3.4.1 Display i

     3.4.2 j++

  3.5 j=1

  3.6 while j<=i-1;            

      3.6.1 Display *

      3.6.2 Display new line

      3.6.2 j++

  3.7 i++

 4. Stop


CODE :

public class Main 

{

  public static void main(String[] args) 

  {

    char ch;

    int i = 1, j, s,n;

    for (i = 0; i <= 3; i++) 

    {

      for (s = 3; s > i; s--) //this loop is used to print spaces

        System.out.print(" ");

      ch = 'A';

      for (j = 0; j <= i; j++) 

      {

        System.out.print(ch); //this loop is used to print characters

        ch++;

      }

      for (n = 0; n < i; n++) //this loop is used to print numbers

        System.out.print(i);

      System.out.println(); 

    }

  }

}


Output : 

(App used for compiling: Dcoder

Dcoder is a very simple and useful mobile app which has most of the languages in it. So give this app a try if you want to learn coding.)


Observations and learning: 

In this program we have seen that in left side pattern is of increasing order of alphabets and is right side pattern is of increasing order of numbers. In this program we have taken use of nested for loop i.e for loop inside for loop. If we change any order program it may give error 

Syntax for Nested For loop: 

for ( initialization; condition; increment ) 

{

for ( initialization; condition; increment ) 

{

 // statement of inside loop 

}

 // statement of outer loop 

}


Conclusion: 

The placing of one loop inside the body of another loop is called nesting. When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops may be nested, the most commonly nested loops are for loops. 

The working process of a for loop is similar to the while loop, only the structure is different. Unlike the while loop, the layout of the control variable, loop condition, and the increment of the control statement can be stated in a single line of cod The given pattern is printed with the help of nested loop where left side pattern is of increasing order of alphabets and right side pattern is of increasing order of numbers.


Question of Curiosity : 

  1. What is the purpose of a loop?

The purpose of loops is to repeat the same, or similar, code a number 

of times. This number of times could be specified to a certain number, 

or the number of times could be dictated by a certain condition being 

met.In short Loops are used to execute a set of statements repeatedly 

until a particular condition is satisfied 

There are usually a number of different types of loops included in 

programming languages including for loops, while loops and 

do….while loops.

 

2. What types of loops does Java support? Give an example of each.

(1)While loop-

A while loop is a control flow statement that allows code to be executed repeatedly based 

on a given Boolean condition. Example:- 

class whileLoopDemo 

public static void main(String args[]) 

int x = 1; 

while (x &lt;= 4) 

System.out.println(&quot;Value of x:&quot; + x); 

x++; 

Output:- 

Value of x:1 

Value of x:2 

Value of x:3 

Value of x:4 

(2)do..while loop

do while loop is similar to while loop with only difference that it checks for condition 

after executing the statements, and therefore is an example of Exit Control Loop. 

Example:- 

class dowhileloopDemo 

public static void main(String args[]) { 

int x = 21; 

do 

System.out.println(&quot;Value of x:&quot; + x); x++; 

while (x &lt; 20); } 

Output:- 

Value of x: 21 

(3)for loop:- 

for loop provides a concise way of writing the loop structure. Unlike a while loop, a for 

statement consumes the initialization, condition and increment/decrement in one line 

thereby providing a shorter, easy to debug structure of looping. 

Example:- 

class forLoopDemo 

{

public static void main(String args[]) 

for (int x = 2; x &lt;= 4; x++) System.out.println(&quot;Value of x:&quot; + x); } 

Output:- Value of x:2 Value of x:3 Value of x:4

 

3. What is the difference between an unlabeled and a labeled continue statement?

The labelled statement can be any block statement; it does not have to be preceded by a loop statement. Without a label, break will break out of the inner loop. With a label you can stop execution of nested loops. the break is given in only inner for loop.

 

4. Which of the followings is not a nested loop?

a. for(i=0;i<10;i++)

        for(j=1;j<i+2;j++)

b. for(i=0;i<10;i++)

       System.out.println(i);

    for(j=1;j<i+2;j++) System.out.println("j="+j);

c. for(i=0;i<10;i++)

        while(j%2!=0)

                { 

           System.out.println (j+"\t"+ (++j));

                 }

 Ans: b

 

 5. What would be printed from the following Java code segment?

Ans : 

for(i=20;i>0;i-=2)

  System.out.println(i);

 20 

18 

16 

14 

12 

10 

2


Similar Programs

  • Write a program to print all Armstrong numbers between 1 to n.
  • Write a program to print Fibonacci series up to n term 
  • Write a program to print the various star patterns. 
  • Write a program to print the various number patterns.
  • Write a program to find first and last digit of any number.
  • Write a program to calculate product of digits of any number.
  • Write a program to swap first and last digits of any number.
  • Write a program to enter any number and check whether the number is palindrome or not.
  • Write a program to find frequency of each digit in a given integer.


Comments