0% found this document useful (0 votes)
24 views10 pages

Java Programming Lab Manual

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)
24 views10 pages

Java Programming Lab Manual

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

JAVA PROGRAMMING LAB MLRITM

Week-1

Aim:Use Eclipse or Net bean platform and acquaint with the various menus. Create a
test project, add a test class, and run it. See how you can use auto suggestions, auto
fill. Try code formatter and code refactoring like renaming variables, methods,
and classes. Try debug step by step with a small program of about 10 to 15 lines
which contains at least one if else condition and a for loop.

Aim: Write a java program to find the factorial of the number.

Program:

class Fact
{
public static void main(String[] args)
{
int number = 5;
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
[Link]("Factorial of a number is:" + factorial);
}
}

Output: Factorial of a number is:120

12
JAVA PROGRAMMING LAB MLRITM

Aim :Write a java program to display Fibonacci series .

Program:

import [Link];
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner([Link]);
[Link]("Enter value of n:");
n = [Link]();
[Link]("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
[Link](a+" ");
}

}
}

Output:

Enter value of n:5


Fibonacci Series:01123

13
JAVA PROGRAMMING LAB MLRITM

Aim :Write a java program to find whether the give number is Armstrong number or not.

Program:
import [Link].*;
import [Link].*;
class ArmStrong
{
public static void main(String[] args)throws IOException
{
int t,s=0,n,r;
[Link]("Enter number:");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
n=[Link]([Link]());
t=n;
while( t!=0)
{
r=t%10;
s=s+r*r*r;
t=t/10;
}
if(n==s)
[Link]("Number given is Armstrong");
else
[Link]("Number given is not Armstrong");
}
}

Output:

Enter number:153
Number given is Armstrong

14
JAVA PROGRAMMING LAB MLRITM

Aim :Write a java program to reverse the given string.

Program:
import [Link].*;
import [Link].*;
class RevStr
{
public static void main(String[] args) throws IOException
{
String or, rev="";
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter String:");
or=[Link]();
int len=[Link]();
for(int i=len-1;i>=0;i--)
{
rev=rev+[Link](i);
}
[Link]("Reverse of a string:"+rev);
}
}

Output:

Enter String:siva
Reverse of a string:avis

15
JAVA PROGRAMMING LAB MLRITM

Aim: Write a java program to evaluate quadratic equation


Program:
import [Link].*;
class QudEq
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int a,b,c;
double x,y,z;
[Link]("ENTER a VALUE:");
br= new BufferedReader(new InputStreamReader([Link]));
a=[Link]([Link]());
[Link]("ENTER b VALUE:");
b=[Link]([Link]());
[Link]("ENTER c VALUE:");
c=[Link]([Link]());
z=[Link](b*b-4*a*c);
x=(-b+[Link](b*b-4*a*c))/(2*a);
y=(-[Link](b*b-4*a*c))/(2*a);
if(z>0)
{
[Link]("THE REAL SOLUTIONS ARE ="+x+"\t"+"Y="+y);
}
else
{
[Link]("THERE ARE NO REAL SOLUTIONS");
}
}
}

Output:
Enter a value:
4
Enter b value:
6
Enter c value:
-2
THE REAL SOLUTIONS ARE X=0.28077640640441515 Y==-1.7807764064044151

16
JAVA PROGRAMMING LAB MLRITM

Aim :Write a java program to print Fibonacci series for the given number using recursion

Program:
import [Link].*;
public class FibRec
{
static int fib(int i)
{
if(i==1||i==2)
{
return i-1;
}
else
{
return fib(i-1)+fib(i-2);
}
}

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


{
BufferedReader br;
int n;
[Link]("enter n value:");
br = new BufferedReader(new InputStreamReader([Link]));
n=[Link]([Link]());
[Link]("FIBONACCI SEQUENCE:");
for(int i=2;i<=n+1;i++)
{
[Link](fib(i)+"\t");
}
}
}
Output:
enter n value:
5
FIBNOCCI SEQUENCE:1 1 2 3 5

17
JAVA PROGRAMMING LAB MLRITM

Aim: Write a java program to print Fibonacci series for the given number using non-recursion
Program:
import [Link].*;
class FibNonRec
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int a=0;
int b=1,t,n;
br=new BufferedReader(new InputStreamReader([Link]));
[Link]("enter n value:");
n=[Link]([Link]());
[Link]("FIBONACCI SEQENCE IS:");
[Link](a+"\t");
do
{

[Link](b+"\t");
t=b;
b=a+b;
a=t;
}while(b<=n);
}
}

Output:
enter n value:
5
FIBNOCCI SEQUENCE IS: 0 1 1 2 3 5

18
JAVA PROGRAMMING LAB MLRITM

Aim: Write a java program to count number of words in a given text


Program:
import [Link].*;
import [Link].*;
class CountWords
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a string : ");
String str=[Link]();
int count=0;
if([Link]()==0)
{
[Link]("[Link] words in given text:" + count);
}
else
{
for(int i=0;i<[Link]();i++)
{
if([Link](i)==' ')
{
count++;
}
}
[Link]("[Link] words in given text:" + (count+1));
}
}
}
Output:
Enter a string:
hi this is siva
[Link] words in given text:
4

19
JAVA PROGRAMMING LAB MLRITM

Aim: Write a java program to check the given string is palindrome or not
Program:
import [Link].*;
public class Palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter String:");
String str=[Link]();
char ch[]=new char[[Link]()];
for(int i=[Link]()-1,j=0;i>=0;i--,j++)
ch[j]=[Link](i);
String restr=new String(ch);
[Link]("Reverse of String "+str+" is "+restr);
if([Link](restr)==0)
[Link](str+" "+"is a Palindrome");
else
[Link](str+" "+"is not a Palindrome");
}
}
Output:
Enter String:
madam
Reverse of String madam is madam
madam is a Palindrome

20
JAVA PROGRAMMING LAB MLRITM

Aim: Write a java program to print the prime numbers upto nth number
Program:
import [Link].*;
class Primenos
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int i,t,flag,n;
br=new BufferedReader(new InputStreamReader([Link]));
[Link]("ENTER n VALUE:");
n=[Link]([Link]());
[Link]("PRIME NUMBERS UP TO"+" "+n+":");
for(i=2;i<=n;i++)
{
flag=1;
for(t=2;t<i;t++)
{
if(i%t==0)
{
flag=0;
break;
}
}
if(flag==1)
[Link](i);
}
}
}
Output:
ENTER n VALUE:
5
PRIME NUMBERS UP TO 5:
2
3
5

21

You might also like