Exp No.
: 1
Programs using classes and objects in Java
1. Write a java program to find the largest of three numbers.
AIM:
To write a JAVA program to find the largest of three numbers.
ALGORITHM:
Step 1: Start.
Step 2: Get 3 numbers from the user, namely,
Step 3: If is greater than print as the largest.
Step 5: Else, print as the largest.
Step 6: End.
PROGRAM:
import [Link];
public class largestOf3
{
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
[Link]("Enter the 3 nos. : ");
int a=[Link]();
int b=[Link]();
int c=[Link]();
if(a>b&&a>c)
[Link]("%d is greatest\n",a);
else if(b>a&&b>c)
[Link]("%d is greatest\n",b);
else
[Link]("%d is greatest\n",c);
[Link]();
}
}
OUTPUT:
RESULT:
The java program to find the largest number of 3 numbers was implemented and
executed successfully.
2. Write a java program to print the nth value in the Fibonacci sequence.
AIM:
To write a JAVA program to print the nth value in the Fibonacci sequence.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the position of the element in the sequence from the user, namely, n.
Step 4: If n is 1, print 0.
Step 5: If n is 2, print 1.
Step 6: Else, calculate the element and print it.
Step 7: End.
PROGRAM:
import [Link];
public class fibonacci
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter the position of elt in fib. series :");
int n=[Link]();
if(n==1)
[Link]("1st elt is 0");
else if(n==2)
[Link]("2nd elt is 1");
else
{
int f=0,sec=1,t=0,i=3;
while(i<=n)
{
t=f+sec;
f=sec;
sec=t;
i++;
}
[Link](n+"th elt is "+t);
}
[Link]();
}
}
OUTPUT:
RESULT:
The JAVA program to print the nth value in the Fibonacci sequence was
implemented and executed successfully.
3. Write a java program to check the number is prime or not.
AIM:
To write a JAVA program to check the number is prime or not.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the number to be checked from the user, n.
Step 4: Determine if the number is prime or not and print the same.
Step 5: End.
PROGRAM:
import [Link];
public class primeNumber
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter a no. :");
int n=[Link]();
if(n==0||n==1)
[Link](n+" is not a prime no.");
else
{
int i=2,flag=0;
for(;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
[Link](n+" is a prime no.");
else
[Link](n+" is not a prime no.");
}
[Link]();
}
}
OUTPUT:
RESULT:
The JAVA program to check the number is prime or not was implemented and
executed successfully.
4. Write a java program to reverse a number using for, while, and recursion.
AIM:
To write a JAVA program to reverse a number using for, while, and recursion.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the number to reversed from the user.
Step 4: The reverse of number is displayed by printing the unit digit and performing
floor division, until the number is 0.
Step 5: End.
PROGRAM:
a) while:
import [Link];
public class reverse1
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter the no. :");
int n=[Link]();
int t,rev=0;
t=n;
while(t>0)
{
rev*=10;
rev+=(t%10);
t/=10;
}
[Link]("Reverse of "+n+" is "+rev);
[Link]();
}
}
b) for:
import [Link];
public class reverse2
{
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter the no. :");
int n=[Link]();
int rev=0;
for(int t=n;t>0;t/=10)
{
rev*=10;
rev+=(t%10);
}
[Link]("Reverse of "+n+" is "+rev);
[Link]();
}
}
c) recursion:
import [Link];
public class reverse3
{
static void revIt(int n)
{
if(n<10)
{
[Link](n);
return;
}
else{
[Link](n%10);
revIt(n/10);
}
}
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter the no. :");
int n=[Link]();
[Link]("Reverse of "+n+" is :");
revIt(n);
[Link]();
}
}
OUTPUT:
a) while:
b) for:
c) recursion:
RESULT:
The JAVA program to reverse a number using for, while and recursion was
implemented and executed successfully.
5. Write a java program to calculate average of n marks using array.
AIM:
To write a JAVA program to calculate average of n marks using array.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the number of subjects and their corresponding marks from the user.
Step 4: Calculate the average of the marks.
Step 5: End.
PROGRAM:
import [Link];
public class arrayAvg
{
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
[Link]("Enter no. of subejcts : ");
int n=[Link]();
int arr[]=new int[n];
int sum=0;
[Link]("Enter the marks : ");
for(int i=0;i<n;i++)
{
arr[i]=[Link]();
sum+=arr[i];
}
[Link]("Average mark : "+sum/n);
[Link]();;
}
}
OUTPUT:
RESULT:
The JAVA program to calculate the average of n marks using array was
implemented and executed successfully.
6. Write a java program to add and multiply two given matrices.
AIM:
To write a JAVA program to add and multiply to given matrices.
ALGORITHM:
Step 1: Start.
Step 2: Import required packages.
Step 3: Get the two matrices and the number of rows and columns in them from the
user.
Step 4: Display the list of operations to the user and get their choice.
Step 5: If the choice is 1, the matrices are added and the resultant is printed.
Step 6: If the choice is 2, the matrices are multiplied and the resultant is printed.
Step 7: End.
PROGRAM:
import [Link];
public class matrix
{
static void getmat(int[][] mat,int r,int c)
{
Scanner s=new Scanner([Link]);
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
mat[i][j]=[Link]();
}
}
static void multiplication(int [][]mat1,int[][] mat2,int mat3[][],int r1,i
nt c1,int r2,int c2)
{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
int sum=0;
for(k=0;k<c1;k++)
sum+=(mat1[i][k]*mat2[k][j]);
mat3[i][j]=sum;
}
}
}
static void addition(int[][] A,int[][] B,int[][] C,int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
C[i][j]=A[i][j]+B[i][j];
}
}
static void printmat(int[][] mat,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
[Link](mat[i][j]+" ");
[Link]();
}
}
public static void main(String args[])
{
Scanner s=new Scanner([Link]);
int r1,c1,r2,c2,result;
[Link]("Enter the no. of rows of matrix 1 :");
r1=[Link]();
[Link]("Enter the no. of columns in matrix 1:");
c1=[Link]();
[Link]("Enter the no. of rows of matrix 2 :");
r2=[Link]();
[Link]("Enter the no. of columns in matrix 2 :");
c2=[Link]();
int A[][]=new int[r1][c1];
int B[][]=new int[r2][c2];
[Link]("Enter the matrix 1 :");
getmat(A,r1,c1);
[Link]("Enter the matrix 2 :");
getmat(B,r2,c2);
[Link]("Enter choice : 1. Add 2. Multiply");
int ch=[Link]();
if(ch==1)
{
if(r1==r2 && c1==c2)
{
int C[][]=new int[r1][c1];
addition(A,B,C,r1,c1);
[Link]("Result mat: ");
printmat(C,r1,c1);
}
else
[Link]("Matrix mismatch");
}
else
{
if(c1==r2)
{
int[][] D=new int[r1][c2];
multiplication(A,B,D,r1,c1,r2,c2);
[Link]("Result mat : ");
printmat(D,r1,c2);
}
else
[Link]("Matrix mismatch");
}
[Link]();
}
}
OUTPUT:
RESULT:
The JAVA program to add and multiply two given matrices was implemented
successfully.