0% found this document useful (0 votes)
83 views11 pages

Java Programming ETCS-357: Maharaja Agrasen Institute of Technology, PSP Area, Sector - 22, Rohini, New Delhi - 110085

This document contains code for 3 Java programming experiments: 1. A program to calculate the factorial of a number using a for loop. 2. A program to check if a number is a palindrome by reversing it and comparing to the original. 3. Programs implementing stack and queue data structures with methods like push, pop, peek, empty, size for stack and enqueue, dequeue, peek for queue.

Uploaded by

Ritwik
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)
83 views11 pages

Java Programming ETCS-357: Maharaja Agrasen Institute of Technology, PSP Area, Sector - 22, Rohini, New Delhi - 110085

This document contains code for 3 Java programming experiments: 1. A program to calculate the factorial of a number using a for loop. 2. A program to check if a number is a palindrome by reversing it and comparing to the original. 3. Programs implementing stack and queue data structures with methods like push, pop, peek, empty, size for stack and enqueue, dequeue, peek for queue.

Uploaded by

Ritwik
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
You are on page 1/ 11

JAVA PROGRAMMING

ETCS-357

Faculty name: Dr. Jyoti Sharma Student name: Utkarsh Pandey


Roll No.: 40314802717
Semester: 5th

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi – 110085
Index

Exp. Experiment Name Date of Date of Remarks Signature


no performance checking
EXPERIMENT No. 1
AIM: Write a java program to find factorial of a number
CODE:
import java.util.*;
public class Factorial
{ public static void main(String[] args)
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int a=sc.nextInt();
int temp=1;
for(int i=a;i>0;i--)
{ temp=temp*i;
}
System.out.print("Factorial of this number is: ");
System.out.print(temp);
}
}

OUTPUT:
EXPERIMENT NO. 2
AIM: Write a java program to check whether a given number is palindrome or not
CODE:
import java.util.*;
public class Palindrome
{
public static void main(String[] args)
{
int num,sum=0,t,remain;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
num=sc.nextInt();
t=num;
while(num>0)
{
remain=num%10;
sum=(sum*10)+remain;
num=num/10;
}
if(t==sum)
System.out.println("This is a palindrome number ");
else
System.out.println("This is not a palindrome number");
}
}
OUTPUT:
EXPERIMENT NO. 3
AIM: Create a java program to implement stack and queue concept.
CODE:
//Implementing Stack Concept
import java.util.*;
class Stack1
{ private int ar[],top,capacity;
Stack1(int size)
{ ar = new int[size];
capacity = size;
top = -1;
}
public void push(int x)
{ if (Full())
{ System.out.println("OverFlow\nProgram Terminated\n");
System.exit(1);
}
System.out.println("Inserting " + x);
ar[++top] = x;
}
public int pop()
{ if (Empty())
{ System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
}
System.out.println("Removing " + peek());
return ar[top--];
}
public int peek()
{ if (!Empty())
return ar[top];
else
System.exit(1);
return -1;
}
public int size()
{ return top + 1;
}
public Boolean Empty()
{ return top == -1;
}
public Boolean Full()
{ return top == capacity - 1;
}
public static void main (String[] args)
{ Stack1 stack = new Stack1(3);
stack.push(1);
stack.push(2);
stack.pop();
stack.pop();
stack.push(3);
System.out.println("Top element is: " + stack.peek());
System.out.println("Stack size is " + stack.size());
stack.pop();
if (stack.Empty())
System.out.println("Stack Is Empty");
else
System.out.println("Stack Is Not Empty");

}
}
//Implementing Queue Concept
import java.util.*;
class queue
{ private int ar[],front,rear,capacity,cn;
queue(int size)
{ ar = new int[size];
capacity = size;
front = 0;
rear = -1;
cn = 0;
}
public void dequeue()
{ if (Empty())
{ System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
}
System.out.println("Removing " + ar[front]);
front = (front + 1) % capacity;
cn--;
}
public void enqueue(int item)
{ if (Full())
{ System.out.println("OverFlow\nProgram Terminated");
System.exit(1);
}
System.out.println("Inserting " + item);
rear = (rear + 1) % capacity;
ar[rear] = item;
cn++;
}
public int peek()
{ if (Empty())
{ System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
}
return ar[front];
}
public int size()
{ return cn;
}
public Boolean Empty()
{ return (size() == 0);
}
public Boolean Full()
{ return (size() == capacity);
}
public static void main (String[] args)
{ queue q = new queue(5);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println("Front element is: " + q.peek());
q.dequeue();
System.out.println("Front element is: " + q.peek());
System.out.println("Queue size is " + q.size());
q.dequeue();
q.dequeue();
if (q.Empty())
System.out.println("Queue Is Empty");
else
System.out.println("Queue Is Not Empty");

}
}
OUTPUT:

You might also like