You are on page 1of 15

Indus Institute of Technology &

Engineering

JAVA PRACTICAL LIST

1. Write a program to print your name.


import java.io.*;
class njp1
{
public static void main(String arg[])
{
System.out.println("Nikunj Patel");
System.out.println("23/Parimal Bunglows");
System.out.println("Sardara Chowk");
System.out.println("Krishnanagar");
}
}

2. Write a program to find the area of rectangle and


circle.

import java.io.*;
class njp2
{
public static void main(String arg[])throws IOException
{
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);

int ch,r,l,w;
float pi=3.14f;
double area;

System.out.println("Select 1 For Find Area of Circle");

By NikunJ Patel Page 1


System.out.println("Select 2 For Find Area of Rectangle");
System.out.print("Enter Choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("Enter Redious of Circle:");
r=Integer.parseInt(br.readLine());
area=pi*r*r;
System.out.println("Area=" + area);
break;
}

case 2:
{
System.out.print("Enter length of Rectangle:");
l=Integer.parseInt(br.readLine());
System.out.print("Enter Width of Rectangle:");
w=Integer.parseInt(br.readLine());
area=l*w;
System.out.println("Area=" + area);
break;
}
}

}
}

3. Write a Java program that prints all real solutions to


the quadratic equation ax2 + bx + c = 0. Read in a, b, c
and use the quadratic formula. If the discriminant b2 -4ac
is negative, display a message stating that there are no real
solutions.

import java.io.*;
import java.lang.Math.*;

By NikunJ Patel Page 2


class njp3
{
public static void main(String arg[])throws IOException
{
int a,b,c;
double delta,ans1,ans2;

InputStreamReader obj=new InputStreamReader(System.in);


BufferedReader br=new BufferedReader(obj);

System.out.print("Enter Value of a:");


a=Integer.parseInt(br.readLine());

System.out.print("Enter Value of b:");


b=Integer.parseInt(br.readLine());

System.out.print("Enter Value of c:");


c=Integer.parseInt(br.readLine());

delta=b*b-4*a*c;

if(delta<0)
{
System.out.println("There are No Solution");
}
else if(delta==0)
{
System.out.println("There is one solution of equation");
ans1=b/(2*a);
System.out.println("Answer :"+ans1);
}
else
{
System.out.println("Following are two Solution of Equation");
ans1=-b+Math.sqrt(delta)/(2*a);
ans2=-b-Math.sqrt(delta)/(2*a);
System.out.println("Answer 1 :"+ans1);
System.out.println("Answer 2 :"+ans2);
}

By NikunJ Patel Page 3


}
}

4. Write a program to calculate the temperature in


Fahrenheit if given in Celsius and vice versa.

import java.io.*;

class njp4
{
public static void main(String arg[]) throws IOException
{
double f,c;
System.out.println("For Convert Temp in celsius to Fahranhit press 1");
System.out.println("For Convert Temp in Fahranhit to celsius press 2");
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.print("Press Ur Choice:");

int ch;
ch=Integer.parseInt(br.readLine());

switch(ch)
{
case 1:
{
System.out.print("Enter Temp in celsius:");
c=Double.parseDouble(br.readLine());
f=1.8*c+32;
System.out.println("Temp in Fahranhit is : "+f);
break;
}

case 2:
{
System.out.print("Enter Temp in Fahranhit:");
f=Double.parseDouble(br.readLine());
c=(f-32)/1.8;
System.out.println("Temp in celsius is :"+c);

By NikunJ Patel Page 4


break;
}
default:
{
System.out.println("Wrong Chice");
}
}

}
}

5. Write a program to check whether the no is odd or


even.

import java.io.*;

class njp5
{
public static void main(String arg[]) throws IOException
{
int no;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);

System.out.print("Enter Number:");
no=Integer.parseInt(br.readLine());

if(no%2==0)
{
System.out.println("Number is Even");
}
else
{
System.out.println("Number is Odd");
}

}
}

By NikunJ Patel Page 5


6. Write a program to check whether the year is leap year
or not.
import java.io.*;

class njp6
{
public static void main(String arg[]) throws IOException
{
int year;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);

System.out.print("Enter Year:");
year=Integer.parseInt(br.readLine());

if((year%4==0 && year%100!=0) || (year%400==0))


{
System.out.println(year +" is Leap Year");
}
else
{
System.out.println(year +" is not Leap Year");
}

}
}

