Vous êtes sur la page 1sur 55

Computer Applications

Assignment
Programs for ICSE 2020
Barnabh Choudhury N-3012

2019-20

i|Page
INDEX
Serial no. Content Page no.

1 Program to check if an input number is a prime number. 1


2 Program to calculate the sum of the digits of an integer. 2
Program to generate the first n numbers of the Fibonacci
3 3
series.
Program to convert a temperature from Celsius to
4 4
Fahrenheit or vice-versa.
Program to check whether a number is a pronic number or
5 5
not.
6 Program to check if a number is a spy number. 6
Menu driven program to:
 To find & display all the factors of a number input by
7 the user except the number itself. 8
 To find & display the factorial of a number input by
the user.
8 Program to store an array and print its contents. 10
9 Program to print the sum of the elements of an array. 12
Program to store 6 elements in Array ‘p’ and 4 in array ‘q’
10 13
and produce a third Array ‘r’ containing all the elements.
Program to input two arrays, print them in tabular format
11 16
and find the sum of each row and each column.
Program to store name, address and amount in 3 different
12 23
arrays & display it.
Program to input integers into an array of size 20 & perform
the following:
13 24
 Display the largest number of the array.
 Display the smallest number of the array.
Program to enter n integers into an array and search for a
14 25
given value within the array using the Linear Search method.
Program to enter n integers into an array and search for a
15 26
given value within the array using the Binary Search method.
16 Program to sort an array using bubble sort-method. 28
17 Program to sort an array using selection sort-method. 30
Program to add or swap the pre-defined values of x and y
18 31
with the use of call by reference.
19 Program to add two values by using call by reference. 32
ii | P a g e
Program to print different shapes with the desired length
20 34
and breadth.
Program to find the area and perimeter of a square,
21 36
rectangle, triangle or a circle using separate functions.
22 Program to generate a right angled number triangle. 40
23 Program to generate an inverted right angle number triangle. 41
Program to generate a mirrored right angled triangle made
24 41
of '*'s
Program to generate a mirrored inverted right angle triangle
25 42
made of '*'s.
26 Program to print a triangle of Stars/Hashes/Numbers. 44
27 Program to print a Diamond of Stars/Hashes/Numbers. 45
Program to print a Hollow Diamond of
28 48
Stars/Hashes/Numbers.
29 Program to generate a 'X' pattern. 50
30 Program to generate a left arrow pattern. 51

iii | P a g e
Simple Looping
Q1. Write a program to check an input integer if it is a prime number or not.

//A program to enter an integer and check if it is a prime number or not


import java.util.Scanner;//importation of Scanner class from the util package
public class P_1
{
public void main()//declaration of void main
{
Scanner s=new Scanner(System.in);
int n;int i=1;int d=0;//variable declaration
System.out.println("Enter the no. you want to check if it is Prime or not:");
n=s.nextInt();
int copy=n;//variable declared to keep a copy
while(i<=n)//while loop begins
{
if(n%i==0)//if condition
{
d++;
}
i++;
}//loop ends
if(d==2)//if-else construct begins
{
System.out.println("The no. "+copy+" is Prime");
}
else
{
System.out.println("The no. "+copy+" is Composite");
}//if-else construct ends
}//end of main()
}//end of class()

Variable Table:
S.no. Name Type Purpose
1. n int For input
2. i int To multiply different numbers with
n
3. d int To count the no. of times n got
divided
4. copy int To keep a copy of n

1|Page
Sample Output:

Q2. Write a program to find the sum of the digits of an integer

//A program to calculate the sum of the digits of an integer


import java.util.Scanner;//importation of Scanner class from the util package
public class P_2
{
public void main()
{
Scanner s=new Scanner(System.in);
int n; //variable declaration
System.out.println("Enter The No. of Which You Want to Find The Sum Of Its Digits:");
n=s.nextInt();
int z=0; //variable declaration
int d;int copy=n;
while(n>0)//while loop begins
{
d=n%10;
z=z+d;
n=n/10;
}//loop ends
System.out.print("The sum of the digits of the integer "+copy+" is: "+z);
}//end of main()
}//end of class()
Variable Table:
S.no. Name Type Purpose
1. n int For input
2. z int To add the digits
3. d int To seperate the digits
4. copy int To keep a copy of n

Sample Output:

2|Page
Q3. Write a program to generate the first n numbers of the Fibonacci series (each number is the sum of the
two preceding ones, starting from 0 and 1).

//A program to generate the first n numbers of the Fibonacci series( each number is the sum of the two
preceding ones, starting from 0 and 1)
import java.util.Scanner;//importation of Scanner class from the util package
public class P_3
{
public void main()
{
Scanner s=new Scanner(System.in);
int n; //variable declaration
System.out.println("Enter The No. Till Which You Would Want To Print The Fibonacci Series:");
n=s.nextInt();
System.out.println("Here's The Reqd. Fibonacci Series:");
int a=0; //variable declaration
int b=0;
int c=1;
while(c<=n)//while loop begins
{
System.out.println(c);
a=b;
b=c;
c=a+b;

}//loop ends
}//end of main()
}//eend of class()
Variable Table:
S.no. Name Type Purpose
1. n int For input
2. a int To store the value of b
3. b int To store the value of c
4. c int To store the value of a+b

3|Page
Sample Output:

Q4. Write a program to input a temperature in Celsius and convert it to Fahrenheit or vice-versa.

// Program to take an input temperature and convert it to Celsius or Fahrenheit.


import java.util.*;
public class P_4
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the scale:\n1.Fahrenheit\n2.Celcius ");
int n=sc.nextInt();//input of scale
switch(n)
{
case 1:System.out.println("Enter the temperature:");
double F=sc.nextInt();//input of temperature
double C=5/9*(F-32);// coversion
System.out.println(F+" is "+C+" degree celcius. ");
break;
case 2:System.out.println("Enter the temperature:");
double c=sc.nextInt();//input of temperature
double f=1.8*(c+32);//conversion
System.out.println(c+" is "+f+" degree fahrenheit. ");
break;
default:System.out.println("Invalid");
break;
}//end of switch case
}//end of main()
}//end of class()
Variable Table:

