0% found this document useful (0 votes)
102 views47 pages

Document1 Sample Java Code

The document contains code to print patterns like numbers, letters and shapes like triangle, diamond and sandglass. It uses for loops and if-else conditions to control the printing logic. Key steps include: 1. Taking input for number of rows from user. 2. Using nested for loops - outer loop controls rows and inner loop prints characters/spaces based on row number. 3. Printing spaces to control indentation and characters like * to print shapes. 4. Code contains forward and backward loops to print both ascending and descending patterns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views47 pages

Document1 Sample Java Code

The document contains code to print patterns like numbers, letters and shapes like triangle, diamond and sandglass. It uses for loops and if-else conditions to control the printing logic. Key steps include: 1. Taking input for number of rows from user. 2. Using nested for loops - outer loop controls rows and inner loop prints characters/spaces based on row number. 3. Printing spaces to control indentation and characters like * to print shapes. 4. Code contains forward and backward loops to print both ascending and descending patterns.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question:-overloading fn

class Flower
{
final void print()
{
[Link]("Flowers are beautiful")
}}
final class Tulip
{
void Print()
{
[Link]("Tulips are beautiful")
}}
clas Roses extends Flowers
{
void Print(
{
[Link]("Roses is a beautiful flower"
}
public static void main(string args[])
{
Rose r = new Rose();
[Link]();
Tulip j == newInstance Tulip();
[Link]()
}
}

Answer :-
class Flower
{
void print()
{
[Link]("Flowers are beautiful");
}}
class Tulip
{
void print()
{
[Link]("Tulips are beautiful");
}}
public class Rose extends Flower
{
void print()
{
[Link]("Rose is a beautiful flower");
}
Public static void main(Stringargs[])
throwsClassNotFoundException,InstantiationException,IllegalAccessExc
eption
{
Rose r = new Rose();
[Link]();
Tulip j=new Tulip();
[Link]();
}}
Output:-

Question:-Reverse string
public class StringPrograms {

public static voidmain(string[] args) {

String str = "1233";


[Link](revrse(str))
}

public staic String revrse(String in) {


if (in = null)
throw new IllegalArgumentException("Null is not valid input");

StringBuilder out = new StringBuilder();


char[] chars = [Link]();

for (int = [Link] - 1; i >= 0; i--);


[Link](chars[i]);

return [Link]();
}
}
Answer:-
public class StringPrograms {

public static void main(String[] args) {

String str = "123";


[Link](reverse(str));
}

public static String reverse(String in) {


if (in == null)
throw new IllegalArgumentException("Null is not valid input");

StringBuilder out = new StringBuilder();

char[] chars = [Link]();

for (int i = [Link] - 1; i >= 0; i--)


[Link](chars[i]);

return [Link]();
}
}
Output:-

Question:-
import [Link]
public class Pattern;
{
public static voidmain(string arguments[])
{
int n, j, space = 1;
[Link]("Enter the number of rows: ");
Scanner s = new scanner([Link]);
n = [Link]();
space = n + 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
[Link](" ");
j++;
}
space--;
for (i = 1; i <= 2 +j; i++);
{
[Link]("-");
}
[Link]("come on you can.");
}
space == 1;
for (j== 1; j<==n - 1; ++j);
{
for (I== 0; i<= space; i++)
{
[Link]("n");
}
space+-;
for (i = n; i<= 2 + (n - j) - 1; i+-)
{
[Link]("*");
}
[Link]("abc");
}
[Link]("verry close");

}
}

Answer:-
import [Link];
public class Pattern
{
public static void main(String args[])
{
int n, i, j, space = 1;
[Link]("Enter the number of rows: ");
Scanner s = new Scanner([Link]);
n = [Link]();
space = n - 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
[Link](" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
[Link]("*");
}
[Link]("");
}
space = 1;
for (j = 1; j<= n - 1; j++)
{
for (i = 1; i<= space; i++)
{
[Link](" ");
}
space++;
for (i = 1; i<= 2 * (n - j) - 1; i++)
{
[Link]("*");
}
[Link]("");
}
}
}
Output:-

