This code is implemented and provided by Ajit Srivastava from GNIOT College, Greater Noida.
------------------------------------------------------------------
Algorithm for this code is given below:
1. Make a condition for the loop
it'll be the total no. of characters to be printed(count spaces also)
a. count the total numbers of characters in each line
b. count total no of lines
c. multiply both to find the condition for the loop
2. Find the if() condition for the next line(when we need to take println() method)
a. divide the flow controlling variable(of loop) with the total number of character in each line
b. compare it with zero
c. if the condition becomes true print a next line using println() method
3. At this stage we have the conditions for the loop and for the next line. Now we need to print the characters of the pattern
a. in these conditions if we need to use those variables which are also used in loop condition, create a copy of those variable
b. by creating copies of variables we can manipulate them and there will be no effect on main condition of loop
4. Now use 'if', 'else if'(if required) and 'else' for making our pattern.
------------------------------------------------------------------
Special Note : Please save this class inside DiamondInSingleLoop.java file in Eclipse IDE.
import java.util.Scanner;
public class DiamondInSingleLoop
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Input terms to print a Diamond: ");
// take a term from keyboard
int term = s.nextInt();
// create a copy of term
int copyOfterm = term;
// variable for printing spaces
int spaceCounter = 1;
// loop starts
for (int i = 1; i <= (2 * term - 1) * (term + 1); i++)
{
// check condition for spaces
if (spaceCounter <= copyOfterm)
{
System.out.print(" ");
spaceCounter++;
}
else
{
System.out.print("* ");
}
// check condition for next line
if (i % (term + 1) == 0)
{
System.out.println();
spaceCounter = 1;
// check condition to get upper part of the diamond
if(i < term *(term + 1))
{
copyOfterm--;
}
else
{
copyOfterm++;
}
}
} // end of the loop
} // end of main method
} // end of class
------------------------------------------------------------------
Output of this code : Input terms to print a Diamond: 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
------------------------------------------------------------------
Algorithm for this code is given below:
1. Make a condition for the loop
it'll be the total no. of characters to be printed(count spaces also)
a. count the total numbers of characters in each line
b. count total no of lines
c. multiply both to find the condition for the loop
2. Find the if() condition for the next line(when we need to take println() method)
a. divide the flow controlling variable(of loop) with the total number of character in each line
b. compare it with zero
c. if the condition becomes true print a next line using println() method
3. At this stage we have the conditions for the loop and for the next line. Now we need to print the characters of the pattern
a. in these conditions if we need to use those variables which are also used in loop condition, create a copy of those variable
b. by creating copies of variables we can manipulate them and there will be no effect on main condition of loop
4. Now use 'if', 'else if'(if required) and 'else' for making our pattern.
------------------------------------------------------------------
Special Note : Please save this class inside DiamondInSingleLoop.java file in Eclipse IDE.
import java.util.Scanner;
public class DiamondInSingleLoop
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Input terms to print a Diamond: ");
// take a term from keyboard
int term = s.nextInt();
// create a copy of term
int copyOfterm = term;
// variable for printing spaces
int spaceCounter = 1;
// loop starts
for (int i = 1; i <= (2 * term - 1) * (term + 1); i++)
{
// check condition for spaces
if (spaceCounter <= copyOfterm)
{
System.out.print(" ");
spaceCounter++;
}
else
{
System.out.print("* ");
}
// check condition for next line
if (i % (term + 1) == 0)
{
System.out.println();
spaceCounter = 1;
// check condition to get upper part of the diamond
if(i < term *(term + 1))
{
copyOfterm--;
}
else
{
copyOfterm++;
}
}
} // end of the loop
} // end of main method
} // end of class
------------------------------------------------------------------
Output of this code : Input terms to print a Diamond: 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
No comments:
Post a Comment