Wednesday, August 18, 2010

Object Oriented Programming (OOP) Lab Aug 18th

First Program By me and Mr.Ashish

Fibonacci and Factorial
==================


import java.util.Scanner;

import java.io.*;

class fact
{
int i,f=0,s=1,third,fact=1,n;

int face(int n)
{

if(n==0)
{
return(fact);
}
else
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}



void fibbo(int n)
{
System.out.println("\n\n\n"+f+"\n\n"+s);

for(i=0;i
{

third=f+s;

f=s;

s=third;

System.out.println("\n\n\n"+third);

}
}

}


class demofact
{
public static void main(String args[])
{
int n,x,choice;


Scanner in=new Scanner(System.in);

while(true)
{
System.out.println("\n\nENTER THE CHOICE...\n\n\n1.FACTORIAL...\n\n\n2.FIBBONACI....\n\n\n3.EXIT");
choice=in.nextInt();
switch(choice)
{
case 1:

System.out.println("\n\n\nENTER THE NUMBER...... ");
n=in.nextInt();
fact ob1=new fact();
System.out.println("\n\n\nThe factorial is "+ob1.face(n));
break;

case 2:
System.out.println("\n\n\nENTER THE NUMBER...... ");
n=in.nextInt();

fact ob2=new fact();
ob2.fibbo(n);
break;

case 3: break;

}
}
}
}


This program however contains a small bug....When U will try to exit the program, it wont. For exiting u can use any alphabet like a,b.... which will generate an exception and then u can exit. We will solve the problem quickly.


Palindrome by Arka "MBPS" Mukherjee a.k.a "Java.lang"
===========================================


import java.io.*;
public class palindrome
{
public static void main(String args[])throws IOException
{


char c[]=new char[20];
char b[]=new char[20];
int a;
int n;
String s;
boolean ispalindrome=true;
System.out.println("enter the STRING");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
c=s.toCharArray();
n=s.length();
for(int i=n-1;i>=0;i-- )
{
b[n-i-1]=c[i];
}
for(int i=0;i
{
if(b[i]!=c[i])
{
System.out.println(" Not Palindrome");
ispalindrome=false;
break;
}
}
if(ispalindrome)
System.out.println("It is Palindrome");


}
}


No comments:

Post a Comment

Featured Post 1

notice

Featured Post 2

IETE

Featured Post 6

results

Featured Post 7

WBUT

Featured Post 3

Featured Post 4

Featured Post 5

Featured Post 8