I would appreciate if the code be given in Java if possible for the 3 questions.
ID: 3871555 • Letter: I
Question
I would appreciate if the code be given in Java if possible for the 3 questions.
Explanation / Answer
1)
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
System.out.print("Fibonacci Series:");
for(int i = 1; i <= 100; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}
public class Fibonacci {
// Don't check for negative number for simplicity
public static int fibonacci(int n) {
if (n < 2) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
public static void main(String[] args) {
int i = 0;
int fn;
while ((fn = fibonacci(i)) < 100) {
System.out.print(fn + " ");
i++;
}
}
}
import java.util.Scanner;
class Fibonacci
{
public static void main(String[] args)
{
int i,no, first=0, second=1, next;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of terms for Series: ");
no=s.nextInt();
first=0;
second=1;
System.out.println("Fibonacci series are: ");
for(i=0; i<no; i++)
{
System.out.println(first);
next = first + second;
first = second;
second = next;
}
}
}
2)
import java.util.Scanner;
import java.util.Stack;
public class InfixToPostfix {
private boolean isOperator(char c){
if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^')
return true;
return false;
}
private boolean checkPrecedence(char c1, char c2){
if((c2 == '+' || c2 == '-') && (c1 == '+' || c1 == '-'))
return true;
else if((c2 == '*' || c2 == '/') && (c1 == '+' || c1 == '-' || c1 == '*' || c1 == '/'))
return true;
else if((c2 == '^') && (c1 == '+' || c1 == '-' || c1 == '*' || c1 == '/'))
return true;
else
return false;
}
/**
* Converts infix expression to postfix
* @param infix infix expression to be converted
* @return postfix expression
*/
public String convert(String infix){
System.out.printf("%-8s%-10s%-15s ", "Input","Stack","Postfix");
String postfix = ""; //equivalent postfix is empty initially
Stack<Character> s = new Stack<>(); //stack to hold symbols
s.push('#'); //symbol to denote end of stack
System.out.printf("%-8s%-10s%-15s ", "",format(s.toString()),postfix);
for(int i = 0; i < infix.length(); i++){
char inputSymbol = infix.charAt(i);
if(isOperator(inputSymbol)){
//repeatedly pops if stack top has same or higher precedence
while(checkPrecedence(inputSymbol, s.peek()))
postfix += s.pop();
s.push(inputSymbol);
}
else if(inputSymbol == '(')
s.push(inputSymbol);
else if(inputSymbol == ')'){
//repeatedly pops if right parenthesis until left parenthesis is found
while(s.peek() != '(')
postfix += s.pop();
s.pop();
}
else
postfix += inputSymbol;
System.out.printf("%-8s%-10s%-15s ", ""+inputSymbol,format(s.toString()),postfix);
}
//pops all elements of stack left
while(s.peek() != '#'){
postfix += s.pop();
System.out.printf("%-8s%-10s%-15s ", "",format(s.toString()),postfix);
}
return postfix;
}
private String format(String s){
s = s.replaceAll(",","");
s = s.replaceAll(" ","");
s = s.substring(1, s.length()-1);
return s;
}
public static void main(String[] args) {
InfixToPostfix obj = new InfixToPostfix();
Scanner sc = new Scanner(System.in);
System.out.print("Infix : ");
String infix = sc.next();
System.out.print("Postfix : "+obj.convert(infix));
}
}
public static int evalPostfix(String exp) {
int res = 0;
myStack list = new myStack();
int n1; //result of 1st popping
int n2; // result of 2nd popping
for (int i = 0; i < exp.length(); i++) {
char ch = exp.charAt(i);
if (ch == ' ') {
} else {
if (ch > '0' && ch < '9') {
list.push(ch);
// list.printS();
} else {
n1 = Integer.parseInt("" + list.pop());
n2 = Integer.parseInt("" + list.pop());
switch (ch) {
case '+':
list.push(n1 + n2);
break;
case '-':
list.push(n1 - n2);
break;
case '*':
list.push(n1 * n2);
break;
case '/':
list.push(n1 / n2);
break;
default:
System.out.println("Invalid operator order!");
}
}
}
}
res = Integer.parseInt("" + list.pop());
return res;
}
3)
import java.io.*;
import java.lang.*;
import java.util.Scanner;
interface i1 {
void list();
}
interface i2 {
void list();
}
class link implements i1 {
private int top = -1, n = 0;
int data[] = new int[1000];
int link[] = new int[1000];
private int NULL = 0;
public void list() {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
System.out.println("list 1.push 2.pop 3.view 4.return to mainmenu");
n = Integer.parseInt(br.readLine());
if (n == 1) {
System.out.println("enter a no to push");
top++;
data[top] = Integer.parseInt(br.readLine());
link[top] = top + 1;
} else if (n == 2) {
System.out.println("enter the position to be delete");
Scanner in = new Scanner(System.in);
int pos = in.nextInt();
int k = data[pos];
data[pos] = data[pos + 1];
top--;
System.out.println("poped value is" + k);
} else if (n == 3) {
System.out.println("the stack is");
for (int i = 0; i <= top; i++) {
System.out.print(data[i] + "->");
}
System.out.println("top");
} else if (n == 4) {
return;
} else {
System.out.println("enter a correct option");
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class lst implements i2 {
private int top = -1, n = 0;
int lst[] = new int[1000];
public void list() {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
System.out.println("stack 1.push 2.pop 3.view 4. return to main menu");
n = Integer.parseInt(br.readLine());
if (n == 1) {
System.out.println("enter the no to push");
top++;
lst[top] = Integer.parseInt(br.readLine());
} else if (n == 2) {
System.out.println("enter the position to be delete");
Scanner in = new Scanner(System.in);
int pos = in.nextInt();
int k = lst[pos];
lst[pos] = lst[pos + 1];
top--;
System.out.println("poped value is" + k);
} else if (n == 3) {
System.out.println("the stack is");
for (int i = 0; i <= top; i++) {
System.out.println(lst[i]);
}
} else {
System.out.println("enter a correct option");
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class stack1 {
public static void main(String args[]) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
System.out.println("1.stack using linked list 2.stack using list 3.Exit");
int n = Integer.parseInt(br.readLine());
if (n == 1) {
link o1 = new link();
o1.list();
} else if (n == 2) {
lst o2 = new lst();
o2.list();
} else if (n == 3) {
break;
} else {
System.out.println("enter the correct option");
}
}
}
}