4|Page
S.no. Name Type Purpose
1. n int For input of scale
2. F double To input/output
temperature in
Fahrenheit
3. C double To input/output
temperature in Celcius.

Sample Output:

Q5. Write a program to check if an integer is pronic. Pronic number is the number which is the product of two
consecutive integers.

/**
* Program to check whether a number is a pronic number or not
* Pronic number is the number which is the product of two consecutive integers
*/
import java.util.*;
public class P_16
{
void main()//declaration of main
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int n=sc.nextInt();//input of the number
int flag=0;
for(int i=0;i<=n;i++)
{
if(i*(i+1)==n)//check for pronic properties
{
flag=1;
break;
}

5|Page
}
if (flag==1)//ouput of pronic number
System.out.println(n+" is a pronic number.");
else
System.out.println(n+" is not a pronic number.");
}//end of main()
}//end of class()
Variable Table:
S.no. Name Type Purpose
1. n int For input of number
2. flag int To act as a checkpoint
for pronic number
3. i int To count in for loop.

Sample Output:

Q6. Write a program to check if for a spy number. A number is spy when the sum of its digits is equal to the
product of its digits.

/**
* Program to check if a number is a spy number.
* A number is spy when the sum of its digits is equal to the product of its digits.
*/
import java.util.*;
public class P_6
{
Scanner sc=new Scanner(System.in);
int sum,prod=1;//declaration of instance variables
void main()//declaration of main()
{

6|Page
System.out.println("Enter the number: ");
int n=sc.nextInt();
for(int a=0;n>0;)//check for spy number
{
a=n%10;
sum+=a;
n=n/10;
prod*=a;
}
System.out.println("The sum of the digits are: "+sum+"\nThe product of it's digits are: "+prod);
if(sum==prod)
System.out.println("The entered number is a spy number.");
else
System.out.println("The entered number is not a spy number.");
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


1. n int For input of number
To contain the sum of
2. Sum int
the numbers
To contain the product
3. prod int
of the integer
For addition &
4 a Int multiplication of the
number.

Sample Output:

7|Page
Q7. Write a menu driven program to find & display all the factors of a number input by the user, except the
number itself and to find the factorial of the number.

/**
*Menu driven program to:
* 1) To find & display all the factors of a number input by the user except the number itself.
* 2) To find & display the factorial of a number input by the user
*/
import java.util.*;
public class P_7
{
void main()//declaration of main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Choose what you want to do:\n1. Factors of a number.\n2. Factorial of a
number.");
int n=sc.nextInt();//Choice of menu
System.out.println("Enter a number: ");
int num=sc.nextInt();//input of number
switch(n)
{
case 1: System.out.println("Factors are= ");
for(int i = 1; i <= num;i++)
{
if (num % i == 0) //checking for factors
{
if (i!=num)
System.out.print(i + ", ");
else if (i==num)
System.out.print(i);
}
}
break;
case 2:int f=1;
for(int i=1;i<=num;i++)//calculating the factorial
f*=i;
System.out.println("Factorial is: "+f);
break;
default: System.out.println("Invalid");
}//end of switch ()
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


8|Page
1. n int For choice of menu
2. num int To contain the number
3. i int To contain factors
4 f Int To contain factorial

Sample Output:

9|Page
Functions and Methods
Q8. Write a menu driven program to either find the pre-defined values of ‘x’ and ‘y’ or swap it.

//A program to add or swap the pre-defined values of x and y with the use of call by reference.
import java.util.Scanner;
public class P_8
{
int op,x,y,z; //instance variables
Scanner s=new Scanner(System.in); //initialization of scanner
P_14()//constructor
{
x=10; //pre-defined values
y=20;
z=0;
}
public void choice() //choice function to choose the operation
{
System.out.println("NOTE: THE VALUES OF 'x' AND 'y' ARE 10 AND 20 RESPECTIVELY");
System.out.println("Please choose the operation you want to proceed with:");
System.out.println("To add PRESS------1");
System.out.println("To swap PRESS------2");
op=s.nextInt();
}//end of choice()
public void operation()//operation function to perform the desired function
{
if(op==1)
{
object1(); //function call
}
if(op==2)
{
object2(); //function call
}
}//end of operation()
public void object1()//object1 function to call the add function
{
P_14 obj1=new P_14();
System.out.println("The added value is:");
add(obj1); //function call
System.out.println("x+y=z ="+obj1.z);
}//end of object1()
public void object2()//object2 function to call the swap function
{
P_14 obj1=new P_14();
System.out.println("The swapped values are");
swap(obj1); //function call
System.out.println("x ="+obj1.x);

10 | P a g e
System.out.println("y ="+obj1.y);
}//end of object2()
public void swap(P_14 obj2)
{
obj2.z=obj2.x; //swapping of values
obj2.x=obj2.y;
obj2.y=obj2.z;
}
public void add(P_14 obj2)
{
obj2.z=obj2.x+obj2.y; //addition of values
}

public void main()//main function to call the functions


{
choice(); //function call
operation();
}//end of main()
}//end of class()

Variable Table:
S.no. Name Type Purpose
To choose the operation, i.e., add or
1. op int
swap
To store the pre-defined value of x,
2. x int
i.e., 10
To store the pre-defined value of y,
3. y int
i.e., 20
To store the added value of x and y
4. z int or to use it as a third variable to
swap the values of x and y

Sample Output:

11 | P a g e
Q9. Write a program to add two values using call by reference.

//A program to add two values by using call by reference.


public class P_24//demo1
{
int x,y,z;
P_24()
{
x=10;
y=20;
z=0;
}

public void main()


{
P_24 object1=new P_24();
add(object1);
System.out.println("Sum of "+x+" and "+y+" is "+object1.z);
}//end of main()

public void add(P_24 object2)


{
object2.z=object2.x+object2.y;
}
}//end of class

Variable Table:
S.No. Name Type Purpose
1. x int 1st integer input.
2. y int 2nd integer input.
3. z int 3rd integer input.
Sample Output:
12 | P a g e
Q10. Write a program to print different shapes with the desired dimensions input by the user along with the
shape.
//A program to print different shapes with the desired length and breadth (size) of the user with a desired
character
import java.util.Scanner;
public class P_10
{
int sqside; //instance variables
char sqchar;
int rectlen;
int rectbred;
char rectchar;
int trilen;
char trichar;
int a;
Scanner s=new Scanner(System.in);
public void inputsq()//inputsq function to enter information about the square
{
System.out.println("Enter the length of the side of the square");
sqside=s.nextInt();
System.out.println("Enter the character to be filled with in the square");
sqchar=(s.next()).charAt(0);
}//end of inputsq()
public void inputrect()//inputrect function to enter information about the rectangle
{
System.out.println("Enter the length of the rectangle");
rectlen=s.nextInt();
System.out.println("Enter the breadth of the rectangle");
rectbred=s.nextInt();
System.out.println("Enter the character to be filled with in the rectangle");
rectchar=(s.next()).charAt(0);
}//end of inputrect()
public void inputtri()//inputtri function to enter information about the triangle
{
System.out.println("Enter the length of the triangle");
trilen=s.nextInt();
13 | P a g e
System.out.println("Enter the character to be filled with in the triangle");
trichar=(s.next()).charAt(0);
}//end of inputtri()
public void polygon (int n, char ch)//polygon function to print the square
{
System.out.println("Here's the required square---");
for(int n1=1; n1<=n; n1++)
{
for(int n2=1; n2<=n; n2++)
{
System.out.print(ch+" ");
}
System.out.println();
}
System.out.println();
}//end of polygon()
public void polygon (int x, int y, char ch)//polygon function to print the rectangle
{
System.out.println("Here's the required rectangle---");
for(int n1=1;n1<=y;n1++)
{
for(int n2=1; n2<=x; n2++)
{
System.out.print(ch+" ");
}
System.out.println();
}
System.out.println();
}//end of polygon()
public void polygon( int x)//polygon function to print the triangle
{
System.out.println("Here's the required triangle---");
for(int n1=1; n1<=x; n1++)
{
for(int n2=1; n2<=n1; n2++)
{
System.out.print(trichar+" ");
}
System.out.println();
}
}//end of polygon()
public void main()//main function to call the other functions
{
inputsq(); //function cslls
inputrect();
inputtri();
polygon(sqside, sqchar);
polygon(rectlen, rectbred, rectchar);

14 | P a g e
polygon(trilen);
}//end of main()
}//end of class()
Variable Table:
S.no. Name Type Purpose
1.
sqside int To store the length of each side of the square

2. sqchar char To choose the character to be filled with in the square

3. rectlen int To store the length of the rectangle

4. rectbred int To store the breadth of the rectangle

To choose the character to be filled with in the


5. rectchar char
rectangle

6. trilen int To store the length o the triangle

To choose the character to be filled with in the


7. trichar char
triangle
8. n int As a copy of sqside
9. ch char As a copy of sqchar, rectchar, trichar

10. x int As a copy of rectlen, trilen


11. y int As a copy of rectbred
12. n1 int For counter
13. n2 int For counter
Sample Output:

15 | P a g e
Q11. Write a program to find the area and perimeter of a square, rectangle, triangle or a circle using seperate
functions.

//A program to find the area and perimeter of a square, rectangle, triangle or a circle using seperate functions.
import java.util.Scanner;
public class P_11
{
int shape, sqside, sqperi, rectlen, rectbred, rectperi, rectarea, tribase, trihypo, triperi, trichoose, triside1;
double triarea; //declaring instance variables
int triside2;
int triside2s;
int cirrad;
double cirperi;
double cirarea;
Scanner s=new Scanner(System.in);
public void input()//input function
{
System.out.println("Choose the shape you want to find the area and perimeter of:");
System.out.println("To choose Square PRESS-------1"); //choices of shape
System.out.println("To choose Rectangle PRESS-------2");
System.out.println("To choose Triangle PRESS-------3");

16 | P a g e
System.out.println("To choose Circle PRESS-------4");
shape=s.nextInt();
}//end of input()
public void choice1()//choice1 function for square
{
if(shape==1)
{
System.out.println("Enter the length of the side (in cms)");//input of the measurements
sqside=s.nextInt();
sqperi=calcu_sqperi(sqside); //function call
sqarea=calcu_sqarea(sqside);
System.out.println("The Perimeter of the Square is: "+sqperi+" cm");
System.out.println("The Area of the Square is: "+sqarea+" cm^2");
}
}//end of choice1()
public int calcu_sqperi(int sqsidecopy)//function to find the perimeter of square
{
int sqperiret;
sqperiret=4*sqsidecopy;
return sqperiret;
}//end of calcu_sqperi()
public int calcu_sqarea(int sqsidecopy)//function to find the area of square
{
int sqarearet;
sqarearet=sqsidecopy*sqsidecopy;
return sqarearet;
}//end of calcu_sqarea()
public void choice2()//choice2 function for rectangle
{
if(shape==2)
{
System.out.println("Enter the length (in cms)"); //input of the measurements
rectlen=s.nextInt();
System.out.println("Enter the breadth (in cms)");
rectbred=s.nextInt();
rectperi=calcu_rectperi(rectlen, rectbred); //function call
rectarea=calcu_rectarea(rectlen, rectbred);
System.out.println("The Perimeter of the Rectangle is: "+rectperi+" cm");
System.out.println("The Area of the Rectangle is: "+rectarea+" cm^2");
}
}//end of choice2()
public int calcu_rectperi(int rectlencopy, int rectbredcopy) //function to find the perimeter of rectangle
{
int rectperiret;
rectperiret=2*(rectlencopy+rectbredcopy);
return rectperiret;
}//end of calcu_rectperi()
public int calcu_rectarea(int rectlencopy, int rectbredcopy) //function to find the area of rectangle

17 | P a g e
{
int rectarearet;
rectarearet=rectlencopy*rectbredcopy;
return rectarearet;
}//end of calcu_rectarea()
public void choice3()//choice3 function for triangle
{
if(shape==3)
{
System.out.println("Choose the type of Triangle:"); //input choice for the type of triangle
System.out.println("For Scalene PRESS----1");
System.out.println("For Isosceles PRESS----2");
System.out.println("For Equilateral PRESS----3");
trichoose=s.nextInt();
if(trichoose==1)//for scalene triangle
{
System.out.println("Enter the length of the 1st side (in cms)"); //input of the measurements
triside1=s.nextInt();
System.out.println("Enter the length of the 2nd side (in cms)");
triside2=s.nextInt();
System.out.println("Enter the length of the base (in cms)");
tribase=s.nextInt();
System.out.println("Enter the length of the hypotenuse (in cms)");
trihypo=s.nextInt();
triperi=calcu_triperi1(triside1, triside2); //function call
triarea=calcu_triarea(tribase, trihypo);
System.out.println("The Perimeter of the Triangle is: "+triperi+" cm");
System.out.println("The Area of the Triangle is: "+triarea+" cm^2");
}
if(trichoose==2)//for isosceles triangle
{
System.out.println("Enter the length of the base (in cms)"); //input of the measurements
tribase=s.nextInt();
System.out.println("Enter the length of the hypotenuse (in cms)");
trihypo=s.nextInt();
System.out.println("Enter the length of the other two sides (in cms)");
triside2s=s.nextInt();
triperi=calcu_triperi2(triside2s); //function call
triarea=calcu_triarea(tribase, trihypo);
System.out.println("The Perimeter of the Triangle is: "+triperi+" cm");
System.out.println("The Area of the Triangle is: "+triarea+" cm^2");
}
if(trichoose==3)//for equilateral triangle
{
System.out.println("Enter the length of the base (in cms)"); //input of the measurements
tribase=s.nextInt();
System.out.println("Enter the length of the hypotenuse (in cms)");
trihypo=s.nextInt();

18 | P a g e
triperi=calcu_triperi3(tribase); //function call
triarea=calcu_triarea(tribase, trihypo);
System.out.println("The Perimeter of the Triangle is: "+triperi+" cm");
System.out.println("The Area of the Triangle is: "+triarea+" cm^2");
}
if(trichoose>3 || trichoose<1)//to deal with user issues
{
System.out.println("SORRY!! WRONG CODE!!");
System.out.println("Please Enter the Correct Code Again");
choice3(); //function call
}
}
}//end of choice3()
public int calcu_triperi1(int triside1copy, int triside2copy) //function to find the perimeter of scalene
triangle
{
int triperiret;
triperiret=triside1+triside2+tribase;
return triperiret;
}//end of calcu_triperi1()
public int calcu_triperi2(int triside2s) //function to find the perimeter of idosceles triangle
{
int triperiret;
triperiret=(triside2s*2)+tribase;
return triperiret;
}//end of calcu_triperi2()
public int calcu_triperi3(int tribasecopy) //function to find the perimeter of equilateral triangle
{
int triperiret;
triperiret=3*tribasecopy;
return triperiret;
}//end of calcu_triperi3()
public double calcu_triarea(int tribasecopy,int trihypocopy) //function to find the area of the triangle
{
double triarearet;
triarearet=0.5*tribasecopy*trihypocopy;
return triarearet;
}//end of calcu_triarea()
public void choice4() //choice4 function for circle
{
if(shape==4)
{
System.out.println("Enter the length of the radius (in cms)"); //input of the measurements
cirrad=s.nextInt();
cirperi=calcu_cirperi(cirrad); //function call
cirarea=calcu_cirarea(cirrad);
System.out.println("The Perimeter of the Circle is: "+cirperi+" cm");
System.out.println("Which is APPROXIMATELY equal to :"+Math.rint(cirperi)+" cm");

19 | P a g e
System.out.println("The Area of the Circle is: "+cirarea+" cm^2");
System.out.println("Which is APPROXIMATELY equal to :"+Math.rint(cirarea)+" cm^2");
System.out.println();
System.out.println("NOTE: THE VALUE OF PI TAKEN INTO ACCOUNT IS 3.14");
}
public double calcu_cirperi(int cirradcopy) //function to find the perimeter of circle
{
double cirperiret;
cirperiret=2*3.14*cirradcopy;
return cirperiret;
}//end of calcu_peri()
public double calcu_cirarea(int cirradcopy) //function to find the area of circle
{
double cirarearet;
cirarearet=0.5*3.14*Math.pow(cirradcopy,2);
return cirarearet;
}//end of calcu_cirarea()
public void main()//main function to call the other functions
{
input(); //function call
choice1();
choice2();
choice3();
choice4();
wronginput();
}//end of main()
public void wronginput()//wronginput function to deal with user issues
{
if(shape>4 || shape<1)
{
System.out.println("SORRY!! WRONG CODE!!");
System.out.println("Please Enter the Correct Code Again");
main();//function call
}
}//end of wronginput()
}//end of class()
Variable Table:
S.no. Name Type Purpose
1. shape int To choose the shape
2. sqside int To enter the side of the square
3. sqperi int To store the perimeter of the square
4. sqarea int To store the area of the square
5. rectlen int To enter the length of the rectangle
6. rectbred int To enter the breadth of the rectangle
7. rectperi int To store the perimeter of the rectangle
8. rectarea int To store the area of the rectangle
20 | P a g e
9. tribase int To enter the length of the base of the triangle
10. trihypo int To enter the length of the hypotenuse of the triangle
11. triperi int To store the perimeter of the triangle
12. triarea double To store the area of the triangle
13. trichoose int To choose the type of triangle
14. triside1 int To store the length of the 1st side of the triangle
15. triside2 int To store the breadth of 2nd side of the triangle
16. triside2s int To store the length of both the sides of the triangle
17. cirrad int To enter the length of the radius of the circle
18. cirperi double To store the perimeter of the circle
19. cirarea double To store the area of the circle
20. sqsidecopy int A copy of sqside
21. sqperiret int To return the perimeter of the square
22. sqarearet int To return the area of the square
23. rectlencopy int A copy of rectlen
24. rectbredcopy int A copy of rectbred
25. rectperiret int To return the perimeter of the rectangle
26. rectarea int To return the area of the rectangle
27. triside1copy int A copy of triside1
28. triside2copy int A copy of triside2
29. triside2scopy int A copy of triside2s
30. tribasecopy int A copy of tribase
31. trihypocopy int A copy of trihypo
32. trperiret int To return the perimeter of the rectangle
33. triarearet double To return the area of the triangle
34. cirradcopy int A copy of cirrad
35. cirperiret double To return the perimeter of the circle
36. cirarearet double To return the area of the circle
Sample Output:

21 | P a g e
22 | P a g e
Arrays
Q12. Write a program to store an array and print its contents.

//A program to store an array and print its contents


import java.util.*;
public class P_12
{
public void main()
{
Scanner scan=new Scanner(System.in);
int len,i;
System.out.println("Enter the length of the array");
len=scan.nextInt();
int x[]=new int[len];
for(i=0;i<len;i++)
{
System.out.println("Enter value# "+(i+1));
x[i]=scan.nextInt();
}//end of loop
System.out.println("The array is");
for(i=0;i<len;i++)
{
System.out.println(x[i]);
}//end of loop
}//end of main()
}//end of class
Variable Table:
S.No. Name Type Purpose
1. len int To store the length.
2. i int 1st integer input.
3. x[] int To store the array.
Sample Output:

23 | P a g e
Q13. Write a program to print the sum of all the elements of an array.

//A program to print the sum of the elements of an array


import java.util.*;
public class P_13
{
public void main()
{
Scanner scan=new Scanner(System.in);
int len,i,sum=0;
System.out.println("Enter the length of the array");
len=scan.nextInt();
int x[]=new int[len];
for(i=0;i<len;i++)
{
System.out.println("Enter value# "+(i+1));
x[i]=scan.nextInt();//taking inputs
sum=sum+x[i];//calculating sum
}//end of loop
System.out.println("The array is");
for(int b:x)
System.out.println(b);//printing array
System.out.print("Sum="+sum);
}//end of main()
}//end of class
Variable Table:

S.No. Name Type Purpose


1. len int To store the length.
2. i int 1st integer input.
3. sum int To store the sum.
4. x[] int To store the array.
Sample Output:

24 | P a g e
Q14. Write program to store 6 elements in an array ‘p’ and 4 in an array ‘q’ and produce a third array
containing all the elements.

//Program to store 6 elements in Array p and 4 in array q and produce a third array r containing all elements.
import java.util.*;
class P_14
{
int p[]=new int [6];// declaring arrays
int q[]=new int[4];
Scanner sc =new Scanner (System.in);
int r[]=new int[6+4];
void main()
{
System.out.println("Input values of p: ");
for(int i=0;i<6;i++)//input of values
p[i]=sc.nextInt();
System.out.println("Input values of q: ");
for(int i=0;i<4;i++)
q[i]=sc.nextInt();
for(int i=0;i<6;i++)//calculating the values for array ‘r’
r[i]=p[i];
for(int j=0;j<4;j++)
r[j+6]=q[j];
System.out.println("The values of r are: ");
for(int i=0;i<10;i++)//displaying values
System.out.println(r[i]);
}
}
Variable Table:
S.No. Name Type Purpose

25 | P a g e
1. p[] int To store the first array
2. q[] int To store the second array
To store the third array containing the elements
3. r[] int
of the previous two arrays
4. i int For count in for loop
5. j int For count in for loop

Sample Output:

Q15. Write a program to input two arrays, print them in tabular format and find the sum of each row and each
column.

//program to input two arrays, print them in tabular format and find the sum of each row and each column
import java.util.*;
class P_12
{
void main()
{
int rows,cols,sumrow,sumcol;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
rows=sc.nextInt();//input of dimensions of array
26 | P a g e
System.out.println("Enter the number of columns: ");
cols=sc.nextInt();
int a[][]=new int[rows][cols];
System.out.println("Enter the values: ");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
a[i][j]=sc.nextInt();//input of values of array
}
}
System.out.println("The array in tabular format: ");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
System.out.print(a[i][j]+"\t");//diplaying array in tabular format
}
System.out.println();
}
rows=a.length;
cols=a[0].length;
for(int i=0;i<rows;i++)
{
sumrow=0;
for(int j=0;j<cols;j++)
{
sumrow+=a[i][j];
}
System.out.println("Sum of row"+(i+1)+": "+sumrow);//calculating sum of each row
}
for (int i=0;i<cols;i++)
{
sumcol=0;
for(int j=0;j<rows;j++)
{
sumcol+=a[j][i];
}
System.out.println("Sum of column"+(i+1)+": "+sumcol);//calculating sum of each column
}
}
}
Variable Table:
S.No. Name Type Purpose
1. i int To count in for loop

