OOP'S Lab Assignment 1
OOP'S Lab Assignment 1
Question 1-
public class Main
{
public static void main(String[] args) {
int s=0,a=1234;
for(;a>0;a=a/10)
s+=a%10;
System.out.println("the sum of digits of 1234 is"+s);
}
}
Question 2-
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int f=1;
for(int i=2;i<=a;i++)
f=f*i;
System.out.println("the factorial is"+f);
}
}
Question 3-
import java.util.*;
class Main
{
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
Question 4-
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int l=0;
int a[] = new int[n];
System.out.println("enter elements of the array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("enter the element to be searched");
int s=sc.nextInt();
for(int i=0;i<n;i++)
{
if(s==a[i])
{
System.out.println("Element found");
l=1;
break;
}
}
if(l!=1)
System.out.print("element not found");
}
}
Question 5-
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int p, q, m, n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix:");
p = sc.nextInt();
System.out.print("Enter the number of columns in the first matrix:");
q = sc.nextInt();
System.out.print("Enter the number of rows in the second matrix:");
m = sc.nextInt();
System.out.print("Enter the number of columns in the second matrix:");
n = sc.nextInt();
if (p == m && q == n) //checks if matricies can be added or not
{
int a[][] = new int[p][q];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("");
System.out.println("Enter all the elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("");
System.out.println("First Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
System.out.println("Second Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < q; k++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
else
{
System.out.println("Addition not possible");
System.out.println("Try Again");
}
}
}
Question 6-
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import java.math.*;
import java.util.*;
public class Main
{
int fakeIndex(int[] arr,int n)
{
int i=0;
int j=n-1;
int p=0;
int q=0;
int f=0;
int index=-1;
while(i<floor(n/2)&&j>=ceil(n/2)&&f==0)
{
p=p+arr[i];
q=q+arr[j];
if(p>q)
{
f=1;
index= j;
break;
}
else if(p<q)
{
f=1;
index= i;
break;
}
else
{
i++;
j--;
}
}
if(n%2==1&&f==0)
{
p=p+arr[0];
q=q+arr[n/2];
if(p!=q)
index= i;
else
index= -1;
}
return index;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of elements");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("enter the elements of the array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int index= fakeIndex(a);
System.out.println("Fake coin is at position="+(++index));
System.out.println("Total number of coins="+n);
}
}