100% found this document useful (1 vote)
1K views14 pages

Assignment 2

The document contains 11 coding problems and their solutions in Java. It covers topics like arrays, methods, loops, conditionals, matrices and more. The solutions demonstrate how to write programs to calculate array sums, add elements in two arrays, get user input, find maximum of three numbers, check if a number is prime, calculate area of shapes, find unique elements between two arrays, check for row/matrix magic properties and more.
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
100% found this document useful (1 vote)
1K views14 pages

Assignment 2

The document contains 11 coding problems and their solutions in Java. It covers topics like arrays, methods, loops, conditionals, matrices and more. The solutions demonstrate how to write programs to calculate array sums, add elements in two arrays, get user input, find maximum of three numbers, check if a number is prime, calculate area of shapes, find unique elements between two arrays, check for row/matrix magic properties and more.
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
You are on page 1/ 14

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

elements.
SOLUTION:

public class SUMMOFARRAY {

public static void main(String[] args) {


int[] ar= new int[5];
for (int o=0;o<ar.length;o++)

{
ar[o]=o;
System.out.println(ar[o]);
}

int sum=0;
for (int k=0;k<ar.length;k++)
{

sum=sum+ar[k];
}
System.out.println("Sum of array is "+sum);

2) Write a program which performs addition of elements which are stored in


two arrays of type double.
SOLUTION:
public class ADDITIONOFARRAY {

public static void main(String[] args) {


double[] dinputArray1= new double[10];

dinputArray1[0]=10.0;
dinputArray1[1]=20.0;
dinputArray1[2]=30.0;
double[] dinputArray2= new double[10];

dinputArray2[0]=20.0;
dinputArray2[1]=50.0;
dinputArray2[2]=30.0;
dinputArray2[3]=70.0;

dinputArray2[4]=80.0;
int[] iSumArray= new int[10];
for(int i=0;i<iSumArray.length; i++)
{
iSumArray[i]=(int) (dinputArray1[i]+dinputArray2[i]);

if(iSumArray[i]!=0) {
System.out.println(iSumArray[i]);
}
}

}
3) Write a method that receives a name as parameter and prints on the console.
“Hello, <name>!” Example
SOLUTION:
import java.util.Scanner;

public class USERINPUTNAME {


static void Printname(String nm)
{
System.out.println("Hello, " +nm +"!");
}

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);
System.out.println("Enter a name");
String name=obj.nextLine();

Printname(name);
}

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.
SOLUTION:
import java.util.Scanner;
public class ReversingNumber {
static void reverse(int nmbr)
{int rev=0,rem,i=1;
while(nmbr!=0)
{ rem=nmbr%10;
rev=rev*10+rem;
nmbr=nmbr/10;

}
System.out.println(rev);
}

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a number");
int num=obj.nextInt();
reverse(num);

5) Write a Boolean method IsPrime(n) that check whether a given integer


number n is prime.
SOLUTION:
import java.util.Scanner;
public class Prime {

static boolean isPrime(int num)


{ int flag=0;
if(num<2 && num>0)
{ return false;
}
else {

for(int i=2;i<=(num/2);i++)
{
if(num%i==0)

{
flag=flag+1;
break;
}

}
if(flag==1)
{
return false;
}

else
{
return true;
}
}
}

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);


System.out.println("Enter a number");
int nm=obj.nextInt();
System.out.println(isPrime(nm));

6) Write a program that can calculate the area of four different geometry figures
- triangle, square, rectangle and circle.
SOLUTION:
import java.util.Scanner;

public class Area {

public static void main(String[] args) {


Scanner obj=new Scanner(System.in);

System.out.println("Enter a type");
String Type=obj.nextLine();
switch(Type)
{

case "Triangle":
System.out.println("Enter a base");
int base=obj.nextInt();
System.out.println("Enter a height");
int height=obj.nextInt();

double ans=((base*height)/2);
System.out.printf("%.2f",ans);
break;
case "Square":

System.out.println("Enter a side");
int side=obj.nextInt();
double ans1=side*side;
System.out.printf("%.2f",ans1);

break;
case "Rectangle":
System.out.println("Enter width");
int width=obj.nextInt();

System.out.println("Enter a height");
int height1=obj.nextInt();
double ans2=width*height1;
System.out.printf("%.2f",ans2);

break;
case "Circle":
System.out.println("Enter a radius");
int radius=obj.nextInt();
double ans3=2*Math.PI*radius;

System.out.printf("%.2f",ans3);
break;
default:
System.out.println("Please enter the valid type");

}
7) Write a method which accepts two integer arrays and returns an array of
unique elements.
SOLUTION:
import java.util.Arrays;

public class UNIQUEELEMENTS {

public static int[] uniqElements(int[] Array1,int[] Array2)


{ int[] MergeArray= new int[Array1.length+Array2.length];
int i,m=0,u=0,y=0;

int[] temparray= new int[100];


//Merging 2 arrays into 1
for(i=0;i<Array1.length;i++)
{

MergeArray[i]=Array1[i];
}
for(int k=i;k<MergeArray.length;k++)
{

MergeArray[k]=Array2[m];
m=m+1;
}

System.out.println("MERGED ARRAY");

for(int p=0;p<MergeArray.length;p++)
{
System.out.println(MergeArray[p]);
}

//Sorting array
Arrays.sort(MergeArray);
//Removing Duplicates
for( u=0;u<(MergeArray.length-1);u++)

{
if(MergeArray[u]!=MergeArray[u+1])
{
temparray[y++]=MergeArray[u];

//y=y+1;
}
}
temparray[y++]=MergeArray[(MergeArray.length-1)];

return temparray;
}

public static void main(String[] args)


{

int[] Array1= {10,5,20,15,25,30};


int[] Array2= {50,12,5,30,15,70};
int [] Resultarray=uniqElements(Array1,Array2);
System.out.println("RESULT ARRAY");
for(int b=0;b<Resultarray.length;b++)
{ if(Resultarray[b]!=0)

System.out.println(Resultarray[b]+"");
}
}
}

8)Predict the output


SOLUTION:
Displays the value from 0 to 9 in one line

10 to 19 in next line so on upto 49


Output:
0 1 2..............9
10 11 12.........19

.
.
40 41.................49

9) Write a method which accepts two matrices of Size N X N and returns


summation of resultant Matrix.
SOLUTION:

public class Summatrix {


static void matrix(int[][]A,int[][]B)
{
int[][] C= {{0,0,0},{0,0,0}};

for(int i=0;i<A.length;i++)
{
for(int j=0;j<A[i].length;j++)
{
C[i][j]=A[i][j]+B[i][j];

System.out.println(C[i][j]);
}
}

public static void main(String[] args) {


int[][] A= {{1,2,3},{4,5,6}};

int[][] B= {{4,5,6},{7,8,9}};
matrix(A,B);
}
}

10) 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).
SOLUTION:

public class ROEWMATRIX {


public static boolean isRowmagic(int[][] A)

{ int i=0;
int[] temp=new int[10];

for( i=0;i<A.length;i++)
{int sum=0;
for(int j=0;j<A[i].length;j++)
{

sum=sum+A[i][j];
}
temp[i]=sum;
}

if(temp[i-A.length]==temp[i-(A.length-1)])
{

return true;
}
else
{
return false;

}
}

public static void main(String[] args) {


int[][] A= {{1,2,3},{1,2,3}};
boolean ans= isRowmagic(A);
System.out.println(ans);

}
}

11) 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
SOLUTION:

public class magicsquaree {


public static boolean isMagic(int [][] a)
{

int sumd1=0, sumd2=0;


for(int j=0;j<a.length;j++)
{
sumd1+=a[j][j];

sumd2+=a[j][a.length-j-1];

for(int y=0;y<a.length;y++)
{
int rowsum=0,colsum=0;
for(int h=0;h<a[y].length;h++)

{
rowsum+=a[y][h];
colsum+=a[h][y];
}

if(rowsum!=colsum || colsum!=sumd1 ||sumd1!=sumd2)


{
return false;

}
}
return true;

public static void main(String[] args) {


int[][] mat= {{2,7,6},{9,5,1},{4,3,8}};

boolean ans=isMagic(mat);
System.out.println(ans);

You might also like