You are on page 1of 13

Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal

Lab File
On
Operating System
(MCA-206)
Submitted To: Submitted By:
Dharmendra Dangi Name: Aditya Sharma
Enroll. No: 0103CA211038

LAKSHMI NARAIN COLLEGE OF TECHNOLOGY, MCA, BHOPAL


INDEX
S. No. List of Experiment Page
No.
1 Write a Java program to determine maximum from given 100 numbers

2 Write a Java program to calculate the factorial of a given numbers.

3 Java program to check whether a given character is alphabet or not

4 Java program to find sum of all digits.

5 Write a Java program to add two binary numbers.

6 Write a Java program for switch statement.

7 Write a Java program to print perfect numbers.

8 Write a Java program to convert a decimal number to binary number.

9 Write a Java program for Parametrized Constructor.

10 Write a Java program using while loop, do while loop, “for” loop.

11 Write a Java program to check whether number is Armstrong or not.

12 Write a Java program for Hierarchical Inheritance.

13 Write a Java program for abstract class and for interface.


Write a Java program to declare, initialize and display the contents of an
14 array of 5 integer values. Also show in Java how the length of array can be
found.
Write a program to accept a string and count total capital and small
15 letters in string.
Write a Java program to print following output:
16
0,1,1,2,3……... (20 such items)

17 Write a Java program for method overloading and for method overriding.

18 Write a Java program to design a class Student that has three data
member name ; Roll no; Marks in five subject and member function to
assign streams on the basis of table given below
AverageMarks Stream
1) 90% or more Computer
2) 80-90% Electronics
3) 75-80% Mechanical
4) 70-75% Chemical
5) 60-70% Civil
Q1. Write a Java program to determine maximum from given 100 numbers

Ans – import java.util.Scanner; public class one { public static void main(String[]

args) {

System.out.println("Enter 100 numbers"); Scanner sc =

new Scanner(System.in); int temp; int maxi = -100000;

for (int i=0; i<100; i++) { temp = sc.nextInt();

maxi = Math.max(maxi, temp);


}

System.out.println("Maximum of these is " + maxi);

}
}
Output:

Q2. Write a Java program to calculate the factorial of a given numbers.

Ans – import java.util.Scanner;

public class two { public static void main(String[] args) { Scanner


sc = new Scanner(System.in);

System.out.println("Enter the number to find factorial");

int n = sc.nextInt(); int fact = 1; for (int i=2; i<=n; i+

+) fact *= i;

System.out.println("Factorial is " + fact);


}
}

Output:
Q3. Java program to check whether a given string is numeric or not

Ans – import java.util.Scanner; public class three {

public static void main(String[] args) { Scanner sc = new

Scanner(System.in); String str = sc.nextLine();

for (int i=0; i<str.length(); i++) { if (!Character.isDigit(str.charAt(i))) {


System.out.println("Its not a number"); return;
}
}

System.out.println("its a number");
}
}
Output:

Q4. Java program to find sum of all digits.

Ans –

public class four { public static void main(String[] args) { int sum =

0;

for (int i=0; i<args.length; i++) {


sum += Integer.parseInt(args[i]);
}

System.out.println("Sum is " + sum);

}
}

Output:
Q5. Write a Java program to add two binary numbers.

Ans – import java.util.Scanner;

public class five { public static void main(String[] args) { Scanner


sc = new Scanner(System.in);
String a = sc.next(); String b = sc.next(); int sum = Integer.parseInt(a, 2) +

Integer.parseInt(b, 2);

System.out.println(Integer.toBinaryString(sum)); }
}

Output:
Q6. Write a Java program for switch statement. Ans –

