Untitled Document

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

ASSIGNMENT 2

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

import java.util.*;
import java.lang.*;
public class SumOfArray {

public static void main(String[] args) {

int [] arr = new int [] {1, 2, 3, 4, 5};

int sum = 0;

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

sum = sum + arr[i]; }

System.out.println("Sum of all the elements of an array: " + sum); } }


Output: Sum of all the elements of an array 15

2)Write a program which performs addition of elements which are stored in two arrays of type
double..

import java.io.*;
import java.lang.*;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
double[] A=new double[n];
double[] B=new double[m];
int k1=Math.min(n,m);
int k=Math.max(n,m);
double[] C=new double[k];
for(int i1=0;i1<n;i1++)
{
A[i1]=s.nextDouble();
}
for(int i2=0;i2<m;i2++)
{
B[i2]=s.nextDouble();
}
for(int i=0;i<k1;i++)
{
C[i]=A[i]+B[i];
}
if(n>m)
{
for(int j1=k1;j1<n;j1++)
C[j1]=A[j1] ;
}
else
{
for(int j2=k1;j2<m;j2++)
C[j2]=B[j2] ;
}
for(int j=0;j<k;j++)
{

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

Input:3
5
10.00 20.00 30.00
20.00 50.00 30.00 70.00 80.00
Output:
30.00 70.00 60.00 70.00 80.00

3)Write a method that receives a name as parameter and prints on the console. “Hello,
<name>!” Example

import java.io.*;
import java.util.Scanner;
import java.lang.String;
public class HelloWorld {
public static void na(String name)
{

System.out.println("Hello,"+name+"!");
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String n=s.next();
na(n);
}}

Input:Peter

Output:
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

import java.io.*;
import java.util.Scanner;
import java.lang.String;
public class HelloWorld {
public static int getMax(int n1,int n2,int n3)
{

if(n1>n2 && n1>n3)


return(n1);
else if(n2>n1 && n2>n3)
return(n2);
else if(n3>n1 && n3>n2)
return(n3);
return(0);
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n1=s.nextInt();
int n2=s.nextInt();
int n3=s.nextInt();
int n=getMax(n1,n2,n3);
System.out.println(n);
}}

Input:
1
2
3

Output:3
5)Write a method that prints the digits of a given decimal number in a reversed order.

import java.io.*;
import java.util.Scanner;
import java.lang.String;
public class HelloWorld {
public static void reverse(int n)
{

int n1=n;
int rev=0;
while(n1>0)
{
int r=n1%10;
n1=n1/10;
rev=rev*10+r ;
}
System.out.println(rev);

}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
reverse(n);
}}

Input:256

Output:652

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

import java.io.*;
import java.util.Scanner;
import java.lang.Math;
public class HelloWorld {
public static void prime(int n)
{
int c=0;
for(int i=1;i<(Math.sqrt(n));i++)
{
if((n%i)==0)
c+=1;
}
if(c==1)

System.out.println("True");
else
System.out.println("False");

}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
prime(n);
}}

Input:13

Output:True

7)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.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.Math;
public class HelloWorld {
public static int prime(int n)
{
int c=0;
int x=(int)(Math.sqrt(n));
for(int i=1;i<=x;i++)
{
if((n%i)==0)
c+=1;
}
if(c==1 && n!=1)

return(1);
else
return(0);
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n1=s.nextInt();
int n2=s.nextInt();
ArrayList<Integer> a= new ArrayList<Integer>(1000);
for(int i=n1;i<=n2;i++)
{
if(prime(i)==1)
{
a.add(i);
}
}

int n=a.size();
for(int j=0;j<(n-1);j++)
{
System.out.print(a.get(j)+",");
}
System.out.print(a.get(n-1));

}}

Input:100
200

Output:101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,
199

8)Write a program that can calculate the area of four different geometry figures - triangle,
square, rectangle and circle.

import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.String;
public class HelloWorld {

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
String s1=s.next();
if(s1.equals("triangle"))
{
int side=s.nextInt();
int he=s.nextInt();
double ar=(side*he)/2;
System.out.println(ar);
}
else if(s1.equals("square"))
{
int side=s.nextInt();
System.out.println((side*side));
}
else if(s1.equals("rectangle"))
{
int l=s.nextInt();
int b=s.nextInt();
System.out.println((l*b));

}
else if(s1.equals("circle"))
{
int ra=s.nextInt();
System.out.println((22*ra*ra)/7);
}}}

Input:triangle
3
6

Output:9.0

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

import java.util.Scanner;
import java.util.*;
import java.lang.String;
public class HelloWorld {

public static void main(String[] args) {


Scanner s=new Scanner(System.in);
int n1=s.nextInt();
int n2=s.nextInt();
int[] A=new int[n1];
int[] B=new int[n2];
boolean contains = false;
for(int i1=0;i1<n1;i1++)
{
A[i1]=s.nextInt();

}
for(int i2=0;i2<n2;i2++)
{
B[i2]=s.nextInt();
}
ArrayList<Integer> arr= new ArrayList<Integer>(10000);

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


for (int j = 0; j < B.length; j++) {
if (A[i] == B[j]) {
contains = true;
break;
}
}

else{
contains = false;
}

}
contains=false;
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < A.length; j++) {
if (A[j] == B[i]) {
contains = true;
break;
}
}

if(!contains){
arr.add(B[i]);
}
else{
contains = false;
}

}
System.out.println(arr);}}

Input:6,6
10,5,20,50,25,30
50,12,5,30,15,70

Output:

[10, 20, 25, 12, 15, 70]

10)Analyze below given code and predict the output.

0123456789
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.*;
import java.lang.*;
public class MatrixAdditionExample{
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6}};
int b[][]={{4,5,6},{7,8,9}};
int c[][]=new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}}
Input:

[1,2,3] ,[4,5,6]

[4,5,6] [7,8,9]

Output:

[5,7,9] [11,13,15]

12)

import java.io.*;

class A {

static int N = 3;

static boolean isMagicSquare(int mat[][])

int sumd1 = 0,sumd2=0;

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

sumd1 += mat[i][i];

sumd2 += mat[i][N-1-i];

}
if(sumd1!=sumd2)

return false;

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

int rowSum = 0, colSum = 0;

for (int j = 0; j < N; j++)

rowSum += mat[i][j];

colSum += mat[j][i];

if (rowSum != colSum || colSum != sumd1)

return false;

return true;

} public static void main(String[] args)

int mat[][] = {{ 2, 7, 6 },

{ 9, 5, 1 },

{ 4, 3, 8 }};

if (isMagicSquare(mat))

System.out.println("Magic Square");

else
System.out.println("Not a magic" +

" Square");

Output:

Magic Square

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy