1.To find the average value of five numbers entered by the user
import java.util.Scanner;
class arr
{
public static void main(String [ ] args)throws Exception
{
int [ ]a;int s=0,i;
Scanner in=new Scanner(System.in);
a=new int[5];
System.out.println("Enter 5 numbers");
for(i=0 ; i<5; i++)
{
a[i]=in.nextInt();
s=s+a[i] ;
}
System.out.println("The average value is:"+s/5);
}
}
2.To find the greatest value from the list of n numbers entered by the user.
import java.util.Scanner;
class arr
{
public static void main(String [] args)throws Exception
{
int []a;int i,greatest,n;
Scanner in=new Scanner(System.in);
a=new int[10];
greatest=a[0];
System.out.println("Enter the number of terms");
n=in.nextInt();
System.out.println("Enter "+n+" numbers:");
for(i=0;i<n;i++)
{
a[i]=in.nextInt();
if(greatest<a[i])
greatest=a[i];
}
System.out.println("The greatest value is:"+greatest);
}
}
3.To find least value from a list of n numbers entered by the user
import java.util.Scanner;
class arr
{
public static void main(String [] args)throws Exception
{
int []a;
int i,least,n;
Scanner in=new Scanner(System.in);
a=new int[10];
System.out.println("Enter the number of terms");
n=in.nextInt();
System.out.println("Enter "+n+" numbers:");
for(i=0;i<n;i++)
{
a[i]=in.nextInt();
}
least=a[0];
for(i=0;i<n;i++)
{
if(least>a[i])
least=a[i];
}
System.out.println("The least value is: "+least);
}
}
4.Arranging n numbers entered by the user in the ascending order
import java.util.Scanner;
class arr
{
public static void main(String [] args)throws Exception
{
int []a;
int i,temp,n,j;
Scanner in=new Scanner(System.in);
a=new int[10];
System.out.println("Enter the number of terms");
n=in.nextInt();
System.out.println("Enter "+n+" numbers:");
for(i=0;i<n;i++)
{
a[i]=in.nextInt();
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The numbers in ascending order is:");
for(i=0;i<n;i++)
{
System.out.print(" "+a[i]);
}
}
}
5.Arranging n numbers entered by the user in the descending order
import java.util.Scanner;
class arr
{
public static void main(String [] args)throws Exception
{
int []a;
int i,temp,n,j;
Scanner in=new Scanner(System.in);
a=new int[10];
System.out.println("Enter the number of terms");
n=in.nextInt();
System.out.println("Enter "+n+" numbers:");
for(i=0;i<n;i++)
{
a[i]=in.nextInt();
}
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The numbers in descending order is:");
for(i=0;i<n;i++)
{
System.out.print(" "+a[i]);
}
}
}
6.Returning array from method
import java.util.Scanner;
class Average{
public static void main(String []args){
Average av=new Average();
Scanner bkl=new Scanner(System.in);
float sum;
sum=0;
float []a;
a=av.avg();
for(int i=0;i<a.length;i++){
sum+=a[i];
}
System.out.println("The average is:"+(sum/(a.length)));
}
public float[] avg(){
Scanner bkl=new Scanner(System.in);
System.out.println("Enter the number of terms:");
int n=bkl.nextInt();
float []b=new float[n];
System.out.println("Now enter "+n+" numbers");
for(int i=0;i<b.length;i++){
b[i]=bkl.nextFloat();
}
return b;
}
}
7.Program to find the sum and product of two matrices by passing multidimensional array to method
import java.util.Scanner;
class bkl{
public static void main(String args [])throws Exception
{
matrix data=new matrix();
data.readdata();
data.showdata();
data.sum();
data.product();
}
}
class matrix
{
int i,j,k;
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
void readdata() //1throws Exception
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=in.nextInt();
}
}
System.out.println("Enter the Second matrix\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
b[i][j]=in.nextInt();
}
}
}
void showdata()
{
System.out.println("First matrix is:");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println(" ");
}
System.out.print("\nSecond matrix is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(" "+b[i][j]);
}
System.out.println(" ");
}
}
void sum()
{
System.out.print("\n The sum of given matrices is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(" "+(a[i][j]+b[i][j]));
}
System.out.println(" ");
}
}
void product()
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
System.out.print("\n The product of given matrices is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(" "+c[i][j]);
}
System.out.println(" ");
}
}
}
Nice set of programs.Really helpful for novice...
ReplyDeleteNice set of programs.Really helpful for novice...
ReplyDelete