7. Write a program to make the calculator operations


using switch…case statement.
import java.io.*;

class njp7
{
public static void main(String arg[])throws IOException
{

int a,b,ans;

By NikunJ Patel Page 6


InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);

System.out.println("Enter The Value of First Operand:");


a=Integer.parseInt(br.readLine());

System.out.println("Enter The Value of Second Operand:");


b=Integer.parseInt(br.readLine());

System.out.print("Enter Operator:");

char op;
op=(char)br.read();

switch(op)
{
case'+':
{
ans=a+b;
System.out.println("Answer="+ans);
break;
}

case '-':
{
ans=a-b;
System.out.println("Answer="+ans);
break;
}

case '*':
{
ans=a*b;
System.out.println("Answer="+ans);
break;

By NikunJ Patel Page 7


}

case '/':
{
ans=a/b;
System.out.println("Answer="+ans);
break;
}

case '%':
{
ans=a%b;
System.out.println("Answer="+ans);
break;
}

default:
{
System.out.println("Wrong Choice");
}
}

}
}
8. Write a program to print the Fibonacci series up to n
numbers.
import java.io.*;
class njp8
{
public static void main(String arg[])throws IOException
{
int i,n,a,b,c;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("Eneter How many no?:");
n=Integer.parseInt(br.readLine());

By NikunJ Patel Page 8


a=0;b=1;
System.out.println(a);
System.out.println(b);

for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
System.out.println(c);
}

}
}
9. Display all the prime numbers within a given range.
import java.io.*;
class njp9
{
public static void main(String arg[])throws IOException
{
int start,end;
InputStreamReader obj=new InputStreamReader (System.in);
BufferedReader br=new BufferedReader(obj);

System.out.print("Enter Starting Number:");


start=Integer.parseInt(br.readLine());

System.out.print("Enter Ending Number:");


end=Integer.parseInt(br.readLine());

for(int j=start;j<end;j++)
{
boolean prime=true;

for(int i=2;i<j/2;i++)
{
if(j%i==0)

By NikunJ Patel Page 9


{
prime=false;
break;
}

}
if (prime)
{
System.out.println(j +" is Prime Number");
}

}
}
}
10.Write a program to calculate the square and cube of
the given number until the user enters the negative no.

11.Write a program to print the following pattern:

*
**
***
****
*****

import java.io.*;;

class njp11
{
public static void main(String arg[])
{
int n,j,p,no=5;
for(n=0;n<no;n++)
{
for(p=no-1;p>n;p--)
{
System.out.print(" ");

By NikunJ Patel Page 10


}

for(j=0;j<=n;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

12. Write a Java program to multiply two given


matrices.
import java.io.*;

class njp12

public static void main(String arg [])throws IOException

int i,j;

int a[][]=new int[3][3];

int b[][]=new int[3][3];

int c[][]=new int[3][3];

InputStreamReader obj=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(obj);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

By NikunJ Patel Page 11


{

System.out.print("Enter the element a["+i+"]["+j+"] of Matrix A:");

a[i][j]=Integer.parseInt(br.readLine());

System.out.println();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print("Enter the element b["+i+"]["+j+"] of Matrix B:");

b[i][j]=Integer.parseInt(br.readLine());

for(i=0;i<3;i++)

for(j=0;j<3;j++)

for(int k=0;k<3;k++)

By NikunJ Patel Page 12


{

c[i][j]=c[i][j]+a[i][k]*b[k][j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(" "+c[i][j]);

System.out.println();

13. Write a Java program that checks whether a given


string is a palindrome or not. Ex: MADAM is a
palindrome.
import java.io.*;

By NikunJ Patel Page 13


class prg45

public static void main(String arg[])throws IOException

int i=0;

InputStreamReader obj=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(obj);

System.out.println("Enter Character:");

char s[]=new char[10];

do

s[i]=(char)br.read();

i++;

}while(s[i-1]!='z');

int len=i-1;

int j;

boolean njp=true;

for(i=0,j=len-1;i<len;i++,j--)

By NikunJ Patel Page 14


if(s[i]!=s[j])

njp=false;

break;

if(njp==true)

System.out.println("String is PALANDROM");

else

System.out.println("String is not PALINDROM");

By NikunJ Patel Page 15

You might also like