Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Showing posts with label core java:array. Show all posts
Showing posts with label core java:array. Show all posts

Wednesday, August 29, 2012

CJ-20 : Matrix addition

Type the programs in NOTEPAD or DOS prompt itself, for Consol Application.
Type in NETBEANS or ECLIPSE for Window Application.
Here after , I am just typing the programs only..to know more about it , refer

Introduction to JAVA 

before doing any JAVA Programs.
***********************************************************************************************
import java.io.*;
public class matrixsum 
{
    public static void main(String args[])throws Exception
    {
        DataInputStream din=new DataInputStream(System.in);
        int twod[][]=new int[10][10];
        int twod1[][]=new int[10][10];
        int sum[][]=new int[10][10];
        int i,j,r,c;
        System.out.println("Enter the number of rows and column of the matrix");
        r=Integer.parseInt(din.readLine());
        c=Integer.parseInt(din.readLine());
        System.out.println("Enter the number of first matrix");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                twod[i][j]=Integer.parseInt(din.readLine());
            }
        }
        System.out.println("The first matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                System.out.print(twod[i][j]+" ");
            }
            System.out.println(); 
        }
        System.out.println("Enter the number of second matrix");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                twod1[i][j]=Integer.parseInt(din.readLine());
            }
        }
        System.out.println("The second matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                System.out.print(twod1[i][j]+" ");
            }
            System.out.println(); 
        }
        System.out.println("The sum of the matrix are :");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                sum[i][j]=twod[i][j]+twod1[i][j];
                System.out.print(sum[i][j]+" ");
            }
            System.out.println();
        }
    }
}
=========================================================================
Output : 

Enter the number of rows and column of the matrix
2
2
Enter the number of first matrix
2
3
4
5
The first matrix are :
2 3 
4 5 
Enter the number of second matrix
5
4
1
2
The second matrix are :
5 4 
1 2 
The sum of the matrix are :
7 7 
5 7 

Friday, August 17, 2012

CJ-19:Sorting array numbers in ascending order

Type the programs in NOTEPAD or DOS prompt itself, for Consol Application.
Type in NETBEANS or ECLIPSE for Window Application.
Here after , I am just typing the programs only..to know more about it , refer

Introduction to JAVA 

before doing any JAVA Programs.
***********************************************************************************************
import java.io.*;
public class ascending 
{
    public static void main(String args[])throws Exception
    {
        DataInputStream din=new DataInputStream(System.in);
        int t,i,j;
        int n=0;
        int result=0;
        int a[]=new int[10];
        System.out.println("Enter the limit :");
        n=Integer.parseInt(din.readLine());
        System.out.println("Enter the numbers :");
        for(i=0;i<n;i++)
        {
            a[i]=Integer.parseInt(din.readLine());
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(a[i]<a[j])
                {
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
            }
        }
        System.out.println("The numbers in ascending orer is :");
        for(i=0;i<n;i++)
            System.out.println(a[i]);   
    }   
}
=========================================================================
Output : 
Enter the limit :
3
Enter the numbers :
5
7
2
The numbers in ascending order is :
2
5
7



Wednesday, August 15, 2012

CJ-18 : Store n numbers into an Array and print it

Type the programs in NOTEPAD or DOS prompt itself, for Consol Application.
Type in NETBEANS or ECLIPSE for Window Application.
Here after , I am just typing the programs only..to know more about it , refer

Introduction to JAVA 

before doing any JAVA Programs.
***********************************************************************************************
import java.io.*;
public class array 
{
   public static void main(String args[]) 
    {
        int a[]=new int[20];
        int i,n;
        try
        {
            DataInputStream x=new DataInputStream(System.in);
            System.out.println("Enter limit value for array");
            n=Integer.parseInt(x.readLine());
            System.out.println("Enter array elements :");
            for(i=0;i<n;i++)
            {
                a[i]=Integer.parseInt(x.readLine());
            }
            System.out.println("array elements are :");
            for(i=0;i<n;i++)
            {
                System.out.println(a[i]);
            }
        }
        catch(Exception e)
        {
        }
    }      
}    
=========================================================================
Output : 

Enter limit value for array
5
Enter array elements :
1
2
3
4
5
array elements are :
1
2
3
4
5