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;
}
System.out.println("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 java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(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 java.io.*;
import java.util.*;
class ArmStrong
{
public static void main(String[] args)throws IOException
{
int t,s=0,n,r;
System.out.println("Enter number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
t=n;
while( t!=0)
{
r=t%10;
s=s+r*r*r;
t=t/10;
}
if(n==s)
System.out.println("Number given is Armstrong");
else
System.out.println("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 java.io.*;
import java.util.*;
class RevStr
{
public static void main(String[] args) throws IOException
{
String or, rev="";
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter String:");
or=dis.readLine();
int len=or.length();
for(int i=len-1;i>=0;i--)
{
rev=rev+or.charAt(i);
}
System.out.println("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 java.io.*;
class QudEq
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int a,b,c;
double x,y,z;
System.out.println("ENTER a VALUE:");
br= new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
System.out.println("ENTER b VALUE:");
b=Integer.parseInt(br.readLine());
System.out.println("ENTER c VALUE:");
c=Integer.parseInt(br.readLine());
z=Math.sqrt(b*b-4*a*c);
x=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
y=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
if(z>0)
{
System.out.println("THE REAL SOLUTIONS ARE ="+x+"\t"+"Y="+y);
}
else
{
System.out.println("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 java.io.*;
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;
System.out.println("enter n value:");
br = new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
System.out.print("FIBONACCI SEQUENCE:");
for(int i=2;i<=n+1;i++)
{
System.out.print(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 java.io.*;
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(System.in));
System.out.println("enter n value:");
n=Integer.parseInt(br.readLine());
System.out.print("FIBONACCI SEQENCE IS:");
System.out.print(a+"\t");
do
{
System.out.print(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 java.io.*;
import java.util.*;
class CountWords
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
String str=br.readLine();
int count=0;
if(str.length()==0)
{
System.out.println("No.of words in given text:" + count);
}
else
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')
{
count++;
}
}
System.out.println("No.of words in given text:" + (count+1));
}
}
}
Output:
Enter a string:
hi this is siva
No.of 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 java.io.*;
public class Palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String:");
String str=br.readLine();
char ch[]=new char[str.length()];
for(int i=str.length()-1,j=0;i>=0;i--,j++)
ch[j]=str.charAt(i);
String restr=new String(ch);
System.out.println("Reverse of String "+str+" is "+restr);
if(str.compareTo(restr)==0)
System.out.println(str+" "+"is a Palindrome");
else
System.out.println(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 java.io.*;
class Primenos
{
public static void main(String args[])throws IOException
{
BufferedReader br;
int i,t,flag,n;
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER n VALUE:");
n=Integer.parseInt(br.readLine());
System.out.println("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)
System.out.println(i);
}
}
}
Output:
ENTER n VALUE:
5
PRIME NUMBERS UP TO 5:
2
3
5
21