You are on page 1of 17

1. Write a program which creates an integer array and displays sum of its elements.

2)Write a program which performs addition of elements which are stored in two arrays of type double.
Arrays lengths may be variable in size. The resultant values must be stored in an integer array. Display the
resultant integer array in a formatted way.
3) Write a method that receives a name as parameter and prints on the console. “Hello, <name>!” Example
4) Create a method GetMax(int a, int b, int c), that returns maximal of three numbers. Write a program that
reads three numbers from the console and prints the biggest of them.
5) Write a method that prints the digits of a given decimal number in a reversed order.
6)Write a Boolean method IsPrime(n) that check whether a given integer number n is prime.
7)Write a method that calculates all prime numbers in given range and returns them as list of integer. Write a
method to print a list of integers. Write a program that takes two integer numbers (each at a separate line)
and prints all primes in their range, separated by a comma.
8) Write a program that can calculate the area of four different geometry figures - triangle, square, rectangle and
circle.

On the first line you will get the figure type. Next you will get parameters for the chosen figure, each on a
different line:

• Triangle - side and height

• Square - side

• Rectangle - width and height


9) Write a method which accepts two integer arrays and returns an array of unique elements.

Example: Array 1 = { 10, 5, 20, 15, 25, 30}

Array 2 = {50, 12, 5, 30, 15, 70}

Result_Array = {10, 20, 25, 50, 12, 70}

Int [] uniqElements(int array1[], int array2[]);

import java.util.ArrayList;

import java.util.Arrays;
import java.util.List;

import java.util.Scanner;

public class Uniqueelements {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of the Array1:");

int n1 = sc.nextInt();

System.out.println("Enter the elements of the Array1:");

int array1[]= new int[n1];

for (int i = 0; i < n1; i++) {

array1[i] = sc.nextInt();

System.out.println("Enter the size of the Array2:");

int n2 = sc.nextInt();

System.out.println("Enter the elements of the Array1:");

int array2[]= new int[n2];

for (int j = 0; j < n2; j++) {

array2[j] = sc.nextInt();

uniqElements(array1,array2);

sc.close();

private static void uniqElements(int[] array1, int[] array2){

boolean contains = false;

List<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < array1.length; i++) {

for (int j = 0; j < array2.length; j++) {


if (array1[i] == array2[j]) {

contains = true;

break;

if(!contains){

list.add(array1[i]);

else{

contains = false;

for (int i = 0; i < array2.length; i++) {

for (int j = 0; j < array1.length; j++) {

if (array2[i] == array1[j]) {

contains = true;

break;

if(!contains){

list.add(array2[i]);

else{

contains = false;

System.out.println(list);

}
10) Analyze below given code and predict the output.

OUTPUT:

0 1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

30 31 32 33 34 35 36 37 38 39

40 41 42 43 44 45 46 47 48 49
11) Write a method which accepts two matrices of Size N X N and returns summation of
resultant Matrix.

import java.util.Scanner;
public class Summationmatrix
{
public static void main(String[] args)
{
int p, q, m, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix:");
p = sc.nextInt();
System.out.print("Enter the number of columns in the first matrix:");
q = sc.nextInt();
System.out.print("Enter the number of rows in the second matrix:");
m = sc.nextInt();
System.out.print("Enter the number of columns in the second matrix:");
n = sc.nextInt();
if (p == m && q == n)
{
int a[][] = new int[p][q];
int b[][] = new int[m][n];
int c[][] = new int[m][n];

System.out.println("Enter all the elements of first matrix:");


for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("");

System.out.println("Enter all the elements of second matrix:");


for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("");

System.out.println("First Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}

System.out.println("Second Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}

for (int i = 0; i < p; i++)


{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < q; k++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}

System.out.println("Matrix after addition:");


for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
else
{
System.out.println("Addition not possible");
System.out.println("Try Again");
}
}
}
12) Write a method public static boolean isRowMagic(int[][] a) that checks if the array is row-magic
(this means that every row has the same row sum).
13) Write a method public static boolean isMagic(int[][] a)that checks if the array is a magic square. This
means that it must be square, and that all row sums, all column sums, and the two diagonal-sums must all be
equal.

You might also like