27 | P a g e
2. j int To count in for loop
3. a[][] int To contain 2D array
4. rows int To contain no. of rows in array
5. cols int To contain no. of columns in array
6. sumrow int To contain sum of a row
7. sumcol int To contain sum of a column
Sample Output:

Q16. Write a program to store name, address and amount in 3 different arrays & display it.

//Program to store name, address and amount in 3 different arrays & display it.
import java.util.*;
class P_16
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of elements you have to enter: ");
int n=sc.nextInt();//input of array length
String name[]=new String[n];
String address[]=new String[n];
int num[]=new int[n];
int sum=0;
for(int i=0;i<n;i++)//input of elements of array
{
System.out.println("Enter name: ");
name[i]=sc.next();
System.out.println("Enter address: ");
address[i]=sc.next();
28 | P a g e
System.out.println("Enter amount: ");
num[i]=sc.nextInt();
}
for(int i=0;i<n;i++)//displaying elements of array
{
System.out.println("Name is: "+name[i]);
System.out.println("Address is: "+address[i]);
System.out.println("Number is: "+num[i]);
}
}
}
Variable Table:
S.No. Name Type Purpose
1. n int To contain the number of elements to enter
2. name[] String For first array to store names
3. address[] String For second array to contain addresses
4. num[] int For third array to contain amount
5. sum int To contain the sum of the amount
6. i int to count in for loop
Sample Output:

