1. // to find areas using functions.using switch
import java.io.*;
class m1
{
double area(double r)
{
return(3.14*r*r);
}
int area(int s)
return(s*s);
}
double area(double l, double b)
{
return(l*b);
}
public static void main(String args[]) throws IOException
{
m1 ob= new m1();
double p;
int q;
double w;
BufferedReader input= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("*** MAIN MENU ***");
System.out.println("1. Area of Circle");
System.out.println("2. Area of Square");
System.out.println("3. Area of Rectangle");
System.out.println("4. Exit");
System.out.println("Enter Choice(1-4)");
int ch=Integer.parseInt(input.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the radius");
double r=Double.parseDouble(input.readLine());
p=ob.area(r);
System.out.println("Area of Circle"+p);
break;
case 2:
System.out.println("Enter the side");
int s=Integer.parseInt(input.readLine());
q=ob.area(s);
System.out.println("Area of Square" +q);
break;
case 3:
System.out.println("Enter length");
int l=Integer.parseInt(input.readLine());
System.out.println("Enter bredth");
int b=Integer.parseInt(input.readLine());
w=ob.area(l,b);
System.out.println("Area of Rectangle" +w);
break;
case 4:
break;
}
}}
2. // to find Sum=(1*2) +(2* 3) +(3*4)...........................+(9*10)
class m2
{
int product(int a,int b)
{
int c;
c=a*b;
return(c);
}
public static void main(String args[])
{
int i,k,s; s=0;
m2 ob= new m2();
for(i=1;i<=9;i++)
{
k=ob.product(i,i+1);
s=s+k;
}
System.out.println("The sum of series=" +s);
}
}
3.// to find the sum of the series using function
import java.io.*;
class m3
{
double fact(int a)
{
int i,f;
f=1;
for(i=1;i<=a;i++)
{
f=f*i;
}
return (f);
}
public static void main(String args[]) throws IOException
{ int j,n;
double k,s;
s=0.0;
m3 ob=new m3();
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your Number");
n=Integer.parseInt(input.readLine());
for(j=1;j<=n;j++)
{ k=ob.fact(n);
}
System.out.println("The sum of the series is "
+s);
}}
4. //s=1+a2/2!.............
import java.io.*;
class m4
{ int fact(int m)
{ int i,f;f=1;
for(i=1;i<=m;i++)
{ f=f*i;
}
return(f);
}
public static void main(String args[]) throws IOException
{ int a,j,n;
double p,s;
s=0.1;
int k=0;
p=0.1;
BufferedReader input= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of a");
a =Integer.parseInt(input.readLine());
System.out.println("Enter the value of n");
n =Integer.parseInt(input.readLine());
m4 ob = new m4();
for(j=2;j<=n;j+=2)
{ k=ob.fact(j);
p=Math.pow(a,j);
s=s+p/k;
}
System.out.println("The sum of the series" +s);
} }
5. // to find whether a number is prime or not using
function
import java.io.*;
class m5
{ int check(int n)
{ int i,f;
f=1;
for(i=2;i<n;i++)
{
if(n%i==0)
f=0;
}
return(f);
}
public static void main(String args[]) throws IOException
{ int a,k=0;
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your number");
a=Integer.parseInt(input.readLine());
m5 ob = new m5();
k=ob.check(a);
if(k= =1)
System.out.println(a + " is a prime number");
else
System.out.println(a + " is not a prime number");
}
}
6. //To find the HCF and LCM
import java.io.*;
class m6
{ int hlcm(int a, int b)
{ int i, hcf, p;
hcf=0;
p=a*b;
for(i=1;i<p;i++)
{ if(a%i==0
&& b%i==0)
hcf =i;
}
return(hcf);
}
public static void main(String args[]) throws IOException
{ BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
int m,n,p,k,c;
k=0, c=0;
m6 ob = new m6();
System.out.println("Enter two numbers");
m=Integer.parseInt(input.readLine());
n=Integer.parseInt(input.readLine());
p=m*n;
k=ob.hlcm(m,n);
c=p/k;
System.out.println("The HCF is " +k);
System.out.println("The LCM is " +c);
} }
7. //To find whether a number is palindrome or not using
function
import java.io.*;
class m7
{
int reverse(int n)
{ int d,r=0;
do
{ d=n%10;
r=r+d*d*d;
n=n/10;
}while(n!=0);
return(r);
}
public static void main(String args[]) throws IOException
{ BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
int k,a,r,d,c;
r=0;
m7 ob = new m7();
System.out.println("Enter Your number");
a=Integer.parseInt(input.readLine());
c=a;
k=ob.reverse(a);
if(k= =c)
System.out.println("The number " + c + " is a
palindrome");
else
System.out.println("The number " + c + " is
not a palindrome");
}}
8. //To check whether a number is Automorphic or not using
function
import java.io.*;
class m8
{ int digits(int n)
{ int c,p,k;
c=0, k=0;
while(n!=0)
{ k=n/10;
c=c+1;
n=k;
}
return(c);
}
public static void main(String args[]) throws IOException
{ BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int m,n,b,p;
double r=0.1;
m8 ob = new m8();
System.out.println("Enter Your number");
n=Integer.parseInt(input.readLine());
m=n;
p=m*m;
b=ob.digits(n);
r=p%(Math.pow(10,b));
if(m= =r)
System.out.println(m+ " is an Automorphic
number");
else
System.out.println(m+ " is an Automorphic
number");
}}
9. //To check whether a number is twin prime or not using function
import java.io.*;
class m9
{ int prime(int n)
{ int i,f=1;
for(i=2;i<n;i++)
{ if(n%i==0)
f=0;
}
return(f);
}
public static void main(String args[]) throws IOException
{ BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
int m,p,x,y;
m9 ob = new m9();
System.out.println("Enter Your first number");
m=Integer.parseInt(input.readLine());
System.out.println("Enter Your Second number");
p=Integer.parseInt(input.readLine());
x=ob.prime(m);
y=ob.prime(p);
if((x= =1 && y= =1)&&(m-p = = 2 || p-m= =2))
System.out.println("The number " + m +
"and" + p +" are twin primes");
else
System.out.println("The number " + m + " and
" + p +" are not twin primes");
} }
10. //To calculate
the electricity bill using function
import java.io.*;
class m10
{
void charge(int u)
{
double c,a;
c=0.0;
a=0.0;
if(u<=100)
{
c=u*0.80;
a=c+50.0;
}
if((u>100) && (u<=200))
{
c=((100*0.80)+(u-100)*1.0);
a=(c+50.0);
}
if(u>200)
{
c=((100*0.80)+(100*1.0)+(u-200)*1.25);
a=c+50.0;
}
System.out.println("The unit consumed="+u);
System.out.println("The charge = Rs." +c);
System.out.println("The additional charge=
Rs.50");
System.out.println("The amount to be paid=Rs."+a);
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int un; un=0;
m10 ob = new m10();
System.out.println("Enter the unit consumed");
un=Integer.parseInt(input.readLine());
ob.charge(un);
}
}
11. //To interchange
the value of a and b by using method
import java.io.*;
class m11
{
int temp;
float temp1;
void display(int a,
int b)
{
temp=a;
a=b;
b=temp;
System.out.println("The value of a" + a +
"and b is" +b );
}
public void display(float a, float b)
{
temp1=a;
a=b;
b=temp1;
System.out.println("The value of a " + a + "
and b is " +b );
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int m,n;
float p,q;
m11 ob = new m11();
System.out.println("Enter the value of m, n(integer),
& p, q(decimals)");
m=Integer.parseInt(input.readLine());
n=Integer.parseInt(input.readLine());
p=Float.parseFloat(input.readLine());
q=Float.parseFloat(input.readLine());
ob.display(m,n);
ob.display(p,q);
}
}
12. //To pass the elements of an array to a function
import java.io.*;
class m12
{
void sort(int m[])
{
int i,j,t;
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(m[i]>m[j])
{
t=m[i];
m[i]=m[j];
m[j]=t;
}
}
}
System.out.println("The numbers are arranged in
ascending order are");
for(i=0;i<10;i++)
{
System.out.println(m[i]);
}
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
m12 ob = new m12();
int n[]= new int[10];
System.out.println("Enter the 10 numbers");
for(int i=0;i<10;i++)
{
n[i]=Integer.parseInt(input.readLine());
}
ob.sort(n);
}
}
13. //Linear Search
import java.io.*;
class m13
{
void search(int m[], int ns)
{
int i,k;
k=0;
for(i=0;i<10;i++)
{
if(m[i]==ns)
{
k=1;
break;
}
}
if(k==1)
System.out.println("The search is sucessful");
else
System.out.println("The search is not sucessful");
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
m13 ob = new m13();
int i,ns;
int n[]= new int[10];
System.out.println("Enter the 10 numbers");
for(i=0;i<10;i++)
{
n[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter your no. to be
searched");
ns=Integer.parseInt(input.readLine());
ob.search(n,ns);
}
}
14. //Binary Search
import java.io.*;
class m14
{ void search(int
m[],int b, int ns)
{ int lb,ub,p,k=0;
lb=0;ub=b-1;
while(lb<=ub)
{ p=(lb+ub)/2;
if(m[p]<ns)
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
{ k=1;
break;
} }
if(k= =1)
System.out.println("The search is successful");
else
System.out.println("The search is not
successful");
}
public static void main(String args[]) throws IOException
{ BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
m14 ob = new m14();
int i,ns,n;
n=Integer.parseInt(input.readLine());
int m[]= new int[n];
System.out.println("Enter the n numbers");
for(i=0;i<n;i++)
{
m[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter your no. to be
searched");
ns=Integer.parseInt(input.readLine());
ob.search(m,n,ns);
}}
15. //To display all the vowels present in the word
import java.io.*;
class m15
{ void display(String
a)
{ int x,y,v;v=0;
char b;
x=a.length();
System.out.println("The vowels are:");
for(y=0;y<x;y++)
{ b=a.charAt(y);
if((b=='a')||(b=='e')||(b=='i')||(b=='o')||(b=='u')||(b=='A')||(b=='E')||(b=='I')||(b=='O')||(b=='U'))
System.out.println(b);
} }
public static void main(String args[]) throws IOException
{ int m;
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m15 ob = new m15();
System.out.println("Enter your word");
String p=input.readLine();
ob.display(p);
} }
16. //boolean Search
import java.io.*;
class m16
{ boolean search(int
m[], int ns)
{ int i;
boolean p=false;
for(i=0;i<10;i++)
{
if(m[i]==ns)
p=true;
}
return(p);
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
m16 ob = new m16();
int i,ns;
int m[]= new int[10];
boolean y;
System.out.println("Enter marks");
for(i=0;i<10;i++)
{
m[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter your no. to be
searched");
ns=Integer.parseInt(input.readLine());
y=ob.search(m,ns);
if(y= =true)
System.out.println("The search is sucessful");
else
System.out.println("The search is not sucessful");
} }
17. //to find cube of ten numbers
import java.io.*;
class m17
{ void cube(int n)
{ int c=0;
c=n*n*n;
System.out.println("Cube of the number is =" +c);
}
public static void main(String args[]) throws IOException
{ BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
m17 ob = new m17();
int i;
System.out.println("Enter the 10 numbers");
for(i=0;i<10;i++)
{ int
n=Integer.parseInt(input.readLine());
ob.cube(n);
}
}
}
18. //to check whether the triangle is equilateral, scalene,
isosceles
import java.io.*;
class m18
{ void check(int a,int b,int c)
{ if((a= =b)&&(b= =c)&&(c= =a))
{System.out.println("The triangle is
equilateral");
}
if((a!=b)&&(b!=c)&&(c!=a))
{
System.out.println("The triangle is scalene");
}
if((a= =b && a!=c)||(b= =c && b!=a )||(c= =a && c!=b))
{ System.out.println("The triangle is isosceles");
}
}
public static void main(String args[]) throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
m18 ob = new m18();
System.out.println("Enter the sides");
int a=Integer.parseInt(input.readLine());
int b=Integer.parseInt(input.readLine());
int c=Integer.parseInt(input.readLine());
ob.check(a,b,c);
}
}
19. // to find areas using functions.using switch
import java.io.*;
class m19
{
void daysofweek(int n)
{
int ch=n;
switch(ch)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid entry");
}}
public static void main(String args[]) throws IOException
{
m19 ob= new m19();
m19 ob2= new m19();
m19 ob3= new m19();
m19 ob4= new m19();
m19 ob5= new m19();
m19 ob6= new m19();
m19 ob7= new m19();
double p;
int q;
double w;
BufferedReader input= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter day number(1-7)");
int dysno=Integer.parseInt(input.readLine());
ob.daysofweek(dysno);
}
}
20. // to find whether a number is armstrong or not using
function
import java.io.*;
class m20
{ int Armstrong(int
n)
{
int p=n;
int k=0, sum=0;
while(n!=0)
{ int r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum= =p)
{ k=1;
}
return(k);
}
public static void main(String args[]) throws IOException
{ int a,k;
k=0;
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your number");
a=Integer.parseInt(input.readLine());
m20 ob = new m20();
k=ob.Armstrong(a);
if(k= =1)
System.out.println(a + " is a armstrong number");
else
System.out.println(a + " is not a armstrong
number");
} }
21. //To display all the vowels present in the word
import java.io.*;
class m21
{
void freq(String x)
{ int y;
int q=0,r=0,s=0,t=0,u=0;
char b;
int m=x.length();
for(y=0;y<m;y++)
{ b=x.charAt(y);
if((b= ='a')||(b= ='A'))
{ q=q+1;
}
if((b=='e')||(b=='E'))
{ r=r+1;
}
if((b=='i')||(b=='I'))
{ s=s+1;
}
if((b=='o')||(b=='O'))
{ t=t+1;
}
if((b=='u')||(b=='U'))
{ u=u+1;
} }
System.out.println("Frequency of vowel a" +q);
System.out.println("Frequency of vowel e" +r);
System.out.println("Frequency of vowel i" +s);
System.out.println("Frequency of vowel o" +t);
System.out.println("Frequency of vowel u" +u);
}
public static void main(String args[]) throws IOException
{
int m;
String p;
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m21 ob = new m21();
System.out.println("Enter your word");
p=input.readLine();
ob.freq(p);
}
}
22. //To display all the vowels present in the word
import java.io.*;
class m22
{ void freq(String x)
{ int y;
String c="";
char b;
int m=x.length();
for(y=0;y<m;y++)
{ b=x.charAt(y);
if((b>='a' && b<='z'))
{ c=c+b;
} }
System.out.println("Lower case letters:" +c);
}
public static void main(String args[]) throws IOException
{ int m;
String p;
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m22 ob = new m22();
System.out.println("Enter your word");
p=input.readLine();
ob.freq(p);
} }
23. //To interchange the values
import java.io.*;
class m23
{ void add(int a[][],int b[][])
{ int x,y;
int p[][]= new int[3][3];
for( x=0;x<3;x++)
{ for(y=0;y<3;y++)
{ p[x][y]=a[x][y]+b[x][y];
} }
System.out.println("The addition matrix is");
for(x=0;x<3;x++)
{ for(y=0;y<3;y++)
{ System.out.print(p[x][y]+" ");
} System.out.println();
}}
public static void main(String args[]) throws IOException
{ BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int n[][]= new int[3][3];
int o[][]= new int[3][3];
int i,j;
System.out.println("Enter the first matrix");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{n[i][j]=Integer.parseInt(input.readLine());
}}
System.out.println("Enter the second matrix");
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{o[i][j]=Integer.parseInt(input.readLine());
}} m23 ob = new
m23();
ob.add(n,o);
}}
24.// to print the following functions like abs,
random,log,sqrt
import java.io.*;
class m24
{
public static void main(String args[]) throws IOException
{
BufferedReader
input=new BufferedReader(new InputStreamReader(System.in));
System.out.println("***** MENU *****");
System.out.println("1. NATURAL lOGARITHM");
System.out.println("2. ABSOLUTE VALUE ");
System.out.println("3.sQUARE ROOT ");
System.out.println("4.RANDOM NUMBER");
System.out.println();
System.out.println("Please enter your
choice(1-4):");
int ch=Integer.parseInt(input.readLine());
switch(ch)
{
case 1:
System.out.println("Please enter a number whose
alogrithm to be found");
int n=Integer.parseInt(input.readLine());
double x=Math.log(n);
System.out.println("The logarithm of the number
is:" +x);
break;
case 2:
System.out.println("Please enter a number whose
Absolute value is to be found");
int n1=Integer.parseInt(input.readLine());
int m = Math.abs(n1);
System.out.println("The absolute value of the number
is:" +m);
break;
case 3:
System.out.println("Please enter a number whose square
root is to be found");
int n2=Integer.parseInt(input.readLine());
double m2 =
Math.sqrt(n2);
System.out.println("The square root of the number
is:" +m2);
break;
case 4:
System.out.println("Please enter a number whose random
number is to be found");
double t=Math.random();
System.out.println("Random number is " +t);
break;
default : System.out.println("Invalid choice");
}
}
}
/*25.
To accept the details of a teacher
including the monthly salary.
(ii)
To display the details of the teacher
(iii) To computer the annual Income Tax as 5%
of the annual salary above Rs. 1,75,000Write a main method to create object of
a class and call the above member method.*/
class m25
{
String name;
String address;
String phone;
String subject;
double sal;
double incometax;
BufferedReader input
= new BufferedReader(new
InputStreamReader(System.in));
void accept() throws IOException
{ System.out.println("Enter Name");
name=input.readLine();
System.out.println("Enter Address");
address=input.readLine();
System.out.println("Enter Phone
Number");
phone=input.readLine();
System.out.println("Enter Subject");
subject=input.readLine();
System.out.println("Enter Salary");
sal=Double.parseDouble(input.readLine());
}
void display()
{
System.out.println("Name : " +name);
System.out.println("Address : "
+address);
System.out.println("Phone : "
+phone);
System.out.println("Subject
: " +subject);
System.out.println("Salary : "
+sal);
}
void calc()
{
double netpay=sal*12.0;
if(netpay>=175000)
{
incometax=5.0/100.0*(netpay-175000);
System.out.println("Income Tax : " +incometax);
}
else
{
System.out.println("No Income tax");
}
}
public static void main(String args[]) throws IOException
{
m25 ob = new m25();
ob.accept();
ob.display();
ob.calc();
}
}
26. //to check
palindrome
import java.io.*;
class m26
{
void palin(String n)
{
char m;
String n1="";
int a=0;
int i=0;
a=n.length();
for(i=a-1;i>=0;i--)
{ m=n.charAt(i);
n1=n1+m;
}
if(n.equals(n1))
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}
public static void main (String args[])throws IOException
{ m26 ob = new m26();
BufferedReader input =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your String");
String n=input.readLine();
ob.palin(n);
} }
27.
// To compute the following : f(x) = 2.4x2 + 5.3x-1.6 for x taken as
–2. using method
//
I f your are using static with function
then you need not to declare object in the main.
//
if not then You can call function using object
{
static double f(double x)
{
return
(2.4*x*x+5.3*x-1.6);
}
public static void main (String args[])
{
double res=f(-2);
System.out.println("The result is : " +res);
} }
class m27a
{
double f(double x)
{
return
(2.4*x*x+5.3*x-1.6);
}
public static void main (String args[])
{
m27a ob= new m27a();
double res=ob.f(-2);
System.out.println("The result is : " +res);
}}
28. //Define a method named asc() to perform the above
stated job
class m28
{ void asc()
{ char c1='?', c2='Z', c3='m',c4='#';
int as1=c1,as2=c2,as3=c3,as4=c4;
System.out.println("The ascii code of " + c1 +
" character is =" + as1);
System.out.println("The ascii code of " + c2 +
" character is =" + as2);
System.out.println("The ascii code of " + c3 +
" character is =" + as3);
System.out.println("The ascii code of " + c4 +
" character is =" + as4);
}
public static void main(String args[])
{ m28 ob = new
m28();
ob.asc();
} }
29. /* WAP in Java with the following members of its class storeCustomer
and compute the total amount to be
paid by the customer and show the data and amount. */
import java.io.*;
class m29
String cust_name, cust_add;
double due_amt, amount;
void getvalues(char cid, String nam, String add, double due)
throws IOException
{ cust_id =cid;
cust_name=nam, cust_add=add;
due_amt=due;
}
void calculate(double
present_amt)
{ amount
=due_amt+present_amt;
}
void display()
{
System.out.println("****** CUSTOMER DETAILS ******");
System.out.println();
System.out.println("CUSTOMER INDENTITY CODE : " + cust_id);
System.out.println("CUSTOMER'S NAME
:" + cust_name);
System.out.println("ADDRESS : " +
cust_add);
System.out.println("AMOUNT DUE : "
+due_amt);
System.out.println("TOTAL AMOUNT : "
+amount);
}
public static void main(String args[]) throws IOException
{ m29 ob = new m29();
BufferedReader input =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Customer
id");
char c =(char)input.read();
System.out.println("Enter Customer
Name");
String na=input.readLine();
System.out.println("Enter Addess");
String ad=input.readLine();
System.out.println("Enter Due amount");
double du=Double.parseDouble(input.readLine());
ob.getvalues(c,na,ad,du);
System.out.println("Enter present
amount");
double pr_amt=Double.parseDouble(input.readLine());
ob.calculate(pr_amt);
ob.display();
} }
30. // To compute the result of a student
import java.io.*;
class m30
{
double result(int roll, String nam,int m1, int m2, int m3)
{
double t=0.0d, per=0.0d;
t =(double)m1+m2+m3;
per =t/3.0;
return(per);
}
public static void main (String args[])throws IOException
{
m30 ob = new m30();
BufferedReader input =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the rollno, name and marks in
english,maths and computers");
int r= Integer.parseInt(input.readLine());
String n=input.readLine();
int em= Integer.parseInt(input.readLine());
int mm= Integer.parseInt(input.readLine());
int cm= Integer.parseInt(input.readLine());
double p=ob.result(r,n,em,mm,cm);
System.out.println();
System.out.println("The percentage of the student is :
" +p);
}
}
31. // To compute S=ut+1/2at2 , where u =20m/s, t=20 min
a=10m/s2
class m31
{
void displacement()
{
double u=20.0,
t=20.0*60.0,a=10.0;
double s=u*t+1.0/2.0*a*t*t;
System.out.println("The displacement is : " + s +
" metres");
}
public static void main (String args[])
{
m31 ob = new m31();
ob.displacement();
} }
32. /* To compute Algebra to find the difference in the two
algebraic expression import java.io.*;
class m32
{ double f1(double x)
{ return x*x+2*x+4;
}
double f2(double y)
{ return
-(y*y)-20*y-1;
} double f3(double
x,double y)
{ double result
=f1(x)-f2(y);
return(result);
} public static void
main (String args[]) throws IOException
{BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m32 ob = new m32();
System.out.println("Enter the value of x");
double x=Double.parseDouble(input.readLine());
double x1=ob.f1(x);
System.out.println("The result is : " +x1);
System.out.println("Enter the value of y");
double y=Double.parseDouble(input.readLine());
double y1=ob.f2(y);
System.out.println("The result is : " +y1);
double res=ob.f3(x,y);
System.out.println("The final result is : " +res);
}}
33. /* To
calculate parameter of square and rectangle
import java.io.*;
class m33
{ long parameterRectangle(long side1,long side2)
{ return 2*(side1+side2);
} long squarepara(long side)
{ return side*side;
} public static void main (String args[]) throws IOException
{ BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m33 ob = new m33();
System.out.println("******MENU*******");
System.out.println("1. TO find rectangle's
parameter");
System.out.println("2. TO find the parameter of
square");
System.out.println("Please enter the choice 1/2");
int ch = Integer.parseInt(input.readLine());
switch(ch)
{ case 1: System.out.println("Please enter the two
sides");
long s1 =Long.parseLong(input.readLine());
long s2 = Long.parseLong(input.readLine());
long p=ob.parameterRectangle(s1,s2);
System.out.println("The parameter of the rectangle is :
" +p);
break;
case 2:
System.out.println("Please enter the side");
long s =Long.parseLong(input.readLine());
long para=ob.squarepara(s);
System.out.println("The parameter of the square is :
" + para);
break;
default :
System.out.println("invalid choice");
}}}
34. /*
WAP to compute double of a number
import java.io.*;
class m34
{ double num,d;
void assign(double n)
{ num=n;
}
void doublenumber()
{ d=2*num;
}
void display()
{
System.out.println("The double of "
+num+ " = " +d);
}
public static void main (String args[]) throws IOException
{ BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m34 ob = new m34();
System.out.println("Please enter a number");
int n = Integer.parseInt(input.readLine());
ob.assign(n);
ob.doublenumber();
ob.display();
}}
35./* Write a complete Java program
that defines the following methods: assign() : that accepts values of the digit
at unit’s place, the digit at ten’s place
and the digit at hundred’s place
findnumber()
: that finds the number corresponding to the accepted digits. main() : that calls the above methods and display the
computed number */
import java.io.*;
class m35
{
int unit,tens;
void assign(int u, int t)
{
unit =u;
tens=t;
}
long findnumber()
{ return tens*10+unit;
}
public static void main (String args[]) throws IOException
{ BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m35 ob = new m35();
System.out.println("Please enter a unit palce digit and
tens place digit");
int n = Integer.parseInt(input.readLine());
int n2 = Integer.parseInt(input.readLine());
ob.assign(n,n2);
System.out.println("The number is : "
+ob.findnumber());
}
}
36.
/* Define a method stringreverse(String
obj) that returns the reversed string object to the main module, where it is displayed.
Note
: reverse funtion is used only with StringBuffer */
{
StringBuffer stringReverse(StringBuffer str)
{
str.reverse();
return str;
}
public static void main (String args[])
{
m36 ob = new m36();
StringBuffer t= new StringBuffer("Aditi");
StringBuffer strr=t.reverse();
System.out.println("The reversed string is : "
+strr);
}}
37. /* Define a method rev(String m, int n) where m denotes
the position in string and
n denotes the number
of character from m position.
The method reverses the n characters specified from
position m and
returns the reversed string to main method where it is
displayed.
*/
import java.io.*;
class m37
{
StringBuffer rev(StringBuffer str,int m,int n)
{
String strn=str.substring(m,m+n);
StringBuffer str2 = new StringBuffer(strn);
str2.reverse();
return str2;
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m37 ob = new m37();
System.out.println("Please enter a string");
String nam=input.readLine();
StringBuffer t=new StringBuffer(nam);
System.out.println("please enter the position from
where to extract");
int pos= Integer.parseInt(input.readLine());
System.out.println("please enter the number of
characters to extract");
int nm=Integer.parseInt(input.readLine());
StringBuffer strr=ob.rev(t,pos,nm);
System.out.println("The string is :"+strr);
}
}
38. /* to read a string from console and replace all
occurrences of the sixth
character of the string by the digit 9.*/
import java.io.*;
class m38
{
void replacing() throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter a string");
String nam=input.readLine();
char x=nam.charAt(5);
String ch=nam.replace(x,'9');
System.out.println("changed string is :" +ch);
}
public static void main (String args[]) throws IOException
{
m38 ob = new m38();
ob.replacing();
}}
39. /* Write a Java method trigonometry() that prints the
sine, cosine, and tangent
ratios of an angle accepted from the user.
Assume that user enters the angles in degrees.*/
import java.io.*;
class m39
{
void trigonometry() throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter angle");
double angle=Double.parseDouble(input.readLine());
double a=Math.PI/180*angle;
double s=Math.sin(a);
double c=Math.cos(a);
double t=Math.tan(a);
System.out.println("Value of Sine of " +angle + " is " +s);
System.out.println("Value of Cossine of " +angle + " is " +c);
System.out.println("Value of tangent of " +angle + " is " +t);
}
public static void main (String args[]) throws IOException
{
m39 ob = new m39();
ob.trigonometry();
}
}
40. /* to find the value of v from the following
expression:1/v =1/u +1/f : where values of u and f can be accepted from the
main method from where it is called.*/
import java.io.*;
class m40
{
void compute(double u, double f)
{
double v= (u*f)/(f+u);
System.out.println("The result is : " +v);
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter values of u and
f");
double u1=Double.parseDouble(input.readLine());
double f1=Double.parseDouble(input.readLine());
m40 ob = new m40();
ob.compute(u1,f1);
}
}
41./*computes the area of a triangle using the hero’s
formula sqrt(s(s-a)(s-b)(s-c) where s =
a+b+c/2*/
import java.io.*;
class m41
{
void areatriangle(double a, double b, double c)
{
double s= (a+b+c)/2;
double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of the triangle is :
" +ar);
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please enter three sides of the
triangle");
double s1=Double.parseDouble(input.readLine());
double s2=Double.parseDouble(input.readLine());
double s3=Double.parseDouble(input.readLine());
m41 ob = new m41();
ob.areatriangle(s1,s2,s3);
}
}
42. /* to convert
obj1 to lowercase and obj2 to uppercase. Display the result.*/
import java.io.*;
class m42
{
void convertcase(String obj1, String obj2)
{
String str1=obj1.toLowerCase();
String str2=obj2.toUpperCase();
System.out.println("The string " +obj1 + " in
lower case is : " +str1);
System.out.println("The string " +obj2 + " in
upper case is : " +str2);
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m42 ob = new m42();
System.out.println("Please enter two strings");
String s1=input.readLine();
String s2=input.readLine();
ob.convertcase(s1,s2);
}
}
43. /* that accepts two strings as parameters, compares
them exactly and
returns true of they match and false if they don’t
match*/
import java.io.*;
class m43
{
boolean comparison(String str1, String str2)
{
if(str1.equals(str2))
return true;
else
return false;
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m43 ob = new m43();
System.out.println("Please enter two strings");
String s1=input.readLine();
String s2=input.readLine();
boolean v=ob.comparison(s1,s2);
if(v= =true)
System.out.println("The string are same");
else
System.out.println("The string are not same");
}
}
44. /*
that extracts first five characters of a string and middle characters of another string. Compares them and prints
‘EQUAL’ if they match else it print ‘NOT
EQUAL’ */
import java.io.*;
class m44
{ void extract(String
str1, String str2)
{ int l=str2.length();
int m=l/2+1;
int st=(0+m)/2+1;
int end=(m+l)/2+1;
String s1=str1.substring(0,5);
String s2=str2.substring(st,end);
System.out.println("First five characters of
" + str1 + " are " + s1);
System.out.println("Mid five characters of
" + str2 + " are " + s2);
if(s1.equals(s2))
System.out.println("They are equal");
else
System.out.println("They are not equal");
}
public static void main (String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m44 ob = new m44();
System.out.println("Please enter two strings");
String s1=input.readLine();
String s2=input.readLine();
ob.extract(s1,s2);
} }
45. /* to generates and displays n terms of a fibonacci series given as
follows: 0,1,1,2,3,5,8,13……*/
class m45
{
void fibo(int a,int b, int n)
{
System.out.println(a);
System.out.println(b);
int i, c=0;
for(i=3;i<=n;i++)
{ c=a+b;
System.out.println(c);
a=b;
b=c;
} }
public static void main (String args[]) throws IOException
{ BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
m45 ob = new m45();
int a=0;
int b=1;
System.out.println("Enter the limit");
int n=Integer.parseInt(input.readLine());
ob.fibo(a,b,n);
}}
46. //WAP in Java using method input a number less than 100
and convert into words.
import java.io.*;
class m46
{ public static String substr(int num)
{ String st="
";
switch(num)
{
case 1:
st="One" ;
break;
case 2:
st="Two" ;
break;
case 3:
st="Three" ;
break;
case 4:
st="Four" ;
break;
case 5:
st="Five" ;
break;
case 6:
st="Six" ;
break;
case 7:
st="Seven" ;
break;
case 8:
st="Eight" ;
break;
case 9:
st="Nine" ;
break;
}
return st;
}
public static String eng(int n)
{
String
t="", T=" ", st=" ";
int amt=n;
int q=1, j=0;
int value[]=new int[3];
while(q!=0)
{ value[j]=n%10;
q=n/10;
n=q;
j++;
}
switch(value[1])
{
case 0: st=substr(value[0]);
break;
case 1:
switch(value[0])
{
case 1: st="Eleven ";
break;
break;
case 3: st="Thirteen";
break;
case 4: st="Fourteen";
break;
case 5: st="Fifteen";
break;
case 6: st="Sixteen";
break;
case 7: st="Seventeen";
break;
case 8: st="Eighteen";
break;
case 9: st="Nineteen";
break;
}
break;
case 2: t="Twenty ";
st=substr(value[0]);
break;
case 3: t="Thirty ";
st=substr(value[0]);
break;
case 4: t="Forty ";
st=substr(value[0]);
break;
case 5: t="Fifty ";
st=substr(value[0]);
break;
case 6: t="Sixty ";
st=substr(value[0]);
break;
case 7: t="Seventy ";
st=substr(value[0]);
break;
case 8: t="Eighty ";
st=substr(value[0]);
break;
case 9: t="Ninety ";
st=substr(value[0]);
break;
}
T=t.concat(st);
return T;
}
public static void main(String args[]) throws IOException
{
BufferedReader input=
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a numnber to convert");
int n=Integer.parseInt(input.readLine());
String h=eng(n);
System.out.println(h);
}}
49.// to print the fibonacci series and check which are
prime which are not.
import java.io.*;
class m49
{
void prime()
{
int a=1,b=1;
int i,j;
int c;
for(i=3;i<=17;i++)
{ c=a+b;
//System.out.println(c);
a=b;
b=c;
for(j=2;j<c;j++)
{
if(c%j==0)
{
break;
}
}
if(c==j)
System.out.println(c + " is prime");
else
System.out.println(c + " is
not prime");
}
}
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
double t;
int a=1;
int b=1;
int c=0;
lt ob = new m49();
//System.out.println("Enter limit");
//int n=Integer.parseInt(input.readLine());
System.out.println(a);
System.out.println(b);
ob.prime();
}
}
Comments
Post a Comment