This content originally appeared on DEV Community and was authored by Vigneshwaralingam
1. Get three numbers and find 2nd maximum
import java.util.Scanner;
public class SecondMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int secondMax;
if ((a >= b && a <= c) || (a <= b && a >= c)) secondMax = a;
else if ((b >= a && b <= c) || (b <= a && b >= c)) secondMax = b;
else secondMax = c;
System.out.println("Second Maximum: " + secondMax);
}
}
2. Recursion- Find sum of digits of a number using recursion.
import java.util.Scanner;
public class SumDigits {
public static int sumOfDigits(int n) {
if (n == 0) return 0;
return n % 10 + sumOfDigits(n / 10);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
System.out.println("Sum of digits: " + sumOfDigits(num));
}
}
3. Get numbers until ‘q’, sum & count
import java.util.Scanner;
public class SumCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0, count = 0;
while (true) {
String input = sc.nextLine(); // e.g. 10,w
String[] parts = input.split(",");
int number = Integer.parseInt(parts[0]);
char ch = parts[1].charAt(0);
sum += number;
count++;
if (ch == 'q') break;
}
System.out.println("Count: " + count);
System.out.println("Sum: " + sum);
}
}
4. Get numbers, validate, print min & max
import java.util.*;
public class MinMaxValidation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> nums = new ArrayList<>();
while (true) {
int n = sc.nextInt();
if (n < 0) {
System.out.println("Error: Negative number not allowed");
continue;
}
if (n > 999) {
System.out.println("Error: Number with more than 3 digits not allowed");
continue;
}
nums.add(n);
if (nums.size() == 5) break; // stop after 5 numbers for example
}
System.out.println("Numbers: " + nums);
System.out.println("Min: " + Collections.min(nums));
System.out.println("Max: " + Collections.max(nums));
}
}
5. Animal, Dog, Human (OOP – method overriding)
class Animal {
void whoAmI() {
System.out.println("Animal");
}
}
class Dog extends Animal {
@Override
void whoAmI() {
System.out.println("Dog");
}
}
class Human extends Animal {
@Override
void whoAmI() {
System.out.println("Human");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
Animal d = new Dog();
Animal h = new Human();
a.whoAmI(); // Animal
d.whoAmI(); // Dog
h.whoAmI(); // Human
}
}
6. Circle radius → diameter & area
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double r = sc.nextDouble();
double diameter = 2 * r;
double area = Math.PI * r * r;
System.out.println("Diameter: " + diameter);
System.out.println("Area: " + area);
}
}
7. SQL – Create Tables
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
class INT
);
CREATE TABLE marks (
id INT,
tamil INT,
eng INT,
maths INT,
science INT,
history INT,
FOREIGN KEY (id) REFERENCES students(id)
);
-- Create Students table
CREATE TABLE Students (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
class INT
);
-- Create Marks table
CREATE TABLE Marks (
id INT PRIMARY KEY,
tamil INT,
eng INT,
maths INT,
science INT,
history INT
);
-- Insert example data
INSERT INTO Students VALUES (1, 'Vicky', 'Male', 10);
INSERT INTO Marks VALUES (1, 45, 50, 60, 55, 48);
-- Total marks per student
SELECT s.id, s.name,
(m.tamil + m.eng + m.maths + m.science + m.history) AS total
FROM Students s
JOIN Marks m ON s.id = m.id;
-- Count pass students (class 10, pass mark 40)
SELECT COUNT(*) FROM Students s
JOIN Marks m ON s.id = m.id
WHERE s.class = 10
AND m.tamil>=40 AND m.eng>=40 AND m.maths>=40 AND m.science>=40 AND m.history>=40;
8. SQL – Find total marks of each student
SELECT id, (tamil + eng + maths + science + history) AS total
FROM marks;
9. HTML – Students table
<!DOCTYPE html>
<html>
<head><title>Students Table</title></head>
<body>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Class</th><th>Gender</th></tr>
<tr><td>1</td><td>Vicky</td><td>10</td><td>Male</td></tr>
<tr><td>2</td><td>Malini</td><td>8</td><td>Female</td></tr>
</table>
</body>
</html>
10. Validate student marks (HTML + JS)
<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validate()">
Tamil: <input type="text" id="tamil"><br>
English: <input type="text" id="eng"><br>
<button type="submit">Submit</button>
</form>
<script>
function validate() {
let t = document.getElementById("tamil").value;
let e = document.getElementById("eng").value;
if (t === "" || e === "") {
alert("Fields cannot be empty");
return false;
}
return true;
}
</script>
</body>
</html>
11. Write user input to file
import java.io.*;
import java.util.Scanner;
public class WriteFile {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
FileWriter fw = new FileWriter("output.txt");
String data = sc.nextLine();
fw.write(data);
fw.close();
}
}
12. Read file and count line, char, words
import java.io.*;
public class FileReadCount {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("output.txt"));
int lines=0, words=0, chars=0;
String line;
while((line=br.readLine())!=null){
lines++;
chars += line.length();
words += line.split("\\s+").length;
}
br.close();
System.out.println("Lines: "+lines);
System.out.println("Words: "+words);
System.out.println("Chars: "+chars);
}
}
13. Stack class (push, pop, peek)
class Stack {
int[] arr;
int top, capacity;
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
void push(int x) {
if (top == capacity-1) System.out.println("Overflow");
else arr[++top] = x;
}
int pop() {
if (top == -1) {
System.out.println("Underflow");
return -1;
}
return arr[top--];
}
int peek() {
if (top == -1) {
System.out.println("Empty");
return -1;
}
return arr[top];
}
}
This content originally appeared on DEV Community and was authored by Vigneshwaralingam