29 | P a g e
Q17. Write a program input integers into an array of size 20 & perform the following:
1) Display the largest number of the array.
2) Display the smallest number of the array.
/**
* Program to input integers into an array of size 20 & perform the following:
* 1) Display the largest number of the array.
* 2) Display the smallest number of the array.
*/
import java.util.*;
public class P_17
{
void main()
{
int a[]=new int[20];//declaring array
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers: ");
for(int i=0;i<20;i++)//input of elements
a[i]=sc.nextInt();
int max=a[0];
int min=a[0];
for(int i=0;i<20;i++)
{
if (a[i]>max)//calculating highest values
max=a[i];
if(a[i]<min)//calculating lowest value
min=a[i];
}
System.out.println("Largest number is: "+max);//output
System.out.println("Smallest number is: "+min);
System.out.println("Sum is: "+sum);
}}
Variable Table:
S.No. Name Type Purpose
1. a[] int To contain the array
2. I int For count in for loop
3. max int To contain the maximum value
4. min int To contain the minimum value

Sample Output:

30 | P a g e
Q18. Write a program to enter n integers into an array and search for a given value within the array using the
Linear Search method.

//A program to enter n integers into an array and search for a given value within the array using the Linear
Search method.
import java.util.*;
public class P_18
{
public void main()
{
Scanner scan=new Scanner(System.in);
int len,temp,flag=0;
System.out.println("Enter the length of the array");
len=scan.nextInt();
int a[]=new int[len];
System.out.println("Enter elements of the array: ");
for(int j=0;j<len;j++)
{
a[j]=scan.nextInt();
}
System.out.println("Enter the value to be searched for");

31 | P a g e
temp=scan.nextInt();
for(int i=0;i<len;i++)
{
if(a[i]==temp)
{
flag=1;
System.out.println("Element at position: "+(i+1));
}
}
if(flag==0)
{
System.out.println("Element not present");
}}}
Variable Table:
S.No. Name Type Purpose
1. len int To store the length.
2. temp int 1stinteger input.
3. flag int To act as a checkpoint for the element searched
4. a[] int To store the array.
5. j int For count in for loop
6. i int For count in for loop
Sample Output:

Q19. Write a program to enter n integers into an array and search for a given value within the array using the
Binary Search method.

//A program to enter n integers into an array and search for a given value within the array using the Binary
Search method.
import java.util.*;
public class P_26
{
int len,ascdsc,swap; //instance variables
32 | P a g e
public void main()
{
Scanner scan=new Scanner(System.in);
int flag=0,lower=0,mid=0,upper,len;
System.out.println("Enter the length of the array");
len=scan.nextInt();//initialising length of the array
int a[]=new int[len];
for(int i=0;i<len;i++)//input of elements
{
System.out.println("Enter element at position "+(i+1));
a[i]=scan.nextInt();
}
for(int v=0; v<len; v++)//sorting begins
{
for(int b=0; b<len-1; b++)
{
if(a[b]>a[b+1])
{
swap=a[b];//swapping
a[b]=a[b+1];
a[b+1]=swap;
}}}
System.out.println("The sorted array in ascending order is---");
for(int v=0; v<len; v++)//printing the sorted array
System.out.println(a[v]);
upper=len-1;
System.out.println("Enter the value to be searched for");
int n=scan.nextInt();
while(lower<=upper)//binary search begins
{
mid=(lower+upper)/2;
if(n<a[mid])
upper=mid-1;
else if(n>a[mid])
lower=mid+1;
else
{
flag=1;
System.out.println("Element found at position: "+(mid+1));//output
break;
}}
if(flag==0)
System.out.println("Element not found");
}}
Variable Table:
S.No. Name Type Purpose
33 | P a g e
1. flag int 1st integer input.
2. lower int To store the lower limit.
3. mid int To store the mid limit.
4. upper int To store the upper limit.
5. len int To store the length.
6. a[] int To store the array.
7. i int 2nd integer input.
8. n int 3rd integer input.
Sample Output:

