You are on page 1of 15

ASSIGNMENT 2

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


of its elements.

PROGRAM:

import java.io.*;
import java.util.*;
class ArrSum
{
public static void main(String []A )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n=sc.nextInt();
int a[]=new int[n];
int s=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
s+=a[i];
}
System.out.print("The sum is "+s);
}
}

OUTPUT:

Enter the size of the array:


5
12345
The sum is 15
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.

PROGRAM:
import java.io.*;
import java.util.*;
class ArrSum
{
public static void main(String []A )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the first array:");
int n=sc.nextInt();
Double a[]=new Double[n];
System.out.println("Enter the elements of the first array:");
for(int i=0;i<n;i++)
{
a[i]=sc.nextDouble();

}
System.out.print("Enter the size of the second array:");
int m=sc.nextInt();
Double b[]=new Double[m];
System.out.println("Enter the elements of the second array:");
for(int i=0;i<m;i++)
{
b[i]=sc.nextDouble();
}
int s;
if(n>m)
{
s=n;
}
else{
s=m;

}
Double[] sum=new Double[s];
for(int i=0;i<s;i++)
{
double c=i<a.length?a[i]:0;
double ch=i<b.length?b[i]:0;
sum[i]=c+ch;
}
System.out.println("The sum array is:");
for(int i=0;i<s;i++)
{
System.out.print(sum[i]+" ");
}

}
}
OUTPUT:
Enter the size of the first array:
3
Enter the elements of the first array:
10.0 20.0 30.0
Enter the size of the second array:5
Enter the elements of the second array:
20.0 30.0 50.0 70.0 80.0
The sum array is:
50.0 80.0 70.0 80.0

3.Write a method that receives a name as parameter and prints on the console.
“Hello, <name>!”
import java.io.*;
import java.util.*;
class Hello
{
static void hello(String input)
{
System.out.println("Hello,"+input+"!");
}
public static void main(String []A )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name");
String s=sc.nextLine();
hello(s);

}
}
OUTPUT:
Enter the name
Peter
Hello,Peter!

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.
PROGRAM:
import java.io.*;
import java.util.*;
class Max
{
static int GetMax(int a, int b, int c) {
int result;
if (a > b && a > c) {
return a;
} else if (b > a && b > c) {
return b;
} else {
return c;
}
}
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.println("enter three numbers");
int a = num.nextInt();
int b = num.nextInt();
int c = num.nextInt();
int f = GetMax(a, b, c);
System.out.println(f);
}
}
OUTPUT:
enter three numbers
10 20 30
30
5.Write a method that prints the digits of a given decimal number in a reversed
order.
PROGRAM:
import java.io.*;
import java.util.*;
class Reverse
{
static void rev(int n) {
int r,sum=0;
int temp=n;
while(n>0) {
r=n%10;
sum=sum*10+r;
n=n/10;
}
System.out.println("Reverse order is "+sum);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
int n=sc.nextInt();
rev(n);
}
}
OUTPUT:
Enter the number
256
Reverse order is 652
6. Write a Boolean method IsPrime(n) that check whether a given integer number
n is prime
PROGRGAM:
import java.io.*;
import java.util.*;
class Prime
{
static boolean isPrime(long n)
{
if(n<=1)
{
return false;
}
int c=0;
for(int i=2;i<(n/2);i++)
{
if(n%i==0)
{
return false;
}

}
return true;
}

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
long n=sc.nextLong();
System.out.println(isPrime(n));
}
}
OUTPUT:
Enter the number
2
True
7. Write a method that calculates all prime numbers in given range
and returns them as list of integers
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.
PROGRAM:
import java.io.*;
import java.util.*;
class PrimeNumber
{
public static void main(String[] a)
{
List<Integer> list=new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the starting and ending
indexes");
int n=sc.nextInt();
int m=sc.nextInt();
for(int i=n;i<=m;i++)
{
if(i==1 || i==0)
continue;
int c=0;

for(int k=2;k<=(i/2);k++)
{
if(i%k==0)
{
c=1;
break;
}

}
if(c==0)
{
list.add(i);
}
}
System.out.println(list.toString());
}
}
OUTPUT:
Enter the starting and ending indexes
0 10
[2, 3, 5, 7]
8. Write a program that can calculate the area of four different geometry figures -
triangle, square, rectangle and circle.
PROGRAM:
import java.io.*;
import java.util.*;
class Area
{
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the shape ");
System.out.println("Triangle Square Rectangle Cirlce");
String s=sc.nextLine();
switch(s)
{
case "Triangle":
case "triangle":
int si=sc.nextInt();
int h=sc.nextInt();
triangle(si,h);
break;
case "Square":
case "square":
int l=sc.nextInt();
square(l);
break;
case "Rectangle":
case "rectangle":
int a=sc.nextInt();
int b=sc.nextInt();
rectangle(a,b);
break;
case "Circle":
case "circle":
double r=sc.nextDouble();
circle(r);
break;
default:
System.out.println("Invalid Input");
}
}
static void circle(double r)
{
System.out.println(3.44*r*r);
}
static void rectangle(int a,int b)
{
System.out.println((double)(a*b));
}
static void square(int l)
{
System.out.println((double)(l*l));
}
static void triangle(int s,int h)
{
System.out.println((double)(0.5*s*h));
}
}

