Tuesday, 15 March 2016

Simple addition using matrix in java

Matrix addition is also possible in java. The following code explains the matrix addition.



import java.io.*;

class matadd

{

public static void main(String args[])throws IOException

{
/*declaration of variables and arrays*/


int i,n,m,j,x,y;

int a[][]=new int [10][10];

int b[][]=new int [10][10];

int c[][]=new int [10][10];

           

DataInputStream br =new DataInputStream(System.in);


/*Getting the number of rows and columns for A and B matrices*/ 


System.out.println("Enter the number of rows for A");

m=Integer.parseInt (br.readLine());



System.out.println("Enter the number of columns for A");

n=Integer.parseInt (br.readLine());



System.out.println("Enter the number of rows for B");

x=Integer.parseInt (br.readLine());



System.out.println("Enter the number of columns for B");

y=Integer.parseInt (br.readLine());


/*Checking whether the matrices are with same order */


if((m==x)&&(n==y))

{

  System.out.println("Enter the A array Elements");

  for (i=0; i<m; i++)

  {

    for (j=0;j<n;j++)

    {

      a[i][j]=Integer.parseInt(br.readLine());

    }

  }

  System.out.println("Enter the B array Elements");

  for (i=0; i<m; i++)

  {

   for (j=0;j<n;j++)

   {

     b[i][j]=Integer.parseInt(br.readLine());

   }

  }

/*addition of the two matrices*/            

    for(i=0;i<m;i++)

  {

    for(j=0;j<m;j++)

    {

      c[i][j]=a[i][j]+b[i][j];

    }

  }
/*printing of the answer in the form of a matrix*/

  System.out.println("Answer");

  for (i=0;i<m;i++)

  {

    System.out.println("\t\n");

    for (j=0;j<n;j++)

    {

  System.out.println(c[i][j]);

  System.out.println("\t");

    }

    }

}

else

{

   System.out.println("Matrix Addition can not be performed for the given rows and columns of the matrices");

}

}

}



Output :



H:\>javac matadd.java

H:\>java matadd

Enter the number of rows for A

3

Enter the number of columns for A

3

Enter the number of rows for B

3

Enter the number of columns for B

3




Enter the A array Elements                                                               
1

2

3

4

5

6

7

8

9



Enter the B array Elements

1

2

3

4

5

6

7

8

9



Answer

2

4

6



8

10

12



14

16

18




Monday, 14 March 2016

Check whether a number is present in an array - simple java coding



/*check an number present in an array or not*/

import java.io.*;
class check
{
public static void main (String args[]) throws IOException
{
DataInputStream s = new DataInputStream(System.in);
int n,i,x,flag;
flag=0;
int a[] = new int [100];
System.out.println("Enter the number of elements");

n= Integer.parseInt(s.readLine());
  System.out.println("Enter the "+n+"numbers");
  for(i=0;i<n;i++)
  {
    a[i]=Integer.parseInt(s.readLine());
  }
  System.out.println("Enter the number you want to search");
  x=Integer.parseInt(s.readLine());
  for(i=0;i<n;i++)
  {
    if(a[i]==x)
    {
     flag++;
    }
  }

  if(flag==0)
  {
    System.out.println(x+" IS NOT PRESENT");
  }
  else
  {
    System.out.println(x+ " IS PRESENT FOR " +flag+ " TIMES");
  }

}
}

Save the file with the extension . java and open the command prompt.

C:\Documents and Settings\dcom358>H:



H:\>cd java

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac check.java
Note: check.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\Java>java check
Enter the number of elements
5
Enter the 5numbers
100
200
100
300
400
Enter the number you want to search
100
100 IS PRESENT FOR 2 TIMES

H:\Java>java check
Enter the number of elements
5
Enter the 5numbers
2650
2354
5286
2547
26478
Enter the number you want to search
2355
2355 IS NOT PRESENT

Find out the given number as odd or even simple java coding

Coding for finding number of numbers as odd and even from a given array.


import java.io.*;
class oddeven
{
public static void main(String args[])throws IOException
{
DataInputStream s = new DataInputStream(System.in);
int n,i;
int odd,even;
odd=even=0;
int a[] = new int[100];
System.out.println("Enter the number of elements");
n= Integer.parseInt(s.readLine());
System.out.println("Enter the "+n+" numbers");
for(i=0;i<n;i++)
 {
  a[i]=Integer.parseInt(s.readLine());
  if(a[i]%2==0)
    {
    even++;
    }
  else
    {
        odd++;
    }
  }
System.out.println("Odd numbers are:" +odd);
System.out.println("Even numbers are:" +even);
}
}


Set the path and compile the program

C:\Documents and Settings\dcom358>H:

H:\>CD JAVA

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac oddeven.java
Note: oddeven.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\Java>java oddeven
Enter the number of elements
7
Enter the 7 numbers
12
10
13
15
17
18
25
Odd numbers are:4
Even numbers are:3

Findout the leap year in java

To find out a given year to be leap year or not.The coding in java.

import java.io.*;
class leapyear
{
public static void main(String args[])throws IOException
{
int a;
DataInputStream l = new DataInputStream(System.in);
a=Integer.parseInt(l.readLine());
if(a%4==0)
{
  if(a%100==0)
   {
    
    if(a%400==0)
     {
       System.out.println("Leap Year");
     }
     else
     {
       System.out.println("Nota Leap Year");
      }
    }
  else
   {
    System.out.println("Leap Year");
    }
 }
else
{
 System.out.println("Not a Leap Year");
}
}
}


Set the path and compile the file.

C:\Documents and Settings\dcom358>H:

H:\>path=C:\jdk1.3\bin;

H:\>javac leapyear.java
Note: leapyear.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\>java leapyear
2015
Not a Leap Year

H:\>java leapyear
2012
Leap Year

To find Greatest number in java coding.

Now lets see the coding for finding the greatest number.

import java.io.*;
class Greatest
{
public static void main(String args[])throws IOException
{
int a,b,c;
DataInputStream s = new DataInputStream(System.in);
a=Integer.parseInt(s.readLine());
b=Integer.parseInt(s.readLine());
c=Integer.parseInt(s.readLine());
if((a==b) && (a==c))
 {
    System.out.println("Three numbers are same.The Greatest number is" +a);
 }
else if(a==b && a!=c)
 {
   if(b>c)
      {
         System.out.println("The Greatest number is" +b );
      }
    else
      {
         System.out.println("The Greatest number is"+c);
      }
 }
else if(b==c && b!=a)
  {
    if(b>a)
     {
        System.out.println("The Greatest number is"+b);
      }
     else
      {
        System.out.println("The Greatest number is"+a);
       }
  }
else if(a==c && a!=b)
 {
  if(a>b)
      {
       System.out.println("The Greatest number is"+a);
      }
  else
     {
       System.out.println("The Greatest number is"+b);
     }
}
else if(a>b && a>c)
 {
   System.out.println(a+"is the Greatest Number");
 }
else if(b>c && b>a)
 {
   System.out.println(b+"is the Greatest Number");
 }
else
  {
    System.out.println(c+"is the Greatest Number");

  }
}
}

Set the path and compile the program.

H:\>path=C:\jdk1.3\bin;

H:\>javac Greatest.java
Note: Greatest.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\>java Greatest
25
25
25
Three numbers same.The greatest number is 25.

H:\>java Greatest
25
26
27
27is the Greatest Number

simple addition in java - user defined variable

Java is simple language. Here is the simple coding to adding two numbers that are user defined.

First, is to include the header file.