Q20. Write a program to sort an array using bubble sort-method.

//A program to sort an array using bubble sort-method


import java.util.Scanner;
public class P_20
{
Scanner s=new Scanner(System.in);
int len; //instance variables
int ascdsc;
int swap;
public void main()
{
System.out.println("Enter the length of the array");
len=s.nextInt();
int x[]=new int [len];
for(int a=0; a<len; a++)//to enter the data
{
34 | P a g e
System.out.println("Enter the data for element #"+(a+1));
x[a]=s.nextInt();
}
System.out.println("The un-sorted array is---");
for(int a=0; a<len; a++)//to print the un-sorted array
{
System.out.println(x[a]);
}
System.out.println("Choose the order in which you want to sort the array");//to choose the order
System.out.println("For ascending order PRESS-----1");
System.out.println("For descending order PRESS-----2");
ascdsc=s.nextInt();
if(ascdsc==1)//for ascending order
{
for(int a=0; a<len-1; a++)//sorting begins
{
for(int b=0; b<len-1; b++)
{
if(x[b]>x[b+1])
{
swap=x[b];//swapping
x[b]=x[b+1];
x[b+1]=swap;
}
}
}
System.out.println("The sorted array in ascending order is---");
for(int a=0; a<len; a++)//printing the sorted array
{
System.out.println(x[a]);
}
}
if(ascdsc==2)//for descending order
{
for(int a=0; a<len-1; a++)//sorting begins
{
for(int b=0; b<len-1; b++)
{
if(x[b]<x[b+1])
{
swap=x[b]; //swapping
x[b]=x[b+1];
x[b+1]=swap;
}
}
}
System.out.println("The sorted array in descending order is---");
for(int a=0; a<len; a++)//printing the sorted array

35 | P a g e
System.out.println(x[a]);
}
if(ascdsc!=1 && ascdsc!=2)//to take care if wrong input
{
System.out.println("SORRY!!! Wrong Code");
System.out.println("Please choose the correct code for the correct order of sorting");
main();//function call
}
}//end of main()
} //end of class()
Variable Table:
S.no. Name Type Purpose
1. len int To store the no. of elements
2. swap int To help in swapping the data in the desired order
3. a int For counter
4. b int For counter
5. x[] int An array to store the elements
6. ascdsc int To choose the order of input

Sample Output:

Q21. Write a program to sort an array using selection sort-method.

//A program to sort an array using selection sort-method