OUTPUT:
Enter the shape
Triangle Square Rectangle Cirlce
square
5
25.0
9. Write a method which accepts two integer arrays and returns an array of
unique elements.
PROGRAM:
import java.io.*;
import java.util.*;
class Array
{
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
int arr1[]=new int[6];
int arr2[]=new int[6];
System.out.println("Enter the elements of first array:");
for(int i=0;i<6;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Enter the elements of Second array:");
for(int i=0;i<6;i++)
{
arr2[i]=sc.nextInt();
}
uniqueEle(arr1,arr2);
}
static void uniqueEle(int []arr1,int []arr2)
{
boolean c=false;
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<arr1.length;i++)
{
for(int j=0;j<arr2.length;j++)
{
if(arr1[i]==arr2[j])
{
c=true;
break;
}
}
if(!c)
{
list.add(arr1[i]);
}
else
{
c=false;
}
}
for(int i=0;i<arr2.length;i++)
{
for(int j=0;j<arr1.length;j++)
{
if(arr2[i]==arr1[j])
{
c=true;
break;
}
}
if(!c)
{
list.add(arr2[i]);
}
else
{
c=false;
}
}
System.out.println(list);
}

}
OUTPUT:
Enter the elements of first array:
10 5 20 15 25 30
Enter the elements of Second array:
50 12 5 30 15 70
[10, 20, 25, 50, 12, 70]
11: Analyze below given code and predict the output.
Displays the values from 0 to 9 on one line
10 to 19 in the next and so on upto 49
Output:
0 1 2 3…… 9
10 11 12 ….. 19
. .
. .
40 41 42 ……49

12. Write a method which accepts two matrices of Size N X N and returns
summation of resultant Matrix.
PROGRAM:
import java.io.*;
import java.util.*;
class ArraySum
{
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of matrices" );
int n1=sc.nextInt();
int n2=sc.nextInt();
int a[][]=new int[n1][n2];
int b[][]=new int[n1][n2];
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
b[i][j]=sc.nextInt();
}
}
int res[][]=new int[n1][n2];

for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
res[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("The sum of the two matrices is:");
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
System.out.print(res[i][j]+" ");
}
System.out.println(" ");
}
}

}
OUTPUT:
Enter the size of matrices
2
2

10 10
20 20

10 10
20 20
The sum of the two matrices is:
20 20

40 40
13.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)
PROGRAM:
import java.util.*;
class RowMatrix
{
static boolean isRowmagic(int[][] a)
{
int i=0;
int[] t=new int[10];
for(i=0;i<a.length;i++)
{
int s=0;
for(int j=0;j<a[i].length;j++)
{
s+=a[i][j];
}
t[i]=s;
}
if(t[i-a.length]==t[i-(a.length-1)])
{
return true;
}
else
{
return false;
}

}
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[2][3];
System.out.println("Enter a matrix of size 2X3");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
boolean ans=isRowmagic(a);
System.out.println(ans);
}
}
OUTPUT:
Enter a matrix of size 2X3
123
123
True
14. 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
PROGRAM:
import java.util.*;
class Matrix
{
static boolean isMagic(int[][] a)
{
int s1=0,s2=0;
for(int i=0;i<a.length;i++)
{
s1+=a[i][i];
s2+=a[i][a.length-i-1];

}
for(int y=0;y<a.length;y++)
{
int r=0,c=0;
for(int h=0;h<a[y].length;h++)
{
r+=a[y][h];
c+=a[h][y];
}
if(r!=c||c!=s1||s1!=s2)
{
return false;
}
}
return true;
}
public static void main(String[] aa)
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
System.out.println("Enter a matrix of size 3X3");
for(int i=0;i<3
;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
boolean ans=isMagic(a);
System.out.println(ans);
}
}
OUTPUT:
Enter a matrix of size 3X3
276
951
438
true

You might also like