import java.util.Scanner; public class six { public static void

main(String[] args) {

Scanner sc = new Scanner(System.in); int num =


sc.nextInt();

switch (num & 1) {


case 0:
System.out.println("Even"); break;

case 1:
System.out.println("Odd"); break;

default:
break;
}
}

Output:

Q7. Write a Java program to print perfect numbers.

Ans –

/**
* seven
*/ import java.util.Scanner; public

class seven {

public static void main(String[] args) { long n, sum = 0;


Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: "); n =
sc.nextLong(); int i = 1; while (i <= n / 2) { if (n % i == 0)
{ sum = sum + i;
} i++;
}
if (sum == n) {
System.out.println(n + " is a perfect number.");
} else
System.out.println(n + " is not a perfect number."); }
}
Output:

Q8. Write a Java program to convert a decimal number to binary number.

Ans – import java.util.Scanner;

public class eight { public static void main(String[] args) { Scanner


sc = new Scanner(System.in); System.out.println("Enter a
number"); int n = sc.nextInt();

String res = Integer.toBinaryString(n);

System.out.println("Binary is " +res);


}

Output:

Q9. Write a Java program for Parametrized Constructor.

Ans – import java.util.Scanner;

public class nine { public static void main(String[] args) { Scanner


sc = new Scanner(System.in);
System.out.println("Enter name & age of person"); String name =
sc.nextLine(); int age = sc.nextInt();

Person p = new Person(name, age); p.display();


}}

class Person { String name;


int age;
Person(String name, int age) { this.name =
name; this.age = age;
}

void display() {
System.out.println("The person " + name + " is " + age + " year old");
}
}
Output:

Q10. Write a Java program using while loop, do while loop, “for” loop.

Ans – public class ten { public static void main(String[] args) { int
n = 10;

System.out.println("\nFor loop demo"); for (int i=0; i<n; i+


+) {
System.out.print(i);
}

System.out.println("\nWhile loop demo");


int i = 0;
while (i<n) {
System.out.print(i++);
}

System.out.println("\nDo while demo");


i = 0; do {
System.out.print(i++);
} while (i<n);
}
}
Output:

Q11. Write a Java program to check whether number is Armstrong or not.

Ans –

import java.util.Scanner; import


java.lang.Math;

public class eleven { static boolean isArmstrong(int n) { int temp, digits


= 0, last = 0, sum = 0; temp = n; while (temp > 0) { temp = temp /
10;
digits++;
}
temp = n; while (temp > 0) { last = temp % 10; sum
+= (Math.pow(last, digits)); temp = temp / 10;
}
if (n == sum) return true;
else return false;
}

public static void main(String args[]) { int num;


Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: "); num =
sc.nextInt(); if (isArmstrong(num))
System.out.print(num + " is an armstrong number"); else
System.out.println(num + " is not an armstrong number"); }
}

Output:

Q12. Write a Java program for Hierarchical Inheritance.

Ans –

class Animal {
void eat() {
System.out.println("eating...");
}
}

class Dog extends Animal {


void bark() {
System.out.println("barking...");
}
}

class Cat extends Animal {


void meow() {
System.out.println("meowing...");
}}

class twelve {
public static void main(String args[]) { Cat c = new Cat();
c.meow();
c.eat();
}
}
Output:

Q13. Write a Java program for abstract class and for interface.

Ans –

abstract class Shape { abstract void draw();


}

class Rectangle extends Shape {


void draw() {
System.out.println("drawing rectangle");
}}

class Circle extends Shape {


void draw() {
System.out.println("drawing circle");
}}

class thirteen {
public static void main(String args[]) { Shape s = new
Circle(); s.draw();
Shape sh = new Rectangle(); sh.draw();
}
}

Output:

Q14. Write a Java program to declare, initialize and display the contents of an array of 5 integer values. Also

show in Java how the length of array can be found. Ans – public class fourteen {

public static void main(String[] args) {


// declaration with initialization
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

System.out.println("Length of array is " + arr.length);

// iteration over array

for (int i : arr) {


System.out.println(i);
}
}
}

Output:

Q15. Write a program to accept a string and count total capital and small letters in string.

Ans – import java.util.Scanner; public class fifteen {

public static void main(String[] args) { Scanner sc = new


Scanner(System.in); String str = sc.nextLine();

int l = 0; int u = 0;

for (int i=0; i<str.length(); i++) { char ch =


str.charAt(i); if (Character.isUpperCase(ch))
{ u++;
} else { l++;
}
}

System.out.println("Upper Case letters = " +u);


System.out.println("Lower case letters = " +l); }

Output:

Q16. Write a Java program to print following output:


0,1,1,2,3……... (20 such items)

Ans –

public class sixteen {


public static void main(String[] args) {

int n = 20, a = 0, b = 1, c;

System.out.print("0 1 ");

for (int i = 2; i <= n; i++) {


c = a + b; a =
b; b = c;
System.out.print(c + " ");
}

Output:

Q17. Write a Java program for method overloading and for method overriding.
Ans –

/**
* seventeen
*/ class Greet {

void display () {
System.out.println("Hello");
}

// overloading display function void display


(String name) {
System.out.println("Hello " + name);
}

} class World extends Greet {

// overriding display function void display () {


System.out.println("Hello World");
} } public class seventeen { public static void main(String[]

args) {

Greet g1 = new Greet();


Greet g2 = new World(); g1.display();
g1.display("Akshat"); g2.display();
}
}

OUTPUT:

Q18. Write a Java program to design a class Student that has three data member name ; Roll no; Marks in
five subject and member function to assign streams on the basis of table given below Ans -
public class eighteen { public static void main(String[] args) {
Student s = new Student("Akshat", 14); s.assignStream(60);

System.out.println(s.stream);
} } class Student

String name; int roll;


String stream;

Student (String name, int roll) { this.name =


name; this.roll = roll; this.stream = "";
}

void assignStream (int m) { if (m >= 90) {


this.stream = "computer";
} else if (m >= 80) {
this.stream = "electronics";
} else if (m >= 75) {
this.stream = "mechanical";
} else if (m >= 70) {
this.stream = "chemical";
} else if (m >= 60) {
this.stream = "civil";
} else { this.stream = "NA";
}
}
}

OUTPUT:

You might also like