Object Oriented Programming: Assignments
Object Oriented Programming: Assignments
ORIENTED
PROGRAMMING
ASSIGNMENTS
ASSIGNMENT – 01
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
class N
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Output window –
1|Page
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.Scanner;
class N
{
public static void main(String args[])
{
System.out.println("enter a number : ");
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
int sum=0;
while(n!=0)
{
int i=n%10;
sum=sum+i;
n=n/10;
}
System.out.println("The sum of digits is :"+sum);
}
}
Output window -
2|Page
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.Scanner;
class N
{
public static void main(String args[])
{
System.out.print("enter a number : ");
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
int sum=0;
while(n!=0)
{
int i=n%10;
sum=sum*10+i;
n=n/10;
}
System.out.println("The reverse of number is :"+sum);
}
}
Output window –
3|Page
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
import java.lang.*;
class N
{
void dtob(int n)
{ int i,count=0;
int a[]=new int[20];
while(n!=0)
{
i=n%2;
a[count]=i;
n=n/2;
count++;
}
int num=0;
for(i=count-1;i>=0;i--)
num=num*10+a[i];
System.out.println(num);
}
void btod(int a)
{ int i;
int count=0;
int num=0;
while(a!=0)
{
i=a%10;
num=num+i*(int )(Math.pow(2,count));
a=a/10;
count++;
}
System.out.println(num);
}
4|Page
NEERAJ KUMAR JAISWAL BATCH -B2
}
}
Output window –
5|Page
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
import java.lang.*;
class N
{
void find(int n)
{
int count=1,i,k,j;
for(i=1;i<=n;i++)
{System.out.println();
System.out.print(" ");
for(j=1;j<=n-i;j++)
System.out.print(" ");
for(k=1;k<=count;k++)
System.out.print("*");
count=count+2;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int n;
System.out.println("enter the number of rows :");
n=sc.nextInt();
N obj=new N();
obj.find(n);
}
}
6|Page
NEERAJ KUMAR JAISWAL BATCH -B2
Output window –
7|Page
NEERAJ KUMAR JAISWAL BATCH -B2
ASSIGNMENT – 02
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.Scanner;
class Mat{
int i,j,k,l;
System.out.println("enter first matrix");
for( i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=sc.nextInt();
for( i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=a[i][j]+b[i][j];
System.out.println();
8|Page
NEERAJ KUMAR JAISWAL BATCH -B2
}
}
Output window –
9|Page
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.Scanner;
class Mat
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
10 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of the matrices:");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
}
Output window –
11 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
3. Write a program read a positive integer value and compute the following
sequence. If the number is even half it , if it is odd multiply by 3 and then add 1.
Repeat the process until the value is 1 , printing out each value. Display how many
of these operations you performed.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
class New
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the number :");
int n=sc. nextInt();
int even=0,odd=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
even++;
}
else
{
n=n*3+1;
odd++;
}
System.out.println(n);
}
System.out.println("total even oprerations :"+even);
System.out.println("total odd oprerations :"+odd);
System.out.println("total operations :"+ (even+odd));
}
}
12 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
Output window –
13 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
ASSIGNMENT – 03
1. Write a program to print all prime numbers between two given numbers.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the range :");
int a=sc.nextInt();
int b=sc.nextInt();
int i,j,flag=0;
for(i=a;i<=b;i++)
{
flag=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.println(i);
}
}
}
14 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
Output window –
15 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
2. Write a program to find minimum, maximum element in given array and also
search for a given element in array and if present print its index.
import java.util.*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the size of array :");
int n=sc.nextInt();
int a[]=new int[n];
int i,flag=0,p=0;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.print("enter the element to be searched :");
int x=sc.nextInt();
int j;
int max= a[0];
int min=a[0];
for( i=0;i<n;i++)
{
if(a[i]==x)
{
p=i;
flag=1;
}
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("the minimum and maximum elelments are : "+min +" and
"+ max);
if(flag==1)
System.out.println(x+" is found at position :"+ (p+1));
else
System.out.println("element is not found ");
}
}
16 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
Output window –
17 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the size of array :");
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
int count=0;
for( i=n-1;i>=0;i--)
{
b[count]=a[i];
count++;
}
for(i=0;i<n;i++)
System.out.print(b[i]+" ");
}
}
Output window –
18 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
/*Solution by
Neeraj Jaiswal 0801IT181051 */
import java.util.Scanner;
class N
{ public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print("enter the end point of fibbonacci series :");
int n=s.nextInt();
int a=0,b=1,fibo;
System.out.println(0);
System.out.println(1);
while(b<=n)
{ fibo=a+b;
System.out.println(fibo);
a=b;
b=fibo;
}
}
}
Output window -
19 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter the number of rows and columns :");
int r=sc. nextInt();
int c=sc.nextInt();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(b[i][j] +" ");
System.out.println();
}
20 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(a[i][j] +" ");
System.out.println();
}
}
}
Output window –
21 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
/*solution by
Neeraj Jaiswal 0801IT181051 */
import java.util.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
class New
{
private static DecimalFormat df2 = new DecimalFormat("#.##");
double calculate(double r,double h)
{
final double pi=3.142;
double vol=(pi*r*r*h)/3;
return vol;
}
Output window –
22 | P a g e