PROGRAMS & FILL IN THE BLANKS

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

Write a program to store the given city names in a single dimension array. Sort these names in alphabetical order.
Input : Delhi, Bangalore, Agra, Mumbai, Calcutta
Output : Agra, Bangalore, Calcutta, delhi, Mumbai
Solution:-
import java.io.*;
public class eight
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    String a[]={"Delhi","Bangalore","Agra","Mumbai","Calcutta"};
    String t=new String();
    for(int i=0;i<5;i++)
    {
        for(int j=i;j<5;j++)
        {
            if(a[i].compareTo(a[j])>0)
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    for(int i=0;i<5;i++)
    {
       System.out.print(a[i]+" , ");
    }
   
}
}
Output
Agra, Bangalore, Calcutta, delhi, Mumbai
Variable Description

Variable
Type
Purpose
a[]
String object array
Storing five city names
t
String object
For use of swaping values
i
Integer
Counter Varable
J
Integer
Counter Varable
wap to check a no. is Armstrong or not.
    Solution:-
import java.io.*;
public class fourteen
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the value of n:-");
    int n=Integer.parseInt(d.readLine());
    int m=n,s=0,r;
    while(m>0)
    {
        r=m%10;
        s=s+(r*r*r);
        m=m/10;
    }
    if(n==s)
        System.out.println("Number is Armstrong Number");
    else
        System.out.println("Number is not Armstrong Number");
    }
}
Output
Enter the value of n:-
153
Number is Armstrong Number

Variable Description
Variable
Type
Purpose
n
Integer
Storing number for checking
m
Integer
Duplicate copy of n
s
Integer
For doing the sum of digit’s cube
r
Integer
For storing the digit of number
 wap to accept the age of n employees & count the numbers of persons in the following age group
    (i) 26 – 35 (ii) 36 – 45    (iii) 46 – 55
Solution:-
import java.io.*;
public class thirteen
{
public static void main()throws IOException
{
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the value of n:-");
    int n=Integer.parseInt(d.readLine());
    int age,a=0,b=0,c=0;
    for(int i=1;i<=n;i++)
    {
        System.out.println("Enter the age of a person:-");
        age=Integer.parseInt(d.readLine());
       
        if(age>=26&&age<=35)
            a++;
        if(age>=36&&age<=45)
            b++;
        if(age>=46&&age<=55)
            c++;
    }
    System.out.println("Age Group 26- 35 :- "+a);
    System.out.println("Age Group 36- 45 :- "+b);
    System.out.println("Age Group 46- 55 :- "+c);
}
}
Output
Enter the value of n:-
6
Enter the age of a person:-
36
Enter the age of a person:-
54
Enter the age of a person:-
27
Enter the age of a person:-
52
Enter the age of a person:-
38
Enter the age of a person:-
29
Age Group 26- 35 :- 2
Age Group 36- 45 :- 2
Age Group 46- 55 :- 2
Variable Description
Variable
Type
Purpose
i
Integer
Counter Variable
age
Integer
For storing age of a person
n
Integer
Number of person
a
Integer
For counting no. of person
b
Integer
For counting no. of person
c
Integer
For counting no. of person
Write a program to display the following   
    *
    ***
    *****
    ***
    *
public class tweleb
{
public void main()
{
for(int i=1;i<=5;i=i+2)
{
    for(int j=1;j<=i;j++)
        System.out.print("*");
System.out.println();
}
for(int i=3;i>=1;i=i-2)
{
    for(int j=1;j<=i;j++)
        System.out.print("*");
System.out.println();
}
}
}
Output
    *
    ***
    *****
    ***
    *

Variable Description
Variable
Type
Purpose
i
Integer
Counter Variable
j
Integer
Counter Variable
 Write a program to display the following
    56789
    6789
    789
    89
    9
Solution:-
public class twele
{
public void main()
{
for(int i=5;i<10;i++)
{
    for(int j=i;j<10;j++)
        System.out.print(j);
System.out.println();
}
}
}
Output
56789
6789
789
89
9
 Variable Description

Variable
Type
Purpose
i
Integer
Counter Variable
j
Integer
Counter Variable
 Write a program to find sum of the series using function int fact(int)
//x – x3/3! + x5/5! – x7/7! ……………………xn/n!
Solution:-
public class seven
{
public void main(int n,int x)
{
double sum=0;
int f;
double p;
int sign=1;
for(int i=1;i<=n;i=i+2)
{
    p=Math.pow(x,i);
    f=fact(i);
    sum=sum+(p/f)*sign;
    sign=sign*-1;
}
System.out.println("Sum is :- " + sum);
}
public int fact(int a)
{
    int x=1;
    for(int i=1;i<=a;i++)
        x=x*i;
    return x;
}}
Output
n=5
x=2
Sum is :- 0.9333333333333333
Variable Description

Variable
Type
Purpose
n
Integer
Storing number of terms in series
x
Integer
Storing value of x in series
i
Integer
Counter Variable
f
Integer
Storing factorial of given integer
sum
double
Storing result
p
double
Storing power of given number
a
integer
Passing argument in function fact()
x
integer
Returning value from function fact()

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...