0% found this document useful (0 votes)
77 views

Object Oriented Programming: Assignments

The document contains 5 programming assignments completed by Neeraj Kumar Jaiswal. The assignments include programs to: 1) Print "Hello World" 2) Calculate the sum of digits of a number 3) Reverse a number 4) Convert between decimal and binary numbers 5) Display a pyramid structure of asterisks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Object Oriented Programming: Assignments

The document contains 5 programming assignments completed by Neeraj Kumar Jaiswal. The assignments include programs to: 1) Print "Hello World" 2) Calculate the sum of digits of a number 3) Reverse a number 4) Convert between decimal and binary numbers 5) Display a pyramid structure of asterisks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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[])
{
System.out.println("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 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

3. Write a program to reverse a number.

/* 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

4. Write a program to convert decimal number to binary and vice versa.

/* 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);
}

public static void main(String args[])


{

4|Page
NEERAJ KUMAR JAISWAL BATCH -B2

Scanner sc=new Scanner (System.in);


int n;
System.out.println("press 1 for binary to decimal ");
System.out.println("press 2 for decimal to binary ");
System.out.print("enter your choice :");
int c;
c=sc.nextInt();
N obj=new N();
N obj1=new N();
switch(c)
{
case 1:System.out.print("enter a number in binary : ");
n=sc.nextInt();
obj.btod(n);
break;
case 2:System.out.print("enter a number in decimal : ");
n=sc.nextInt();
obj1.dtob(n);
break;
default:System.out.println("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 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

1. Write a program to perform matrix addition.

/* Solution by -
Neeraj Jaiswal 0801IT181051 */

import java.util.Scanner;
class Mat{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("enter no. of rows");
int r=sc.nextInt();
System.out.println("enter no. of columns");
int c=sc.nextInt();
int a[][]=new int[r][c];
int b[][]=new int[r][c];

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();

System.out.println("enter second matrix");


for( i=0;i<r;i++)
for(j=0;j<c;j++)
b[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("the required matrix : ");


for( i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(" "+a[i][j]);

System.out.println();

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 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];

System.out.println("Enter elements of first matrix");


for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");


p = in.nextInt();
q = in.nextInt();
if (n != p)

System.out.println("The matrices can't be multiplied with each other.");


else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter elements of second matrix");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
second[c][d] = in.nextInt();
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;
}
}
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

3. Write a program to copy elements of an array to other in reverse order.

/* 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

4. Write a program to print fibbonacci series upto a given end-point.

/*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

5. Write a program to rotate a mairix by 90 and 180 degrees.

/* 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();

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]=sc.nextInt();
}
}
System.out.println("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++)
System.out.print(b[i][j] +" ");
System.out.println();
}

System.out.println("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++)
System.out.print(a[i][j] +" ");
System.out.println();
}

}
}

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 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;
}

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.print("enter the radius and height of cone :");
double r=sc. nextDouble();
double h=sc.nextDouble();
New n=new New();
double ans = n.calculate(r,h);
System.out.println(df2.format(ans));
}
}

Output window –

22 | P a g e

You might also like