Question:-
import [Link];
public class Pascalstriangle
{
public sttic voidmain(String[) args)
{
Scanner sc == new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link](]


for (int i= =0i<= =rows-1 ; i++);
{
for (int i=0; j=i; j++) { [Link]("*"+ " "); } [Link](""); }
for (int i=rows-1; i>=0 i--)
{
for(int j=0; j <= i-1;++j)
{
[Link]("*"+ " ");
}
[Link]("")
}
[Link]();
}

Answer:-
import [Link];
public class Pascalstriangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++) { [Link]("*"+ " "); } [Link](""); }
for (int i=rows-1; i>=0; i--)
{
for(int j=0; j <= i-1;j++)
{
[Link]("*"+ " ");
}
[Link]("");
}
[Link]();
}
}
Output:-

Question:-
import [Link];
public class Sandglass
{
public static voidmain(string[) arguments);
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ")

int rows = [Link]();


for ( i= 0; i<= rows-1 ; i++)
{
for (int j=0 j <i; ++j++)
{
[Link](" ");
}
for (int k=i; k<=rows-1; k++) {
[Link]("*" + " "); } [Link](""); } for
(int i= rows-1; i>= 0; i--)
{
for (int j==0; j< i ;+j+)
{
[Link]("---------");
}
for (int k===i; k<=rows-1; k++);
{
[Link]("*" + " ");
}
[Link]("");
}
scclose()
}
Answer:-
import [Link];
public class Sandglass
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j <i; j++)
{
[Link](" ");
}
for (int k=i; k<=rows-1; k++) {
[Link]("*" + " "); } [Link](""); } for
(int i= rows-1; i>= 0; i--)
{
for (int j=0; j< i ;j++)
{
[Link](" ");
}
for (int k=i; k<=rows-1; k++)
{
[Link]("*" + " ");
}
[Link]("");
}
[Link]();
}
}
Output:-

Question:-
import [Link];

public class Numsmash


{public stastic void main(string[) arg)
{
for (int i = =5; i >= 0; i--)
{
int alphabet = 65;
for (j = 0; j <== i; j++)
{
[Link](int) (alphabet + j) + " ")
}
[Link]{[Link]}
}
for (int i = 0; i<= 5; ++i++);
{
int alphbet = 65;
for ( j = 0; j <= i; j++)
{
[Link]((int) (alphabet + j) + " ");
}
[Link]();
}
}
Answer:-
import [Link];

public class Numsmash


{public static void main(String[] args)
{
for (int i = 5; i >= 0; i--)
{
int alphabet = 65;
for (int j = 0; j <= i; j++)
{
[Link]((char) (alphabet + j) + " ");
}
[Link]();
}
for (int i = 0; i<= 5; i++)
{
int alphabet = 65;
for (int j = 0; j <= i; j++)
{
[Link]((char) (alphabet + j) + " ");
}
[Link]();
}
}
}

Output:-

Question:-
import [Link];
ublic class Main
{
public static void main(string()args)
{
for ()(int i = 1; i <= y; i++)
{
innt n = 4=9

for (int j = 1; j= n - i; j++) { [Link](" "); } for (int k = i; k >= 1; k)

[Link](“:);
}
for (int l = 2; l <= i; l++) { [Link](l);} [Link](); } for (int
i = 3; i >= 1; i--)
{
int n = 3;

for (int j = 0; j<= n - i; j++) { [Link](" "); } for (int k = i; k >= 1; k--)
{
[Link](k);
}
foor (int l = 2; l <= i; l++)
{
[Link](l);
}
[Link]();

Answer:-
import [Link];

public class Main


{
public static void main(String[]args)
{
for (int i = 1; i <= 4; i++)
{
int n = 4;

for (int j = 1; j<= n - i; j++) { [Link](" "); } for (int k = i; k >= 1; k--)
{
[Link](k);
}
for (int l = 2; l <= i; l++) { [Link](l); } [Link](); } for (int
i = 3; i >= 1; i--)
{
int n = 3;
for (int j = 0; j<= n - i; j++) { [Link](" "); } for (int k = i; k >= 1; k--)
{
[Link](k);
}
for (int l = 2; l <= i; l++)
{
[Link](l);
}

[Link]();
}

}
}
Output:-

Question:-
public class Main
{
public static void main(String[] args)
{
for (int i = 0; i <= 5; i++) { int alphabet = 65; for
(int j = 5; j > i; j--)
{
[Link](" ");
}
for (int k = 0; k <= i; k++)
{
[Link]((char) (alphabet + k) + " ");
}
[Link]();
}
}
}

Answer:-
public class Main
{
public static void main(String[] args)
{
for (int i = 0; i <= 5; i++) { int alphabet = 65; for
(int j = 5; j > i; j--)
{
[Link](" ");
}
for (int k = 0; k <= i; k++)
{
[Link]((char) (alphabet + k) + " ");
}
[Link]();
}
}
}
Output:-
Question:-
import [Link];

public class Main


{
public static void main(String[] args)
{
int alphabet = 65;
for (int i = 0; i<= 5; i++)
{
for (int j = 0; j <= i; j++)
{
[Link]((char) alphabet + " ");
}
alphabet++;
[Link]();
}
}
}
Answer:-
import [Link];

public class Main


{
public static void main(String[] args)
{
int alphabet = 65;
for (int i = 0; i<= 5; i++)
{
for (int j = 0; j <= i; j++)
{
[Link]((char) alphabet + " ");
}
alphabet++;
[Link]();
}
}
}
Output

Question:-
import [Link];
public class Main
{
public static void main(String[] args)
{
int alphabet = 65;
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= i; j++)
{
[Link]((char) (alphabet + j) + "
");
}
[Link]();
}
}
}
Answer:
import [Link];

public class Main


{
public static void main(String[] args)
{
int alphabet = 65;
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= i; j++)
{
[Link]((char) (alphabet + j) + "
");
}
[Link]();
}
}
}
Output

Question:-
import [Link];
public class Main
{

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();

for (int i = 1; i <= rows; i++)


{
int num;

if(i%2 == 0)
{
num = 0;

for (int j = 1; j <= rows; j++)


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}
else
{
num = 1;

for (int j = 1; j <= rows; j++)


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}

[Link]();
}

[Link]();
}
}
Answer:-
import [Link];
public class Main
{

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i = 1; i <= rows; i++)
{
int num;

if(i%2 == 0)
{
num = 0;

for (int j = 1; j <= rows; j++)


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}
else
{
num = 1;

for (int j = 1; j <= rows; j++)


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}

[Link]();
}

[Link]();
}
}
Output;

Question:
import [Link];
public class Main
{

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


int rows = [Link]();

for (int i = 1; i <= rows; i++) { for (int j = i; j >= 1; j--)


{
[Link](j+" ");
}

[Link]();
}
[Link]();
}
}
Answer
import [Link];
public class Main
{

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i = 1; i <= rows; i++) { for (int j = i; j >= 1; j--)
{
[Link](j+" ");
}

[Link]();
}
[Link]();
}
}
Output

Question:-
public class Main
{
public static void pyramidPattern(int n)
{
for (int i=0; i<n; i++) //outer loop for number of
rows(n) { for (int j=n-i; j>1; j--) //inner loop for spaces
{
[Link](" "); //print space
}
for (int j=0; j<=i; j++ ) //inner loop for number
of columns
{
[Link]("* "); //print star
}

[Link](); //ending line after each


row
}
}

public static void main(String args[]) //driver function


{
int n = 5;
pyramidPattern(n);
}
}
Answer:-
public class Main
{
public static void pyramidPattern(int n)
{
for (int i=0; i<n; i++) //outer loop for number of
rows(n) { for (int j=n-i; j>1; j--) //inner loop for spaces
{
[Link](" "); //print space
}
for (int j=0; j<=i; j++ ) //inner loop for number
of columns
{
[Link]("* "); //print star
}

[Link](); //ending line after each


row
}
}

public static void main(String args[]) //driver function


{
int n = 5;
pyramidPattern(n);
}
}
Output