import java.util.Scanner;
public class P_21

36 | P a g e
{
Scanner s=new Scanner(System.in);
int len; //instance variables
int swap;
int small;
int big;
int p;
int ascdsc;
public void main()//main function to carry out the processes
{
System.out.println("Enter the length of the array");
len=s.nextInt();
int x[]=new int [len];
for(int a=0; a<len; a++)//to input the data
{
System.out.println("Enter the data for the element #"+(a+1));
x[a]=s.nextInt();
}
System.out.println("The un-sorted array is----");
for(int a:x)//to print the un-sorted array
System.out.println(a);
System.out.println("Choose the order in which you want to sort the array"); //to choose the order of
sorting
System.out.println("For ascendding order PRESS----1");
System.out.println("For descending order PRESS----2");
ascdsc=s.nextInt();
if(ascdsc==1)//for ascending order
{
for(int a=0; a<len; a++)//sorting beginning
{
small=x[a];
p=a;
for(int b=a+1; b<len; b++)
{
if(small>x[b])
{
small=x[b];
p=b;
}
}
swap=x[a];//swapping
x[a]=small;
x[p]=swap;
}
System.out.println("The sorted array in ascending order is-----");
for(int a=0; a<len; a++)//printing the sorted array
{
System.out.println(x[a]);

37 | P a g e
}
}
if(ascdsc==2)//for descending order
{
for(int a=0; a<len; a++)//sorting begins
{
big=x[a];
p=a;
for(int b=a+1; b<len; b++)
{
if(big<x[b])
{
big=x[b];
p=b;
}
}
swap=x[a]; //swapping
x[a]=big;
x[p]=swap;
}
System.out.println("The sorted array in descending order is-----");
for(int a=0; a<len; a++)//to print the sorted array
{
System.out.println(x[a]);
}
}
if(ascdsc!=1 && ascdsc!=2)//to take care of wrong input
{
System.out.println("SORRY!!! Wrong Code");
System.out.println("Please select the correct code for the correct order of sorting the array");
main();
}
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


1. len int To store the no. of elements
2. swap int To help in swapping the data in the desired order
3. small int To store the smallest value
4. big int To store the biggest value
5. x[] int An array to store the elements
6. ascdsc int To choose the order of input
7. p int To store the index no. of the value
8. a int For counter
38 | P a g e
9. b int For counter
Sample Output:

39 | P a g e
Nested Looping & Patterns
Q22. Write a program to generate a right angled number triangle.

//A program to generate a right angled number triangle.


import java.util.*;
public class P_22
{
public void main()
{
Scanner scan=new Scanner(System.in);
int n,i,j;
System.out.println("Enter the number of lines you want");
n=scan.nextInt();//input for number of lines
for(i=1;i<=n;i++)//generating pattern
{
for(j=1;j<=i;j++)
{
System.out.print(j);//printing pattern
}
System.out.println();//next line
}}}
Variable Table:
S.No. Name Type Purpose
1. n int To store the no. of lines.
2. i int 1st integer input.
3. j int 2nd integer input.

Sample Output:

40 | P a g e
Q23. Write a program to generate a inverted right angle number triangle

//A program to generate a inverted right angle number triangle


import java.util.*;
public class P_23
{
public void main()
{
Scanner scan=new Scanner(System.in);
int n,i,j;
System.out.println("Enter the number of lines you want");
n=scan.nextInt();
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();
}}}
Variable Table:
S.No. Name Type Purpose
1. n int To store the no. of lines.
2. i int 1st integer input.
3. j int 2nd integer input.

Sample Output:

Q24. Write a program to generate a mirrored right angled triangle made of '*'s.

//A program to generate a mirrored right angled triangle made of '*'s


import java.util.*;
public class P_24
{
public void main()
41 | P a g e
{
Scanner scan=new Scanner(System.in);
int n,i,j,k;
System.out.println("Enter the number of lines you want");
n=scan.nextInt();
for(i=1;i<=n;i++)
{
for(j=n-i;j>0;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Variable Table:
S.No. Name Type Purpose
1. n int To store the no. of lines.
2. i int 1st integer input.
3. j int 2nd integer input.
4. k int 3rd integer input.

Sample Output:

Q25. Write a to generate a mirrored inverted right angle triangle made of '*'s

//A program to generate a mirrored inverted right angle triangle made of '*'s
import java.util.*;
42 | P a g e
public class P_25
{
public void main()
{
Scanner scan=new Scanner(System.in);
int n,i,j,k;
System.out.println("Enter the number of lines you want");
n=scan.nextInt();
for(i=n;i>=1;i--)
{
for(j=i-n;j<=n;j++)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Variable Table:
S.No. Name Type Purpose
1. n int To store the no. of lines.
2. i int 1st integer input.
3. j int 2nd integer input.
4. k int 3rd integer input.

Sample Output:

43 | P a g e
Q26. Write a program to print a triangle of Stars/Hashes/Numbers in an Ascending Order
//A program to print a triangle of Stars/Hashes/Numbers in an Ascending Order with some leave of space. The
No. of Lines is to be Input By The User
import java.util.Scanner;//importation of Scanner class from the util package
public class P_26
{
public void main()
{
Scanner s=new Scanner(System.in);
int n;int n1;int l=1; //variable declaration
System.out.println("If you would like *s in the Triangle----PRESS 1"); //Options to choose from *, # or
Numbers
System.out.println("If you would like #s in the Triangle----PRESS 2");
System.out.println("If you would like Nos in the Triangle---PRESS 3");
n1=s.nextInt();
if(n1==1)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Triangle:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<i; j++)
System.out.print(" ");
for(int k=n; k>=i; k--)
System.out.print("* ");
System.out.println();//line changing
} //loop ends
}//if condition ends
if(n1==2)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Triangle:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<i; j++)
System.out.print(" ");
for(int k=n; k>=i; k--)
System.out.print("# ");
System.out.println();//line changing
} //loop ends
}//if condition ends
if(n1==3)//if condition
{ System.out.println("Enter The Number of Lines You Want In The Triangle:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<i; j++)

44 | P a g e
System.out.print(" ");
for(int k=1; k<=l; k++)
System.out.print(k+" ");
if(l<n)
l++;
System.out.println();//line changing
} //loop ends
}//if condition ends
if(n1!=1 && n1!=2 && n1!=3)//if condition to take care of Wrong Input
{
System.out.println("SORRY! Wrong Code");
}//if condition ends
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


To enter the no. of
1. n int
lines
2. n1 int To select option
3. i int For counter
4. j int For counter
Sample Output:

Q27. Write a program to print a Diamond of Stars/Hashes.

//A program to print a Diamond of Stars/Hashes with the desired number of Lines of The User
import java.util.Scanner;//importation of Scanner class from the util package
45 | P a g e
public class P_27
{
public void main()
{
Scanner s=new Scanner(System.in);
int n;int n1;int l=1;char c; //variable declaration
System.out.println("If you would like *s in the Diamond------------PRESS 1"); //Options to choose between
* or #
System.out.println("If you would like #s in the Diamond------------PRESS 2");
n1=s.nextInt();
if(n1==1)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Diamond:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<i; j++)
System.out.print(" ");
for(int k=n; k>=i; k--)
System.out.print("* ");
System.out.println();//line changing
} //loop ends
for(int i=1; i<=n; i++)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int k=n-1; k>=i; k--)
System.out.print("* ");
System.out.println();//line changing
}
}//if condition ends
if(n1==2)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Diamond:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<i; j++)
System.out.print(" ");
for(int k=n; k>=i; k--)
System.out.print("# ");
System.out.println();//line changing
46 | P a g e
} //loop ends
for(int i=1; i<=n; i++)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int k=n-1; k>=i; k--)
System.out.print("# ");
System.out.println();//line changing
}
}//if condition ends
if(n1!=1 && n1!=2)//if condition to take care of Wrong Input
System.out.println("SORRY! Wrong Code");
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


