Computer Project Work 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

FUNCTION OVERLOADING PROGRAMS

Q1. WAP to overload a function with the class name Area and calculate the area
of parallelogram, rhombus and a trapezium.

import java.util.Scanner;

class Area

void area(int b, int h)

int ar=b*h;

System.out.println("The area of the paralellogram with base "+b+" and


height "+h+" is "+ar);

void area(double d1, double d2)

double ar=1/2.0*(d1+d2);

System.out.println("The area of the Rhombus with diagonals "+d1+" and


"+d2+" is "+ar);

void area(int a,int b,int h)

double ar=1/2.0*(a+b)*h;

System.out.println("The area of the Trapezium with diagonals "+a+" and


"+b+" and height "+h+" is "+ar);

}
}

OUTPUT :-

Q2. WAP to overload a function with the class name Perimeter and calculate
the perimeter of square, a rectangle and circle.

import java.util.Scanner;

class Perimeter

void perimeter(int s)

int pr=4*s;

System.out.println("The perimeter of the square with side "+s+" is "+pr);

void perimeter(int l,int b)

int pr=2*(l+b);

System.out.println("The perimeter of a rectangle with length "+l+" and


breadth "+b+" is "+pr);

}
void perimeter(double r)

double pr=2*(22/7.0)*r;

System.out.println("The perimeter of a circle with radius "+r+" is "+pr);

OUTPUT :-
ARRAY PROGRAMS
Q1. WAP to input 10 values in an array to count the number of odd and even
numbers present in the array.

import java.util.Scanner;

class array_1

void display()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

int o=0,e=0;

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int j=0;j<10;j++)

if(a[j]%2==0)

e=e+1;

else
{

o=o+1;

System.out.println("The frequency of the even elements and odd element


are "+e+" and "+o+" respectively");

OUTPUT :-

Q2. WAP to input 10 values in an array and display the highest and the lowest
value from the array.

import java.util.Scanner;

class array_2

void display()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 numbers");
int a[]=new int[10];

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

int max=a[0];

int min=a[0];

for(int i=0;i<10;i++)

if(a[i]>max)

max=a[i];

if(a[i]<min)

min=a[i];

System.out.println("The maximum element and minimum element of the


array are "+max+" and "+min+" respectively");

}
OUTPUT :-

Q3. WAP to input 10 values in an array and count the number of values divisible
by 5.

import java.util.Scanner;

class array_3

void display()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

int c=0;

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int i=0;i<10;i++)

{
if(a[i]%5==0)

c=c+1;

System.out.println("The number of elements divisible by 5 are "+c);

OUTPUT :-

Q4. WAP to input 10 values in an array and calculate the sum of all even
numbers present in the array.

import java.util.Scanner;

class array_4

void calculate()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];


int s=0;

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int i=0;i<10;i++)

if(a[i]%2==0)

s=s+a[i];

System.out.println("The sum of all even elements in the array is "+s);

OUTPUT :-
Q5. WAP to input 10 value in an array and calculate the sum of all the last digit
of each element.

import java.util.Scanner;

class array_5

void calculate()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

int s=0,l=0;

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int i=0;i<10;i++)

l=a[i]%10;

s=s+l;

System.out.println("The sum of the last digit of all the elements is "+s);

}
OUTPUT :-

Q6. WAP to input 10 values in an single-dimensional array to display the last


digit of each element.

import java.util.Scanner;

class array_6

void display()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

for(int i=0;i<10;i++)

{
int l=a[i]%10;

System.out.println("The last digit of "+a[i]+" is "+l);

OUTPUT :-

Q7. WAP to input 10 values in a single-dimensional array and display the sum of
each element.

import java.util.Scanner;

class array_7

void display()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)

a[i]=sc.nextInt();

int r=0,d=0;

for(int i=0;i<10;i++)

int n=a[i],copy=n;

while(n>0)

r=n%10;

d=r+d;

n=n/10;

System.out.println("The sum of the digits of the number "+copy+" is "+d);

r=0;

d=0;

}
OUTPUT :-

Q8. WAP to input 10 values in a single-dimensional array and count the number
of palindromic number present in that array.

import java.util.Scanner;

class array_8

int palindrome(int n)

int r=0,d=0;

while(n>0)

r=n%10;

d=(d*10)+r;

n=n/10;

return d;
}

void count()

Scanner sc=new Scanner(System.in);

int a[]=new int[10];

System.out.println("Enter 10 numbers");

for(int i=0;i<10;i++)

a[i]=sc.nextInt();

int c=0;

for(int i=0;i<10;i++)

if(a[i]==palindrome(a[i]))

c++;

System.out.println("The number of elements that are Palindromic Number is


"+c);

}
OUTPUT :-
SUM OF SERIES PROGRAM
Q. WAP to display the sum of the series with the class name SeriesSum.

S= + + + ………(
! ! ! )!

import java.util.Scanner;

class SeriesSum

int x,n;

double sum;

SeriesSum(int xx,int nn)

x=xx;

n=nn;

double find_fact(int m)

int i;

double f=1.0;

for(i=1;i<=m;i++)

f=f*i;

return f;

}
double find_power(int x,int y)

double power=Math.pow(x,y);

return power;

void calculate()

for(int i=2;i<=n;i=i+2)

sum=sum+(find_power(x,i)/find_fact(i-1));

void display()

System.out.println("The sum of the series is "+sum);

void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int num=sc.nextInt();

System.out.println("Enter the number of terms till which the sum of series


will be calculated");
int term=sc.nextInt();

SeriesSum ob=new SeriesSum(num,term);

ob.calculate();

ob.display();

OUTPUT :-

You might also like