0% found this document useful (0 votes)
285 views

Assignment 2

The document contains 9 Java programs. Program 1 asks the user to input elements into an integer array and calculates the sum of the elements. Program 2 adds the elements of two double arrays of variable length and stores the results in a new array. The remaining programs demonstrate additional Java concepts like methods, conditionals, loops, arrays, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
285 views

Assignment 2

The document contains 9 Java programs. Program 1 asks the user to input elements into an integer array and calculates the sum of the elements. Program 2 adds the elements of two double arrays of variable length and stores the results in a new array. The remaining programs demonstrate additional Java concepts like methods, conditionals, loops, arrays, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment 2

Program 1

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

import java.util.Scanner;
public class class7 {

public static void main(String[] args) {


int[] a=new int[5];
int sum=0;
for(int i=0;i<a.length;i++)
{
Scanner x=new Scanner(System.in);
a[i]=x.nextInt();
}
for(int i=0;i<a.length;i++)
{
sum+=a[i];
}
System.out.println("Sum :"+sum);
}
}
}

Program 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.

import java.util.*;

public class doublesum {


static Double[] Doublesum(Double[] a, int n1, Double[] b, int n2) {
int size;
if(n1>n2) {
size=n1;
}else {
size=n2;
}
Double[] sum = new Double[size];
for (int i = 0; i < size; i++) {
double k=i<a.length?a[i]:0;
double m=i<b.length?b[i]:0;
sum[i] = k + m;
}
return sum;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the array1 size");
int n1 = s.nextInt();
Double[] array1 = new Double[n1];
System.out.println("Enter the array1 elements");
for (int i = 0; i < n1; i++) {
array1[i] = s.nextDouble();
}
System.out.println("Enter the array2 size");
int n2 = s.nextInt();
Double[] array2 = new Double[n2];
System.out.println("Enter the array2 elements");
for (int k = 0; k < n2; k++) {
array2[k] = s.nextDouble();
}
s.close();
Double[] sum = Doublesum(array1, n1, array2, n2);
System.out.println("The array sum is " + Arrays.toString(sum));
}
}

Output:

Enter the array1 size:


3
Enter the array1 elements:
10.0 20.0 30.0
Enter the array2 size:
5
Enter the array2 elements:
20.0 30.0 50.0 70.0 80.0
The array sum is [30.0, 50.0, 80.0, 70.0, 80.0]
Program 3

Write a method that receives a name as parameter and prints on the console. “Hello, <name>!”
import java.util.Scanner;
public class class9 {

public static void main(String[] args) {


Scanner a=new Scanner(System.in);
String s=a.nextLine();
hello(s);

}
public static void hello(String s) {
System.out.println("Hello,"+s+"!");
}
}

Program 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.
import java.util.Scanner;
public class class10 {

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
System.out.println("Enter a:");
int a=s.nextInt();
System.out.println("Enter b:");
int b=s.nextInt();
System.out.println("Enter c:");
int c=s.nextInt();
max(a,b,c);
}
public static void max(int a,int b,int c) {
if(a>b && a>c)
{
System.out.println(a);
}
else if(b>a && b>c)
{
System.out.println(b);
}
else {
System.out.println(c);
}
}
}

Program 5

Write a method that prints the digits of a given decimal number in a reversed order.

import java.util.Scanner;
public class class11{
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);

}
Program 6

Write a Boolean method IsPrime(n) that check whether a given integer number n is prime.

import java.util.Scanner;

public class class12{


static boolean IsPrime(int n) {
if(n<=1)
{
return false;
}
int count = 0;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 1) {
return false;
} else {
return true;
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean result = IsPrime(n);
System.out.println(result);

}
}

Program 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.
import java.util.ArrayList;
import java.util.Scanner;

public class class13 {


public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter the range");
int n1=s.nextInt();
int n2=s.nextInt();
s.close();
System.out.println(primenum(n1,n2));
}

static ArrayList<Integer> primenum(int n1,int n2){


int count;
ArrayList<Integer> a=new ArrayList<>();
for(int i=n1;i<=n2;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count=count+1;
}
}
if(count==2)
{
a.add(i);
}
}
return a;
}
}

Program 8

Write a program that can calculate the area of four different geometry figures - triangle, square,
rectangle and circle.
import java.util.Scanner;
public class class14 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter figure type");
System.out.println("1.triangle 2.Squre 3.Rectangle 4.circle");
int n=sc.nextInt();
switch (n){
case 1:
System.out.println("enter side and height of triangle");
int s=sc.nextInt();
int h=sc.nextInt();
System.out.println("Area of triangle is "+triangle(s,h));
break;
case 2:
System.out.println("enter side of square");

int l=sc.nextInt();
System.out.println("Area of square is "+square(l));
break;

case 3:
System.out.println("enter length and breadth rectangle");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Area of Rectangle is "+Rectangle(a,b));
break;
case 4:
System.out.println("enter radius of circle");
double r=sc.nextInt();
System.out.println("Area of circle is "+circle(r));
break;
default:
System.out.println("Invalid option");
}
}
private static double circle(double r) {
return 3.44*r*r;
}
private static double Rectangle(int a, int b) {
return a*b;
}
private static double square(int l) {
return l*l;
}

private static double triangle(int s, int h) {


return 0.5*s*h;
}
}

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class class15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int arr1[]= {10,5,20,15,25,30};
int arr2[]= {50,12,5,30,15,70};
System.out.println("Elements in Array1 is: "+Arrays.toString(arr1));
System.out.println("Elements in Array2 is: "+Arrays.toString(arr2));
uniqElements(arr1,arr2);
sc.close();
}
private static void uniqElements(int[] arr1, int[] arr2){
boolean contains = 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]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr1[i]);
}
else{
contains = false;
}
}
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
contains = true;
break;
}
}
if(!contains){
list.add(arr2[i]);
}
else{
contains = false;
}
}
System.out.println(list);
}
}

Program 10

Write a method which accepts two matrices of Size N X N and returns summation of resultant
Matrix.

package lesson1;

import java.util.Scanner;

public class class14 {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter row and column of matrix");
int n1=sc.nextInt();
int n2=sc.nextInt();
int arr1[][]=new int[n1][n2];
int arr2[][]=new int[n1][n2];
int res[][]=new int[n1][n2];
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
arr1[i][j]=sc.nextInt();
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
arr2[i][j]=sc.nextInt();
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
res[i][j]=arr1[i][j]+arr2[i][j];
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
System.out.print(res[i][j]+" ");
}
System.out.println();
}
sc.close();
}
}
Program 11

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).*/

package lesson1;

import java.util.Scanner;

public class isRowMagic {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int arr1[][]=new int[a][b];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
arr1[i][j]=sc.nextInt();
}
}
System.out.println(rowMagic(arr1,a,b));
sc.close();

private static boolean rowMagic(int[][] arr1,int n1,int n2) {


int res[]=new int[n1];
int sum=0;
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
sum+=arr1[i][j];
}
res[i]=sum;
}
for(int i=0;i<n1-1;i++){
if(res[i]!=res[i+1]){
return false;
}
}
return true;
}
}
Program12

Write a method public static boolean isMagic(int[][] a) that checks if the array is a magic square.

import java.util.Arrays;
public class class18
{
public static void main(String []args)
{
int[][] a = {{4,9,2}, {3,5,7}, {8,1,6}};
final int n=a.length;
final int nSquare=n*n;
final int targetSum=n*(n*n+1)/2;
boolean[] flag= new boolean[n*n];
int row, col, sum;
System.out.println("Original Matrix is:");
for (int i = 0;i< a.length;i++)
{
System.out.println(Arrays.toString(a[i]));
}
for(row=0; row< n; row++)
{
sum=0;
System.out.print("row "+row+": ");
for (col=0; col< n; col++)
{
int value = a[row][col];
sum += value;
if (col > 0)
System.out.print(" + ");
System.out.print(value);
}
System.out.println(" = "+sum);
if (sum != targetSum)
{
System.out.println("Row sum incorrect : Not a magic Square:");
return;
}
}
sum=0;
System.out.print("diagonal: ");
for (int pos=0; pos< n; pos++)
{
row = n-1 - pos;
col = pos;
int value = a[row][col];
sum += value;
if ( pos > 0 )
System.out.print(" + ");
System.out.print(value);
}
System.out.println(" = "+sum);
if ( sum != targetSum )
{
System.out.println("Diagonal is incorrect : Not a magic Square:");
return;
}
for ( row=0; row< n; row++ )
{
for ( col=0; col< n; col++ )
{
int num = a [ row ][ col ];
if ( n < 1 || num > nSquare )
{
System.out.println("Number out of range : Not a magic Square:");
return;
}
if ( flag [ num-1 ])
{
System.out.println("Duplicate number : Not a magic Square :");
return;
}
flag[num-1] = true;
}
}
System.out.println("The Given Matrix is MagicSquare ");
}
}

You might also like