To enter the no. of
1. n int
lines
2. n1 int To select option
3. i int For counter
4. j int For counter
Sample Output:

47 | P a g e
Q28. Write a program to print a Hollow Diamond of Stars/Hashes.

//A program to print a Hollow Diamond of Stars/Hashes/Numbers with the desired number of lines of the user
import java.util.Scanner;//importation of Scanner class from the util package
public class P_28
{
public void main()
{
Scanner s=new Scanner(System.in);
int n;int n1;int l=1; //variable declaration
System.out.println("If you would like *s in the Diamond----PRESS 1"); //Options to choose between * or #
System.out.println("If you would like #s in the Diamond----PRESS 2");
n1=s.nextInt();
if(n1==1)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Diamond:");
n=s.nextInt();
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print("*");
for(int k=n; k>i; k--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.print("*");
System.out.println();//line changing
} //loop ends
for(int i=1; i<=n; i++)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print("*");
for(int k=n-1; k>=i; k--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.println();//line changing
} //loop ends
}//if condition ends
if(n1==2)//if condition
{
System.out.println("Enter The Number of Lines You Want In The Diamond:");
n=s.nextInt();
48 | P a g e
for(int i=n; i>=1; i--)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print("#");
for(int k=n; k>i; k--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.print("#");
System.out.println();//line changing
} //loop ends
for(int i=1; i<=n; i++)//nested for loop begins
{
for(int j=1; j<=i; j++)
System.out.print("#");
for(int k=n-1; k>=i; k--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.print("#");
System.out.println();//line changing
} //loop ends
}//if condition ends
if(n1!=1 && n1!=2)//if condition to take care of Wrong Input
System.out.println("SORRY! Wrong Code");
}//end of main()
}//end of class()
Variable Table:

S.no. Name Type Purpose


To enter the no. of
1. n int
lines
2. n1 int To select option
3. i int For counter
4. j int For counter
5. k int For counter
Sample Output:

49 | P a g e
Q29. Write a program to generate a pattern ‘X’ where number of lines and symbol is input by the user.

//Program to generate a 'X' pattern where number of lines and symbol is input by the user.
import java.util.*;
public class P_29
{
void main()
{
Scanner sc=new Scanner(System.in);//declaration of scanner class
System.out.println("Enter number of lines: ");//input of no. of line
int n=sc.nextInt();
System.out.print("Enter Symbol: ");//input of symbol
char c=sc.next().charAt(0);
int k=n*2-1;//calculating total no. of lines the pattern will occupy
for(int i=1;i<=k;i++)//outer loop
{
for(int j=1;j<=k;j++)//inner loop
{
if(j==i || j==k-i+1)//printing the pattern
System.out.print(c);
System.out.print(" ");
}
System.out.println();//change of line
}
}
}
Variable Table:
50 | P a g e
S.no. Name Type Purpose
1. n int To enter the no. of lines
2. c char To contain the symbol to be printed
3. k int To contain total number of lines
4. i int For count in for loop
5. j int For count in for loop
Sample Output:

Q30. Write a program to generate a left arrow pattern where the user inputs the number of lines and symbol.

//Program to generate a left arrow pattern where the user inputs the number of lines and symbol.
import java.util.*;
public class P_30
{
void main()
{
Scanner sc=new Scanner(System.in);//declaration of scanner class
System.out.println("Enter number of lines: ");//input of number of line
int n=sc.nextInt();
System.out.print("Enter Symbol : ");//input of symbol to be used in the pattern
char c = sc.next().charAt(0);
for(int i=1;i<=n;i++)//1st outer loop
{
for(int j=1;j<=n-i;j++)//loop to print the spaces
System.out.print(" ");

51 | P a g e
for(int j=i;j<=n;j++)//loop to print the top part of the patter
System.out.print(c);
System.out.println();//change in line
}
for(int i=1;i<n;i++)//2nd outer loop
{
for(int j=0;j<i;j++)//loop to print the spaces
System.out.print(" ");
for(int j=0;j<=i;j++)//loop to print the bottom part of the pattern
System.out.print(c);
System.out.println();//change in line
}
}
}
Variable Table:

S.no. Name Type Purpose


1. n int To enter the no. of lines
2. c char To contain the symbol to be printed
3. i int For count in for loop
4. j int For count in for loop
Sample Output:

52 | P a g e

Vous aimerez peut-être aussi