You are on page 1of 7

OOC ASSIGNMENT

Name:Vishal Koul

USN:1BG19CS130

1.Write a Java Program to find Armstrong of a given number.


import java.util.Scanner;
class Armstrong
{
public static void main(String[] arg)
{
int a,arm=0,n,temp;
Scanner sc =new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
temp=n;
for( ;n!=0;n/=10 )
{
a=n%10;
arm=arm+(a*a*a);
}
if(arm==temp)
System.out.println(temp+" is a armstrong number ");
else
System.out.println(temp+" is not a armstrong number ");
}
}

Output:
Enter a number
345
345 is not a armstrong number

2.Write a Java Program to generate prime number from 1 to N.


import java.util.Scanner;
class Prime
{
public static void main(String arg[])
{
int i,count;
System.out.print("Enter n value : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("Prime numbers between 1 to "+n+" are ");
for(int j=2;j<=n;j++)
{
count=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
{
count++;
}
}
if(count==2)
System.out.print(j+" ");
}
}
}

Output:
Enter n value :100
Prime numbers between 1 to 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

3.Write a Java Program to reverse a string in Java.


import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of the string: " + reverse);
}
}
Output:
Enter a string to reverse
SUN RISES IN THE EAST
Reverse of the string: TSAE EHT NI SESIR NUS
4.Write a Java Program to implement queues.
import java.util.*;
class Queue
{
int arr[], front, rear, cap, n1;
Queue(int n)
{
arr = new int[n];
cap = n;
front = 0;
rear = -1;
n = 0;
}
public void dequeue()
{
if (isEmpty())
{
System.out.println("No items in the queue,cannot delete");
System.exit(1);
}
System.out.println("Deleting " + arr[front]);
front = (front + 1) % cap;
n1--;
}
public void enqueue(int val)
{
if (isFull())
{
System.out.println("OverFlow!!Cannot add more values");
System.exit(1);
}
System.out.println("Adding " + val);
rear = (rear + 1) % cap;
arr[rear] = val;
n1++;
}
public int peek()
{
if (isEmpty())
{
System.out.println("Queue empty!!Cannot delete");
System.exit(1);
}
return arr[front];
}
public int size()
{
return n1;
}
public Boolean isEmpty()
{
return (size() == 0);
}
public Boolean isFull()
{
return (size() == cap);
}
public static void main (String[] args)
{
Queue q = new Queue(5);
q.enqueue(20);
q.enqueue(40);
q.enqueue(60);
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.isEmpty())
System.out.println("Queue Is Empty");
else
System.out.println("Queue Is Not Empty"): }}

Output:
Adding 20
Adding 40
Adding 60
Front Element is:20
Deleting 20
Front Element is:40
Queue size is 2
Deleting 40
Deleting 60
Queue is empty

5.Write a Java Program to implement the functions of a calculator.


import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
float a, b, res;
char choice, ch;
Scanner scan = new Scanner(System.in);

do
{
System.out.print("1. Addition\n");
System.out.print("2. Subtraction\n");
System.out.print("3. Multiplication\n");
System.out.print("4. Division\n");
System.out.print("5. Exit\n\n");
System.out.print("Enter Your Choice : ");
choice = scan.next().charAt(0);
switch(choice)
{
case '1' : System.out.print("Enter Two Numbers : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a + b;
System.out.print("Result = " + res);
break;
case '2' : System.out.print("Enter Two Numbers : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a - b;
System.out.print("Result = " + res);
break;
case '3' : System.out.print("Enter Two Numbers : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a * b;
System.out.print("Result = " + res);
break;
case '4' : System.out.print("Enter Two Numbers : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a / b;
System.out.print("Result = " + res);
break;
case '5' : System.exit(0);
break;
default : System.out.print("Invalid Choice!!!");
break;
}
System.out.print("\n");
}while(choice != 5);
}
}

Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 1


Enter Two Numbers : 2
4
Result = 6.0
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 2


Enter Two Numbers : 56
28
Result = 28.0
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 3


Enter Two Numbers : 7
9
Result = 63.0
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 4


Enter Two Numbers : 99
11
Result = 9.0
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 5

You might also like