1. Java Program to Find Smallest and Largest Element in an Array
import java.util.Scanner; //import Scanner class in our program
class demo
{
public static void main(String...s)
{
int i,n,large,small;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements:");
n=sc.nextInt();
int a[]=new int[n];
System.out.print("\nEnter elements of Array:");
for(i=0;i<n;++i)
a[i]=sc.nextInt();
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
System.out.print("\nSmallest Element:"+small);
System.out.println("\nLargest Element:"+large);
}
}
class demo
{
public static void main(String...s)
{
int i,n,large,small;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements:");
n=sc.nextInt();
int a[]=new int[n];
System.out.print("\nEnter elements of Array:");
for(i=0;i<n;++i)
a[i]=sc.nextInt();
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
System.out.print("\nSmallest Element:"+small);
System.out.println("\nLargest Element:"+large);
}
}
For example we have two sorted arrays a1[]={2,3,5,11} and a2[]={4,7,9} then union of a1 and a2 will be {2,3,4,5,7,9,11}. A Java program for finding union of two arrays is given below.
import java.util.Scanner; //import Scanner class in our program
class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("\nEnter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("\nEnter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("\nUnion of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
{
System.out.print(a1[i]+" ");
i++;
}
else
if(a2[j]<a1[i])
{
System.out.print(a2[j]+" ");
j++;
}
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
if(i<n1)
while(i<n1)
{
System.out.print(a1[i]+" ");
i++;
}
if(j<n2)
while(j<n2)
{
System.out.print(a2[j]+" ");
j++;
}
}
}
class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("\nEnter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("\nEnter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("\nUnion of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
{
System.out.print(a1[i]+" ");
i++;
}
else
if(a2[j]<a1[i])
{
System.out.print(a2[j]+" ");
j++;
}
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
if(i<n1)
while(i<n1)
{
System.out.print(a1[i]+" ");
i++;
}
if(j<n2)
while(j<n2)
{
System.out.print(a2[j]+" ");
j++;
}
}
}
import java.util.Scanner; //import Scanner class in our program
class demo
{
public static void main(String...s)
{
int i,j,n,m;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of rows:");
m=sc.nextInt();
System.out.print("Enter number of columns:");
n=sc.nextInt();
int a1[][]=new int[m][n];
int a2[][]=new int[m][n];
int a3[][]=new int[m][n];
System.out.print("\nEnter elements of first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a1[i][j]=sc.nextInt();
System.out.print("\nEnter elements of second matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a2[i][j]=sc.nextInt();
System.out.print("\nAddition of Matrices:\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
a3[i][j]=a1[i][j]+a2[i][j];
System.out.print(a3[i][j]+" ");
}
System.out.print("\n");
}
}
}
class demo
{
public static void main(String...s)
{
int i,j,n,m;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of rows:");
m=sc.nextInt();
System.out.print("Enter number of columns:");
n=sc.nextInt();
int a1[][]=new int[m][n];
int a2[][]=new int[m][n];
int a3[][]=new int[m][n];
System.out.print("\nEnter elements of first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a1[i][j]=sc.nextInt();
System.out.print("\nEnter elements of second matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a2[i][j]=sc.nextInt();
System.out.print("\nAddition of Matrices:\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
a3[i][j]=a1[i][j]+a2[i][j];
System.out.print(a3[i][j]+" ");
}
System.out.print("\n");
}
}
}
C++ Program to Add two matrices
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int A[10][10],B[10][10],c[10][10];
int i,j,m,n,p,q;
cout<<"Enter no. of rows and cloumns of Matrix A:";
cin>>m>>n;
cout<<"\nEnter no. of rows and columns of Matrix B:";
cin>>p>>q;
if(m==p&&n==q)
cout<<"\nMatrices can be Added";
else
{
cout<<"\nMatrices can not Added";
exit(0);
}
cout<<"\nEnter matrix A row wise:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>A[i][j];
}
cout<<"\nMatrix A:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<A[i][j]<<" ";
cout<<"\n";
}
cout<<"\nEnter Matrix B row wise:";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>B[i][j];
}
cout<<"\n\nMatrix B:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<B[i][j]<<" ";
cout<<"\n";
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=A[i][j]+B[i][j];
}
cout<<"\nSum of Matrices A and B:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<" ";
cout<<"\n";
}
getch();
}
import java.util.Scanner; //import Scanner class in our program
class demo
class demo
{
public static void main(String...s)
{
int i,j,m,n,p,q,k;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of rows and columns of first matrix:");
m=sc.nextInt();
n=sc.nextInt();
System.out.print("Enter number of rows and columns of second matrix:");
p=sc.nextInt();
q=sc.nextInt();
if(n!=p)
System.out.print("\nSorry multiplication can't be done!!");
else
{
int a1[][]=new int[m][n];
int a2[][]=new int[p][q];
int a3[][]=new int[m][q]; //resultant matrix will be of size mxq
System.out.print("\nEnter elements of first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a1[i][j]=sc.nextInt();
System.out.print("\nEnter elements of second matrix:\n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
a2[i][j]=sc.nextInt();
System.out.print("\nMultiplication of Matrices:\n");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
a3[i][j]=0;
for(k=0;k<n;++k)
a3[i][j]=a3[i][j]+(a1[i][k]*a2[k][j]);
System.out.print(a3[i][j]+" ");
}
System.out.print("\n");
}
}
}
}
5. Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +......+ 1/n
class Harmonic
{
public static void main(String...s)
{
int n,i;
float sum=0;
n=Integer.parseInt(s[0]);
for(i=1;i<=n;i++)
{
sum=sum+(float)1/i;
}
System.out.println("\nSum="+sum);
}
}
public static void main(String...s)
{
int i,j,m,n,p,q,k;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of rows and columns of first matrix:");
m=sc.nextInt();
n=sc.nextInt();
System.out.print("Enter number of rows and columns of second matrix:");
p=sc.nextInt();
q=sc.nextInt();
if(n!=p)
System.out.print("\nSorry multiplication can't be done!!");
else
{
int a1[][]=new int[m][n];
int a2[][]=new int[p][q];
int a3[][]=new int[m][q]; //resultant matrix will be of size mxq
System.out.print("\nEnter elements of first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
a1[i][j]=sc.nextInt();
System.out.print("\nEnter elements of second matrix:\n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
a2[i][j]=sc.nextInt();
System.out.print("\nMultiplication of Matrices:\n");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
a3[i][j]=0;
for(k=0;k<n;++k)
a3[i][j]=a3[i][j]+(a1[i][k]*a2[k][j]);
System.out.print(a3[i][j]+" ");
}
System.out.print("\n");
}
}
}
}
5. Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +......+ 1/n
class Harmonic
{
public static void main(String...s)
{
int n,i;
float sum=0;
n=Integer.parseInt(s[0]);
for(i=1;i<=n;i++)
{
sum=sum+(float)1/i;
}
System.out.println("\nSum="+sum);
}
}
0 comments :
Please Enter best of your Comments