Wednesday, 15 April 2015

ARRAY PROGRAMS

:- Write a program to input marks of students of 6 subjects & display avg. of best four
Solution:-
import java.io.*;
public class nine
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    int sub[]=new int[6];
    for(int i=0;i<6;i++)
    {
              System.out.println("Enter value of array sub:- ");
sub[i]=Integer.parseInt(d.readLine());
    }
    int temp, tot=0;
    for(int i=0;i<6;i++)
    {  
        for(int j=0;j<6-i-1;j++)
        {
            if(sub[j]<sub[j+1])
            {
                temp=sub[j];
                sub[j]=sub[j+1];
                sub[j+1]=temp;
            }
        }
    }
    for(int i=0;i<4;i++)
    {
        tot=tot+sub[i];
    }
    System.out.println("Average of Best Four Subject := " + (tot/4));
}
}
Output
Enter Value of array sub:-
78
Enter Value of array sub:-
67
Enter Value of array sub:-
98
Enter Value of array sub:-
87
Enter Value of array sub:-
99
Enter Value of array sub:-
100
Average of Best Four Subject := 96

Variable Description
Variable
Type
Purpose
sub[]
Integer array
Storing Six subject’s marks
tot
Integer
For use of sum of best four subject
i
Integer
Counter Varable
J
Integer
Counter Varable
temp
Integer
For use of swaping values

:- Write a program to merge array a & array b in array c.
Solution:-
import java.io.*;
public class eleven
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    int a[]=new int[5];
    int b[]=new int[5];
    int c[]=new int[10];
   
    //Inputing values for array a and b
    for(int i=0;i<5;i++)
    {
        System.out.println("Enter Value of array a:-");
        a[i]=Integer.parseInt(d.readLine());
    }
    for(int i=0;i<5;i++)
    {
        System.out.println("Enter Value of array b:-");
        b[i]=Integer.parseInt(d.readLine());
    }
   
    //Merging value in array c
    int x=0;
    for(int i=0;i<5;i++)
    {
        c[x]=a[i];
        x++;
    }    
    for(int i=0;i<5;i++)
    {
        c[x]=b[i];
        x++;
    }
    System.out.println("Merged Array");
    for(int i=0;i<10;i++)
    {
       System.out.print(c[i]);
    }
}
}
Output
Enter Value of array a:-
2
Enter Value of array a:-
3
Enter Value of array a:-
4
Enter Value of array a:-
5
Enter Value of array a:-
6
Enter Value of array b:-
7
Enter Value of array b:-
3
Enter Value of array b:-
2
Enter Value of array b:-
3
Enter Value of array b:-
4
Merged Array
2345673234

Variable Description
Variable
Type
Purpose
a[]
Integer array
Storing five elements in array
b[]
Integer array
Storing five elements in array
c[]
Integer array
Storing 10 elements after merging array a and b
i
Integer
Counter Variable
j
Integer
Counter Variable
x
Integer
Counter Variable

 Write a program to find common elements of 2 array of integer

Solution:-
import java.io.*;
public class twele
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    int a[]=new int[5];
    int b[]=new int[5];
       
    //Inputing values for array a and b
    for(int i=0;i<5;i++)
    {
        System.out.println("Enter Value of array a:-");
        a[i]=Integer.parseInt(d.readLine());
    }
    for(int i=0;i<5;i++)
    {
        System.out.println("Enter Value of array b:-");
        b[i]=Integer.parseInt(d.readLine());
    }
   
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
        {
            if(a[i]==b[j])
            {
                System.out.println("Common Element is :- " + a[i]+"\t Array a : " +i+ "\t Array b : "+j);
            }
        }
    }
}
}   
Output
Enter Value of array a:-
2
Enter Value of array a:-
3
Enter Value of array a:-
4
Enter Value of array a:-
5
Enter Value of array a:-
6
Enter Value of array b:-
4
Enter Value of array b:-
3
Enter Value of array b:-
5
Enter Value of array b:-
1
Enter Value of array b:-
6
Common Element is :- 3  Array a : 1 Array b : 1
Common Element is :- 4  Array a : 2 Array b : 0
Common Element is :- 5  Array a : 3 Array b : 2
Common Element is :- 6  Array a : 4 Array b : 4

Variable Description
Variable
Type
Purpose
a[]
Integer array
Storing five elements in array
b[]
Integer array
Storing five elements in array
i
Integer
Counter Variable
j
Integer
Counter Variable


No comments:

Post a Comment