Assignment 3 PF (SP21-BSE-043)
Assignment 3 PF (SP21-BSE-043)
Assignment 3 PF (SP21-BSE-043)
Islamabad
Assignment 03
SUBMITTED BY: Maqbool Ahmed
(SP21-BSE-043)
SUBMITTED TO: SIR MUAFFAR IQBAL
SUBJECT: PF
DATED: 09-JUNE-2022
Q1:
CODE:
import java.util.*;
public class q1 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows of matrix 1: ");
int r1 = in.nextInt();
System.out.print("Enter the number of columns of matrix 1:
");
int c1 = in.nextInt();
System.out.print("Enter the number of rows of matrix 2: ");
int r2 = in.nextInt();
System.out.print("Enter the number of columns of matrix 2:
");
int c2 = in.nextInt();
if(c1==r2) {
int mat1[][] = new int[r1][c1];
int mat2[][] = new int[r2][c2];
int res[][] = new int[r1][c2];
System.out.println("Enter the elements of matrix 1: ");
for (int i= 0 ; i < r1 ; i++ ) {
for ( int j= 0 ; j < c1 ;j++ )
mat1[i][j] = in.nextInt();
}
System.out.println("Enter the elements of matrix 2: ");
for (int i= 0 ; i < r2 ; i++ ) {
for ( int j= 0 ; j < c2 ;j++ )
mat2[i][j] = in.nextInt();
}
System.out.println("\nOutput matrix:-");
for ( int i= 0 ; i < r1 ; i++ )
for ( int j= 0 ; j <c2;j++) {
int sum=0;
for (int k= 0 ; k <r2;k++ ) {
sum +=mat1[i][k]*mat2[k][j] ;
}
res[i][j]=sum;
}
for ( int i= 0 ; i < r1; i++ ) {
for ( int j=0 ; j < c2;j++ )
System.out.print(res[i][j]+" ");
System.out.println();
}
}
else
System.out.print("Multiplication does not exist! ");
}
}
OUTPUT:
Q2:
CODE:
import java.util.Scanner;
public class q2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int sum1=0;
int sum3=0;
int max=0;
int [][]a=new int[4][4];
System.out.println("enter elements");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
// sum of row
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
sum=sum+a[i][j];
}
System.out.println( "sum of row is "+ sum);
}
// sum of column
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
sum1=sum1+a[j][i];
}
System.out.println( "sum of column is "+ sum1);
}
// sum of diagonal
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==j){
sum3=sum3+a[i][j];
}
}
}
System.out.println("sum of diagonal is "+sum3);
// prime no
int b = 0;
int f = 0;
int count=0; // to show total number of prime numbers
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
b = a[i][j];
f = 0;
for (int k = 2; k <= b / 2; k++) {
if (b % k == 0) {
f++;
break;
}
}
if (f == 0) {
count++;
System.out.println(b + " is prime number");
}
}
}
System.out.println();
System.out.println("A total of "+count+" Prime Numbers are
present in Array");
System.out.println();
// largest element in matrix
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(max<a[i][j]){
max=a[i][j];
}
}
}
System.out.println("largest no in array is "+max);
}
}
OUTPUT:
Q3:
CODE:
import java.util.Scanner;
public class Class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("HOW MANY ELEMENTS YOU WANT IN ARRAY");
int elements = input.nextInt();
System.out.println("ENTER ELEMENTS");
int[] n = new int[elements];
for (int i = 0; i < n.length; i++) {
n[i] = input.nextInt();
}
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + " ");
}
System.out.println();
System.out.println("---------------------------");
System.out.println(" MENU");
System.out.println("---------------------------");
System.out.println("PRESS\n1 FOR COUNT\n2 FOR PARTITION\n3
FOR DUPLICATES\n4 FOR CIRCULAR\n5 FOR SHIFT CIRCULAR");
int select = input.nextInt();
if (select == 1) {
System.out.println();
System.out.println("Enter the number to find it's
occurrences");
int num = input.nextInt();
int c = count(n, num);
System.out.println(num + " is " + c + " Times");
}
if(select==2) {
System.out.println();
System.out.println("Before Partition");
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + " ");
}
System.out.println();
partition(n, 0, n.length - 1);
System.out.println("After partition");
for (int i = 0; i < n.length; i++) {
System.out.print(n[i] + " ");
}
}
if(select==3){
duplicates(n);
}
if(select==4){
System.out.println();
circular(n);
}
if(select==5){
System.out.println();
shiftCircular(n);
}
}
static int count(int arr[],int a){
int count=0;
for(int i=0;i< arr.length;i++){
if(arr[i]==a){
count++;
}
}
return count;
}
static void swap(int[] arr, int low, int pivot){
int tmp = arr[low];
arr[low] = arr[pivot];
arr[pivot] = tmp;
}
static int partition(int[] arr, int low, int high){
int p = low;
int j;
for(j=low+1; j <= high; j++)
if(arr[j] < arr[low])
swap(arr, ++p, j);
swap(arr, low, p);
return p;
}
static void duplicates(int n[]) {
int b[] = new int[1000];
int counter;
for (int i = 0; i < n.length; i++) {
counter = 1;
if (n[i] != -1) {
for (int j = i + 1; j < n.length; j++) {
if (n[i] == n[j]) {
counter++;
n[j] = -1;
}
}
b[i] = counter;
}
}
for (int i = 0; i < n.length; i++) {
if (n[i] != -1) {
System.out.println(n[i] + " is " + b[i] + "
times");
}
}
}
static void circular(int []n){
int first=n[0];
int second =n[1];
for(int i=0;i<n.length-2;i++){
n[i] = n[i+1] + n[i+2];
}
n[n.length-2] = n[n.length-1]+first;
n[n.length-1] = first+second;
for(int i=0;i<n.length;i++){
System.out.print(n[i]+ " ");
}
}
static void shiftCircular(int n[]){
int first = n[0];
int second = n[1];
for(int i=0;i<n.length-2;i++){
n[i]= n[i+2];
}
n[n.length-1]=second;
n[n.length-2]=first;
for(int i=0;i<n.length;i++){
System.out.print(n[i]+ " ");
}
}
}
OUTPUT: