IT practical programs
1. Write a Java program to display “Welcome to Java Programming”.
class HelloJava {
public static void main(String[] args) {
[Link]("Welcome to Java Programming");
}
}
2. Write a program to perform addition, subtraction, multiplication, and division of two numbers.
import [Link];
class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Addition = " + (a + b));
[Link]("Subtraction = " + (a - b));
[Link]("Multiplication = " + (a * b));
[Link]("Division = " + (a / b));
}
}
3. Write a program to check whether a given number is even or odd.
import [Link];
class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if (n % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");
}
}
4. Write a program to find the largest of three numbers using if-else.
import [Link];
class LargestThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
if (a > b && a > c)
[Link]("Largest = " + a);
else if (b > c)
[Link]("Largest = " + b);
else
[Link]("Largest = " + c);
}
}
5. Write a program to display the multiplication table of a given number.
import [Link];
class Table {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
for (int i = 1; i <= 10; i++) {
[Link](n + " x " + i + " = " + (n * i));
}
}
}
6. Write a program to find the sum of first N natural numbers.
import [Link];
class SumNatural {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter value of N: ");
int n = [Link]();
int sum = 0, i = 1;
while (i <= n) {
sum = sum + i;
i++;
}
[Link]("Sum = " + sum);
}
}
7. Write a program to store and display elements of an array.
import [Link];
class ArrayDisplay {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[5];
[Link]("Enter 5 elements:");
for (int i = 0; i < 5; i++) {
a[i] = [Link]();
}
[Link]("Array elements:");
for (int i = 0; i < 5; i++) {
[Link](a[i]);
}
}
}
8. Write a program to find the largest element in an array.
import [Link];
class LargestArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[5];
[Link]("Enter 5 elements:");
for (int i = 0; i < 5; i++) {
a[i] = [Link]();
}
int max = a[0];
for (int i = 1; i < 5; i++) {
if (a[i] > max)
max = a[i];
}
[Link]("Largest element = " + max);
}
}
9. Create a Student class with roll number, name, and marks and display the details.
class Student {
int roll;
String name;
int marks;
void input(int r, String n, int m) {
roll = r;
name = n;
marks = m;
}
void display() {
[Link]("Roll: " + roll);
[Link]("Name: " + name);
[Link]("Marks: " + marks);
}
public static void main(String[] args) {
Student s = new Student();
[Link](1, "Amit", 85);
[Link]();
}
}
10. Write a program to demonstrate method overloading by adding two and three numbers.
class Add {
int sum(int a, int b) {
return a + b;
}
int sum(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Add obj = new Add();
[Link]("Sum of two = " + [Link](5, 10));
[Link]("Sum of three = " + [Link](5, 10, 15));
}
}
11. Create a base class Person and a derived class Student. Display student details.
class Person {
String name;
void setName(String n) {
name = n;
}
}
class Student extends Person {
int roll;
void setRoll(int r) {
roll = r;
}
void display() {
[Link]("Name: " + name);
[Link]("Roll: " + roll);
}
public static void main(String[] args) {
Student s = new Student();
[Link]("Rahul");
[Link](10);
[Link]();
}}
12. Write a program to handle division by zero using try-catch.
class DivideException {
public static void main(String[] args) {
try {
int a = 10 / 0;
[Link](a);
} catch (ArithmeticException e) {
[Link]("Division by zero not allowed");
}
}
}
13. Create a custom exception for invalid age (age < 18).
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) {
super(msg);
}
}
class AgeCheck {
public static void main(String[] args) {
try {
int age = 16;
if (age < 18)
throw new InvalidAgeException("Not eligible to vote");
else
[Link]("Eligible to vote");
} catch (Exception e) {
[Link]([Link]());
}
}
}
14. Write a program to reverse a string and count number of characters.
class StringReverse {
public static void main(String[] args) {
String s = "Computer";
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev = rev + [Link](i);
}
[Link]("Original: " + s);
[Link]("Reverse: " + rev);
[Link]("Length: " + [Link]());
}
}
15. Write a program to create two threads that print numbers from 1 to 5.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](i);
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
16. Create a Database
CREATE DATABASE school;
17. Open the Database.
USE school;
18. Create STUDENT Table.
CREATE TABLE student (
admno INT PRIMARY KEY,
name VARCHAR(20),
class INT,
section CHAR(1),
marks INT
);
19. Insert Record into STUDENT.
INSERT INTO student VALUES (101, 'Amit', 12, 'A', 85);
20. Insert Multiple Records.
INSERT INTO student VALUES
(102, 'Riya', 12, 'A', 90),
(103, 'Rahul', 12, 'B', 78);
21. Display All Records.
SELECT * FROM student;
Output.
101 Amit 12 A 85
102 Riya 12 A 90
103 Rahul 12 B 78
22. Display Selected Columns.
SELECT name, marks FROM student;
output
Amit 85
Riya 90
Rahul 78
23. Update Marks of a Student.
UPDATE student
SET marks = 88
WHERE admno = 101;
24. Delete a Record.
DELETE FROM student
WHERE admno = 103;
25. Add a New Column.
ALTER TABLE student
ADD dob DATE;
26. Modify Column Size
ALTER TABLE student
MODIFY name VARCHAR(30);
27. Order Records by Marks.
SELECT * FROM student
ORDER BY marks DESC;
28. Group By Class
SELECT class, COUNT(*)
FROM student
GROUP BY class;
29. Create FEES Table
CREATE TABLE fees (
admno INT,
amount INT,
month VARCHAR(10)
);
30. Join Two Tables
SELECT [Link], [Link]
FROM student, fees
WHERE [Link] = [Link];