OBJECT
ORIENTED
PROGRAMMING
ASSIGNMENTS
NEERAJ KUMAR JAISWAL
0801IT181051 BATCH B2 IT
NEERAJ KUMAR JAISWAL BATCH -B2
ASSIGNMENT – 01
1. Write a program to print hello world as output.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
class N
{
public static void main(String args[])
{
[Link]("Hello World");
}
}
Output window –
1|Page
NEERAJ KUMAR JAISWAL BATCH -B2
2. Write a program to calculate sum of digits of a number.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link];
class N
{
public static void main(String args[])
{
[Link]("enter a number : ");
Scanner sc=new Scanner ([Link]);
int n=[Link]();
int sum=0;
while(n!=0)
{
int i=n%10;
sum=sum+i;
n=n/10;
}
[Link]("The sum of digits is :"+sum);
}
}
Output window -
2|Page
NEERAJ KUMAR JAISWAL BATCH -B2
3. Write a program to reverse a number.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link];
class N
{
public static void main(String args[])
{
[Link]("enter a number : ");
Scanner sc=new Scanner ([Link]);
int n=[Link]();
int sum=0;
while(n!=0)
{
int i=n%10;
sum=sum*10+i;
n=n/10;
}
[Link]("The reverse of number is :"+sum);
}
}
Output window –
3|Page
NEERAJ KUMAR JAISWAL BATCH -B2
4. Write a program to convert decimal number to binary and vice versa.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link].*;
import [Link].*;
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];
[Link](num);
}
void btod(int a)
{ int i;
int count=0;
int num=0;
while(a!=0)
{
i=a%10;
num=num+i*(int )([Link](2,count));
a=a/10;
count++;
}
[Link](num);
}
public static void main(String args[])
{
4|Page
NEERAJ KUMAR JAISWAL BATCH -B2
Scanner sc=new Scanner ([Link]);
int n;
[Link]("press 1 for binary to decimal ");
[Link]("press 2 for decimal to binary ");
[Link]("enter your choice :");
int c;
c=[Link]();
N obj=new N();
N obj1=new N();
switch(c)
{
case 1:[Link]("enter a number in binary : ");
n=[Link]();
[Link](n);
break;
case 2:[Link]("enter a number in decimal : ");
n=[Link]();
[Link](n);
break;
default:[Link]("invalid choice entered ");
}
}
}
Output window –
5|Page
NEERAJ KUMAR JAISWAL BATCH -B2
5. Write a program to display pyramid structure with * symbol.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link].*;
import [Link].*;
class N
{
void find(int n)
{
int count=1,i,k,j;
for(i=1;i<=n;i++)
{[Link]();
[Link](" ");
for(j=1;j<=n-i;j++)
[Link](" ");
for(k=1;k<=count;k++)
[Link]("*");
count=count+2;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner ([Link]);
int n;
[Link]("enter the number of rows :");
n=[Link]();
N obj=new N();
[Link](n);
}
}
6|Page
NEERAJ KUMAR JAISWAL BATCH -B2
Output window –
7|Page
NEERAJ KUMAR JAISWAL BATCH -B2
ASSIGNMENT – 02
1. Write a program to perform matrix addition.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link];
class Mat{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter no. of rows");
int r=[Link]();
[Link]("enter no. of columns");
int c=[Link]();
int a[][]=new int[r][c];
int b[][]=new int[r][c];
int i,j,k,l;
[Link]("enter first matrix");
for( i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=[Link]();
[Link]("enter second matrix");
for( i=0;i<r;i++)
for(j=0;j<c;j++)
b[i][j]=[Link]();
for( i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=a[i][j]+b[i][j];
[Link]("the required matrix : ");
for( i=0;i<r;i++)
{
for(j=0;j<c;j++)
[Link](" "+a[i][j]);
[Link]();
8|Page
NEERAJ KUMAR JAISWAL BATCH -B2
}
}
Output window –
9|Page
NEERAJ KUMAR JAISWAL BATCH -B2
2. Write a program to perform matrix multiplication.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link];
class Mat
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows and columns of first matrix");
m = [Link]();
n = [Link]();
int first[][] = new int[m][n];
[Link]("Enter elements of first matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = [Link]();
[Link]("Enter the number of rows and columns of second matrix");
p = [Link]();
q = [Link]();
if (n != p)
[Link]("The matrices can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
[Link]("Enter elements of second matrix");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = [Link]();
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
sum = sum + first[c][k]*second[k][d];
10 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
multiply[c][d] = sum;
sum = 0;
}
}
[Link]("Product of the matrices:");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
[Link](multiply[c][d]+"\t");
[Link]("\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 [Link].*;
class New
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("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++;
}
[Link](n);
}
[Link]("total even oprerations :"+even);
[Link]("total odd oprerations :"+odd);
[Link]("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 [Link].*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the range :");
int a=[Link]();
int b=[Link]();
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)
[Link](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 [Link].*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the size of array :");
int n=[Link]();
int a[]=new int[n];
int i,flag=0,p=0;
for(i=0;i<n;i++)
a[i]=[Link]();
[Link]("enter the element to be searched :");
int x=[Link]();
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];
}
[Link]("the minimum and maximum elelments are : "+min +" and
"+ max);
if(flag==1)
[Link](x+" is found at position :"+ (p+1));
else
[Link]("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
3. Write a program to copy elements of an array to other in reverse order.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link].*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the size of array :");
int n=[Link]();
int a[]=new int[n];
int b[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=[Link]();
int count=0;
for( i=n-1;i>=0;i--)
{
b[count]=a[i];
count++;
}
for(i=0;i<n;i++)
[Link](b[i]+" ");
}
}
Output window –
18 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
4. Write a program to print fibbonacci series upto a given end-point.
/*Solution by
Neeraj Jaiswal 0801IT181051 */
import [Link];
class N
{ public static void main(String args[])
{
Scanner s=new Scanner([Link]);
[Link]("enter the end point of fibbonacci series :");
int n=[Link]();
int a=0,b=1,fibo;
[Link](0);
[Link](1);
while(b<=n)
{ fibo=a+b;
[Link](fibo);
a=b;
b=fibo;
}
}
}
Output window -
19 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
5. Write a program to rotate a mairix by 90 and 180 degrees.
/* Solution by -
Neeraj Jaiswal 0801IT181051 */
import [Link].*;
class N
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number of rows and columns :");
int r=sc. nextInt();
int c=[Link]();
int a[][]=new int[r][c];
int b[][]=new int[r][c];
int i,j,g=c-1,f;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=[Link]();
}
}
[Link]("After 180 degree rotation");
for(i=0;i<r;i++)
{
for(j=0,g=c-1;j<c && g>=0;j++,g--)
{
b[i][j]= a[g][i];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
[Link](b[i][j] +" ");
[Link]();
}
[Link]("After 180 degree rotation");
for(i=0;i<r;i++)
{
20 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
for(j=0,g=c-1;j<c && g>=0;j++,g--)
{
a[i][j]= b[g][i];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
[Link](a[i][j] +" ");
[Link]();
}
}
}
Output window –
21 | P a g e
NEERAJ KUMAR JAISWAL BATCH -B2
6. Write a program to find volume of cone by creating a class with appropriate
field and methods.
/*solution by
Neeraj Jaiswal 0801IT181051 */
import [Link].*;
import [Link];
import [Link];
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;
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter the radius and height of cone :");
double r=sc. nextDouble();
double h=[Link]();
New n=new New();
double ans = [Link](r,h);
[Link]([Link](ans));
}
}
Output window –
22 | P a g e