You are on page 1of 11

The Islamia University of

Bahawalpur
Rahim yar khan Campus

Assignment of JAVA

Submitted To: Sir Zahid Aslam


Submitted By: Haroon Alrasheed Abbasi

Roll no: 525

Department of Computer Sciences


=>Program115:
import java.io.*;

class Sum{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int sum=0;
int arr[]=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter value at index "+i+":");
arr[i]=Integer.parseInt(ob.readLine());
}
for(int i=0;i<5;i++)
{
sum+=arr[i];
}
System.out.println("Sum of Elements="+sum);
}
}

=>Program116:
import java.io.*;

class Deviation{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int sum=0,i;
int arr[]=new int[5];
float avg,deviation;
for(i=0;i<5;i++)
{
System.out.println("Enter value at index "+i+":");
arr[i]=Integer.parseInt(ob.readLine());
sum+=arr[i];
}
avg=sum/5.0f;
System.out.println("Average="+avg);
for(i=0;i<5;i++)
{
deviation=(i-avg)/10.0f;
System.out.println("Deviation of Element="+deviation);
}
}
}

=>Program117:
import java.io.*;

class Square{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int sum=0;
int arr[]=new int[10];
for(int i=0;i<5;i++)
{
System.out.println("Enter value at index "+i+":");
arr[i]=Integer.parseInt(ob.readLine());
//to add square of elements to arry
arr[i+5]=arr[i]*arr[i];
}
System.out.println("Element"+"\t"+"Square");
for(int i=0;i<5;i++)
{
System.out.println(arr[i]+"\t"+arr[i+5]);
}
}
}

=>Program118:
import java.io.*;

class Average{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
float sum=0,avg=0,arr[]=new float[10];
for(int i=0;i<10;i++)
{
System.out.println("Enter value at index "+i+":");
arr[i]=Float.parseFloat(ob.readLine());
sum+=arr[i];
}
avg=sum/10f;
System.out.println("average of Elements="+avg);
}
}

=>Program119:
import java.io.*;

class MinMaxTemprature{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,min,max,temp[]=new int[7];

for(i=0;i<7;i++)
{
System.out.println("Enter Temprature:");
temp[i]=Integer.parseInt(ob.readLine());
}
min=temp[0];
max=temp[0];
for(i=0;i<7;i++)
{
if(min>temp[i])
min=temp[i];
if(max<temp[i])
max=temp[i];
}
System.out.println("Minimum Temprature:"+min);
System.out.println("Maximum Temprature:"+max);
}
}

=>Program120:
import java.io.*;
import java.lang.ArrayIndexOutOfBoundsException;

class AscSorting{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,t,ar[]=new int[5];
for(i=0;i<5;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<5;i++)
for(j=0;j<4-i;j++)
if(ar[j]>ar[j+1])
{
t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
System.out.println("Sorted Array(Asending order):");
for(i=0;i<5;i++)
System.out.println(ar[i]);
}
}

=>Program121:
import java.io.*;

class DescSorting{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,t,ar[]=new int[5];
for(i=0;i<5;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<5;i++)
for(j=0;j<4-i;j++)
if(ar[j]<ar[j+1])
{
t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
System.out.println("Sorted Array(Asending order):");
for(i=0;i<5;i++)
System.out.println(ar[i]);
}
}

=>Program122:
import java.io.*;
class SmallerLarger{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,t,ar[]=new int[10];
int min,max;
for(i=0;i<10;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
min=ar[0];
max=ar[0];
for(i=0;i<10;i++)
{
if(min>ar[i])
min=ar[i];
if(max<ar[i])
max=ar[i];
}
System.out.println("Minimum Temprature:"+min);
System.out.println("Maximum Temprature:"+max);
}
}

=>Program123:
import java.io.*;

class BubbleSort{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,t,ar[]=new int[5];
for(i=0;i<5;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<5;i++)
for(j=0;j<4-i;j++)
if(ar[j]>ar[j+1])
{
t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
System.out.println("Bubble Sort(Asending order):");
for(i=0;i<5;i++)
System.out.println(ar[i]);
}
}

=>Program124:
import java.io.*;
class SelectionSort{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,ar[],t,loc,min;
ar=new int[5];
for(i=0;i<5;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<5;i++)
{
min=ar[i];
loc=i;
for(j=0+i;j<5;j++)
if(min>ar[j])
{
min=ar[j];
loc=j;
}
t=ar[i];
ar[i]=ar[loc];
ar[loc]=t;
}
System.out.println("Selection Sort(Asending order):");
for(i=0;i<5;i++)
System.out.println(ar[i]);
}
}

=>Program26:
import java.io.*;
class Histrogram{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,ar[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter Value:");
ar[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<10;i++)
{
for(j=0;j<ar[i];j++)
System.out.print("*");
System.out.println();
}

}
}

=>Program127:
import java.io.*;

class PassFail{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,marks[],p,f;
p=f=0;
marks=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter Subject Marks:");
marks[i]=Integer.parseInt(ob.readLine());
}
for(i=0;i<10;i++)
{
if(marks[i]<=33)
f=f+1;
else
p=p+1;
}
System.out.println("Pass Subjcts:"+p);
System.out.println("Fail Subjcts:"+f);
}
}

=>Program128:
import java.io.*;

class Revers{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,a[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter an number:");
a[i]=Integer.parseInt(ob.readLine());
}
System.out.println("In Reverse Order:");
for(j=9;j>=0;j--)
System.out.println(a[j]);
}
}
=>Program130:
import java.io.*;

class Matrix{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int a[][],b[][],sum[][],i,j;
a=new int[2][3];
b=new int[2][3];
sum=new int[2][3];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
System.out.println("Enter Value a:");
a[i][j]=Integer.parseInt(ob.readLine());
}
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
System.out.println("Enter Value b:");
b[i][j]=Integer.parseInt(ob.readLine());
}
System.out.println("Sum of Matrix:");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
System.out.print(sum[i][j]+"\t");
}
System.out.println();
}
}
}

=>Program132:
import java.io.*;

class Matrix{
public static void main(String args[])throws IOException
{
BufferedReader ob=new BufferedReader(new
InputStreamReader(System.in));
int i,j,a[][],n,m;
a=new int[3][5];
System.out.println("Enter value TO Multiply:");
n=Integer.parseInt(ob.readLine());
System.out.println("Enter array:");
for(i=0;i<3;i++)
for(j=0;j<5;j++)
{
System.out.println("Enter value of array Element:");
a[i][j]=Integer.parseInt(ob.readLine());
}
System.out.println("Orignal Value");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("After Multiplication");
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
m=a[i][j]*n;
System.out.print(m+"\t");
}
System.out.println();
}

}
}