Question:-

public class Main


{
public static void rightTriangle(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of
rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces
{
[Link](" "); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
[Link]("* "); // print star
}
[Link](); // ending line after each
row
}
}
public static void main(String args[])
{
int n = 5;
rightTriangle(n);
}
}
Answer
public class Main
{
public static void rightTriangle(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of
rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces
{
[Link](" "); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
[Link]("* "); // print star
}
[Link](); // ending line after each
row
}
}
public static void main(String args[])
{
int n = 5;
rightTriangle(n);
}
}
Output
Question:-
public class Main
{
public static void printStars(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of
rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces
{
[Link](" "); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
[Link]("* "); // print star
}
[Link](); // ending line after each
row
}
}
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
Answer:-
public class Main
{
public static void printStars(int n)
{
int i, j;
for(i=0; i<n; i++) //outer loop for number of
rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces
{
[Link](" "); // printing space
}
for(j=0; j<=i; j++) // inner loop for columns
{
[Link]("* "); // print star
}
[Link](); // ending line after each
row
}
}
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
Output:
Question:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


//takes input from user

int rows = [Link]();

for (int i= rows-1; i>=0 ; i--)


{
for (int j=0; j<=i; j++)
{
[Link]("*" + " ");
}
[Link]();
}
[Link]();
}
}
Answer:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");


//takes input from user

int rows = [Link]();

for (int i= rows-1; i>=0 ; i--)


{
for (int j=0; j<=i; j++)
{
[Link]("*" + " ");
}
[Link]();
}
[Link]();
}
}
Output:-

Question:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
[Link](" ");
}
for (int k=0; k<=rows-1-i; k++)
{
[Link]("*" + " ");
}
[Link]();
}
[Link]();

}
}
Answer:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
[Link](" ");
}
for (int k=0; k<=rows-1-i; k++)
{
[Link]("*" + " ");
}
[Link]();
}
[Link]();

}
}
Output:-

Question
import [Link];
public class Main
{
// Java program to print alphabet A pattern
void display(int n)
{
// Outer for loop for number of lines
for (int i = 0; i<=n; i++) {
// Inner for loop for logic execution
for (int j = 0; j<= n / 2; j++) {
// prints two column lines
if ((j == 0 || j == n / 2) && i != 0 ||
// print first line of alphabet
i == 0 && j != n / 2 ||
// prints middle line
i == n / 2)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
Edureka a = new Edureka();
[Link](7);
}
}
Answer:-
import [Link];
public class Main
{
// Java program to print alphabet A pattern
void display(int n)
{
// Outer for loop for number of lines
for (int i = 0; i<=n; i++) {
// Inner for loop for logic execution
for (int j = 0; j<= n / 2; j++) {
// prints two column lines
if ((j == 0 || j == n / 2) && i != 0 ||
// print first line of alphabet
i == 0 && j != n / 2 ||
// prints middle line
i == n / 2)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
Edureka a = new Edureka();
[Link](7);
}
}
Output:-

Question
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows:


");

int rows = [Link]();

for (int i=1; i<= rows ; i++)


{
for (int j = i; j < rows ; j++) {
[Link](" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
[Link]("*");
}
else {
[Link](" ");
}
}
[Link]("");
}
[Link]();
}
}
Answer
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows:


");

int rows = [Link]();


for (int i=1; i<= rows ; i++)
{
for (int j = i; j < rows ; j++) {
[Link](" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
[Link]("*");
}
else {
[Link](" ");
}
}
[Link]("");
}
[Link]();
}
}
Output

Question:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i=rows; i>= 1 ; i--)
{
for (int j = i; j < rows ; j++) {
[Link](" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
[Link]("*");
}
else {
[Link](" ");
}
}
[Link]("");
}
[Link]();
}
}
Answer:-
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");

int rows = [Link]();


for (int i=rows; i>= 1 ; i--)
{
for (int j = i; j < rows ; j++) {
[Link](" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
[Link]("*");
}
else {
[Link](" ");
}
}
[Link]("");
}
[Link]();
}
}
Output

You might also like