package assignment2;
import java.util.Scanner;
public class question1 {
public static void main(String[] args) {
int []arr=new int[5];
int sum=0,i;
Scanner in =new Scanner(System.in);
for( i=0;i<5;i++)
{
arr[i]=in.nextInt();
}
for(i=0;i<5;i++)
{
sum+=arr[i];
}
System.out.println(sum);
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question2 {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int s1=sc.nextInt();
double []arr1=new double[s1];
int i;
for(i=0;i<s1;i++)
{
arr1[i]=sc.nextDouble();
}
int s2=sc.nextInt();
double []arr2=new double[s2];
for(i=0;i<s2;i++)
{
arr2[i]=sc.nextDouble();
}
int s3;
s3=((s1>s2)?s1:s2);
double []arr3=new double[s3];
for(i=0;i<s3;i++)
{
arr3[i]=arr1[i]+arr2[i];
}
System.out.println(arr3);
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question3 {
static void func()
{
Scanner sc=new Scanner (System.in);
String naam=sc.nextLine();
System.out.println("Hello "+naam);
public static void main(String[] args) {
func();
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question4 {
static void greatest(int a,int b, int c)
{
System.out.println((a>b)?((a>c)?a:c):((b>c)?b:c));
}
public static void main(String[] args) {
greatest(100,4,2000);
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question5 {
static void reverse(int n)
{
int rev=0;
while(n!=0)
{
rev=(rev*10)+(n%10);
n=n/10;
}
System.out.println(rev);
}
public static void main(String[] args) {
reverse(345);
------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question6 {
static void isPrime()
{
int a=0,d=1;
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
while(d<n/2)
{
if(n/d==0)
a++;
d++;
}
if(a>1)
System.out.println("false");
else
System.out.println("true");
public static void main(String[] args) {
isPrime();
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question7 {
public static void main(String[] args) {
Scanner sc =new Scanner (System.in);
System.out.println("enter upperbound");
int f =sc.nextInt();
System.out.println("enter lowerbound");
int s =sc.nextInt();
for(int n=f;n<=s;n++)
{
if (isPrime(n)) {
System.out.println(n);
}
}
}
public static boolean isPrime(int num)
{
for(int i=2;i<=num/i;++i)
{
if(num%i==0) {
return false;
}
}
return true;
}
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.Scanner;
public class question8 {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
String s=sc.nextLine();
if(s.equals("Triangle"))
{
int side,height;
side=sc.nextInt();
height=sc.nextInt();
System.out.println((1/2)*side*height);
}
if(s.equals("Square"))
{
int side;
side=sc.nextInt();
System.out.println(side*side);
}
if(s.equals("Rectangle"))
{
int length,breadth;
length=sc.nextInt();
breadth=sc.nextInt();
System.out.println(length*breadth);
}
if(s.equals("Circle"))
{
int radius;
radius=sc.nextInt();
System.out.println((3.47)*radius*radius);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
package assignment2;
import java.util.*;
public class question9 {
public static void main(String[] args) {
int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
int[] arr2 = new int[] { 5, 6, 7, 8 };
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;
}
}
System.out.println(list);
}
-------------------------------------------------------------------------------------------------------------------------------------------
QUESTION 10
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
-------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Exercise10 {
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Input number of rows of matrix");
m = in.nextInt();
System.out.println("Input number of columns of matrix");
n = in.nextInt();
int array1[][] = new int[m][n];
int array2[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Input elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
array1[c][d] = in.nextInt();
System.out.println("Input the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
array2[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = array1[c][d] + array2[c][d];
System.out.println("Sum of the matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
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);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
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);
}}