1) Assignment method
// Aim: To find square root of a number
class SquareRoot {
public static void main(String args[]) {
int a = 49;
double r = [Link](a);
[Link]("Number: " + a);
[Link]("Square Root = " + r);
}
}
OUTPUT:
2) Blue J Method
// aim: to check whether a given number is a neon number or not
class neon
{
public static void main(int n)
{
int sq = n * n;
int sum = 0;
while(sq > 0)
{
sum = sum + sq % 10;
sq = sq / 10;
}
if(sum == n)
[Link](n + “ is a neon number.”);
else
[Link](n + “ is not a Neon number.”);
}
}
OUTPUT:
3) Conditional operator
// Aim: To check whether a number is above 18 or not using Conditional
Operator
import [Link].*;
class CheckAge {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
int n = [Link]();
String result = (n > 18) ? "Above 18" : "Not above 18";
[Link](result);
}
}
OUTPUT:
4. If else if type program
//Aim: electricity bill calculation
import [Link].*;
class ElectricityBill
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int u;
double charge;
[Link]("Enter units consumed: ");
u = [Link]();
if(u <= 100)
charge = u * 2.10;
else if(u <= 200)
charge = u * 4.20;
else if(u <= 300)
charge = u * 6.30;
else
charge = u * 8.80;
[Link]("Total charge = ₹" + charge);
}
}
}
OUTPUT:
5. Discount Calculations
i)
//Aim: price after discount
Import [Link];
Class Discount {
Public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link](“Enter price: “);
Double price = [Link]();
[Link](“Enter discount %: “);
Double discount = [Link]();
Double discountedPrice = price – (price * discount / 100);
[Link](“Price after discount: “ + discountedPrice);
}
}
OUTPUT:
ii) Calculating the total price with the fixed discounts
import [Link];
class Discount {
public static void main(String[] args)
{Scanner sc = new Scanner([Link]);
double a, b;
[Link](“Enter total cost Of purchase: “);
a = [Link](); // total Cost
double discount = 0;
If (a >= 10000) {
discount = 16;
} else if (a >= 5001) {
discount = 12;
}
else if (a >= 2000)
{
discount = 8;
} else {
discount = 5;
}
b = a – (a * discount / 100); //Price after discount
[Link](“Total price After discount: + b);
}
}
OUTPUT:
6. Menu driven number pattern
// Aim: To display two number patterns using a menu-driven program
import [Link].*;
class MenuPattern
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("1. Pattern 1");
[Link]("2. Pattern 2");
[Link]("Enter your choice: ");
int ch = [Link]();
if(ch == 1)
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
[Link](j);
[Link]();
}
}
else if(ch == 2)
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
[Link](j);
[Link]();
}
}
else
[Link]("Invalid Choice");
}
}
OUTPUT:
7. Menu driven string pattern
// // Aim: To display two string patterns using a menu-driven program
import [Link].*;
class StringMenu
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("1. Pattern 1");
[Link]("2. Pattern 2");
[Link]("Enter your choice: ");
int ch = [Link]();
[Link](); // consume newline
[Link]("Enter a word: ");
String s = [Link]();
if(ch == 1)
{
for(int i = 0; i < [Link](); i++)
[Link]([Link](0, i+1));
}
else if(ch == 2)
{
for(int i = [Link](); i > 0; i--)
[Link]([Link](0, i));
}
else
[Link]("Invalid Choice");
}
}
OUTPUT:
8. Menu driven sum of series
// Aim: To calculate the sum of natural numbers, odd numbers, and even
numbers using a menu-driven program
import [Link].*;
class SumSeries {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int choice;
do {
[Link]("Menu:");
[Link]("1. Sum of first n natural numbers");
[Link]("2. Sum of first n odd numbers");
[Link]("3. Sum of first n even numbers");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch(choice) {
case 1:
[Link]("Enter n: ");
int n1 = [Link]();
int sum1 = n1 * (n1 + 1) / 2;
[Link]("Sum = " + sum1);
break;
case 2:
[Link]("Enter n: ");
int n2 = [Link]();
int sum2 = n2 * n2;
[Link]("Sum = " + sum2);
break;
case 3:
[Link]("Enter n: ");
int n3 = [Link]();
int sum3 = n3 * (n3 + 1);
[Link]("Sum = " + sum3);
break;
case 4:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice! Try again.");
}
} while(choice != 4);
}
}
OUTPUT:
9. Menu driven number type
// Aim: To check whether a number is Prime, Armstrong, or Automorphic using
loops and if-else
// Aim: To check whether a number is Prime, Armstrong, or Automorphic using
loops and if-else
import [Link].*;
class NumberType {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int choice = 0;
while(choice != 4) {
[Link]("Menu:");
[Link]("1. Check Prime");
[Link]("2. Check Armstrong");
[Link]("3. Check Automorphic");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
if(choice == 1) { // Prime
[Link]("Enter number: ");
int n = [Link]();
int i;
for(i = 2; i <= n/2; i++) {
if(n % i == 0)
break;
}
if(n <= 1 || i <= n/2)
[Link](n + " is Not Prime");
else
[Link](n + " is Prime");
}
else if(choice == 2) { // Armstrong
[Link]("Enter number: ");
int n = [Link]();
int temp = n, sum = 0;
while(temp != 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp = temp / 10;
}
if(sum == n)
[Link](n + " is Armstrong");
else
[Link](n + " is Not Armstrong");
}
else if(choice == 3) { // Automorphic
[Link]("Enter number: ");
int n = [Link]();
int square = n * n;
int temp = n;
while(temp > 0) {
if(temp % 10 != square % 10)
break;
temp = temp / 10;
square = square / 10;
}
if(temp == 0)
[Link](n + " is Automorphic");
else
[Link](n + " is Not Automorphic");
}
else if(choice == 4) {
[Link]("Exiting...");
}
else {
[Link]("Invalid choice!");
}
}
}
}
OUTPUT:
10. String – Palindrome words of string
// Aim: To check whether a given string is a palindrome
import [Link].*;
class PalindromeString {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int i = 0, j = [Link]() - 1;
while(i < j) {
if([Link](i) != [Link](j))
break;
i++;
j--;
}
if(i >= j)
[Link](str + " is a palindrome");
else
[Link](str + " is not a palindrome");
}
}
OUTPUT:
11) String – Piglatin using sentence
// Aim: To convert a sentence into Pig Latin
import [Link].*;
class PigLatin {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
String words[] = [Link](" ");
[Link]("Pig Latin: ");
for(int i = 0; i < [Link]; i++) {
String word = words[i];
if([Link]() > 0) {
// Move first letter to end and add "ay"
String pig = [Link](1) + [Link](0) + "ay";
[Link](pig + " ");
}
}
}
}
OUTPUT:
[Link] – Accept a string and print all unique words.
// Aim: To accept a string and print all unique words
import [Link].*;
class UniqueWords {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of words:");
int n = [Link]();
String words[] = new String[n];
[Link]("Enter words:");
for(int i = 0; i < n; i++) {
words[i] = [Link]();
}
[Link]("Unique words:");
for(int i = 0; i < n; i++) {
int j;
for(j = 0; j < i; j++) {
if(words[i].equals(words[j]))
break;
}
if(i == j)
[Link](words[i] + " ");
}
}
}
OUTPUT:
13. Arrays
i)
// // Aim: To accept name and marks of 10 students, calculate total, sort in
descending order, and display
import [Link].*;
class StudentMarks {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
String names[] = new String[10];
int marks[][] = new int[10][3]; // 3 subjects
int total[] = new int[10];
// Accept names and marks
for(int i = 0; i < 10; i++) {
[Link]("Enter name of student " + (i+1) + ": ");
names[i] = [Link]();
total[i] = 0;
for(int j = 0; j < 3; j++) {
[Link]("Enter marks in subject " + (j+1) + ": ");
marks[i][j] = [Link]();
total[i] += marks[i][j];
}
[Link](); // consume newline
}
// Selection sort in descending order
for(int i = 0; i < 10-1; i++) {
int idx = i;
for(int j = i+1; j < 10; j++) {
if(total[j] > total[idx])
idx = j;
}
// Swap total
int tempTotal = total[i];
total[i] = total[idx];
total[idx] = tempTotal;
// Swap names
String tempName = names[i];
names[i] = names[idx];
names[idx] = tempName;
}
// Display name and total
[Link]("\nName\tTotal Marks");
for(int i = 0; i < 10; i++) {
[Link](names[i] + "\t" + total[i]);
}
}
}
OUTPUT:
ii)
// Aim: To accept name and marks of 10 students, calculate total, sort in
descending order, and display
import [Link].*;
class StudentMarks {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
String names[] = new String[10];
int marks[][] = new int[10][3]; // 3 subjects
int total[] = new int[10];
// Accept names and marks
for(int i = 0; i < 10; i++) {
[Link]("Enter name of student " + (i+1) + ": ");
names[i] = [Link]();
total[i] = 0;
for(int j = 0; j < 3; j++) {
[Link]("Enter marks in subject " + (j+1) + ": ");
marks[i][j] = [Link]();
total[i] += marks[i][j];
}
[Link]();
}
// Selection sort in descending order
for(int i = 0; i < 10-1; i++) {
int idx = i;
for(int j = i+1; j < 10; j++) {
if(total[j] > total[idx])
idx = j;
}
// Swap total
int tempTotal = total[i];
total[i] = total[idx];
total[idx] = tempTotal;
// Swap names
String tempName = names[i];
names[i] = names[idx];
names[idx] = tempName;
}
// Display name and total
[Link]("\nName\tTotal Marks");
for(int i = 0; i < 10; i++) {
[Link](names[i] + "\t" + total[i]);
}
}
}
OUTPUT:
iii)
// Aim: To fill a 4x4 array, replace diagonal elements with 0, and print largest
elements of each row and column
import [Link].*;
class DiagonalReplace {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int a[][] = new int[4][4];
// Accept elements
[Link]("Enter 16 elements for 4x4 matrix:");
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
a[i][j] = [Link]();
}
}
// Replace left and right diagonal elements with 0
for(int i = 0; i < 4; i++) {
a[i][i] = 0; // left diagonal
a[i][3-i] = 0; // right diagonal
}
// Print modified matrix
[Link]("\nModified matrix:");
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
[Link](a[i][j] + "\t");
}
[Link]();
}
// Largest element of each row
[Link]("\nLargest element of each row:");
for(int i = 0; i < 4; i++) {
int max = a[i][0];
for(int j = 1; j < 4; j++) {
if(a[i][j] > max)
max = a[i][j];
}
[Link]("Row " + (i+1) + ": " + max);
}
// Largest element of each column
[Link]("\nLargest element of each column:");
for(int j = 0; j < 4; j++) {
int max = a[0][j];
for(int i = 1; i < 4; i++) {
if(a[i][j] > max)
max = a[i][j];
}
[Link]("Column " + (j+1) + ": " + max);
}
}
}
OUTPUT:
iv)
// Aim: To accept elements into an n*n matrix and print all boundary elements
import [Link].*;
class BoundaryElements {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of matrix n: ");
int n = [Link]();
int a[][] = new int[n][n];
// Accept elements
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
[Link]("Enter element at (" + i + "," + j + "): ");
a[i][j] = [Link]();
} }
// Print boundary elements
[Link]("\nBoundary elements:");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == 0 || i == n-1 || j == 0 || j == n-1)
[Link](a[i][j] + " ");
}
} }
}
OUTPUT:
14. Functions
i)
// Aim: To accept a 4-digit number and check whether it is an USHWA number
using function with argument and return
import [Link].*;
class UshwaNumber {
// Function with argument and return
public int checkUshwa(int num) {
int sum = 0;
int temp = num;
int first = temp / 1000; // 1st digit
int last = temp % 10; // last digit
while(temp != 0) {
sum = sum + (temp % 10);
temp = temp / 10;
}
return sum - 2 * (first + last); // if result = 0 → USHWA
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
UshwaNumber obj = new UshwaNumber();
[Link]("Enter a 4-digit number: ");
int num = [Link]();
int result = [Link](num);
if(result == 0)
[Link](num + " is an USHWA number");
else
[Link](num + " is not an USHWA number");
}
}
OUTPUT:
ii. To accept a sentence and print in ascending order (has argument / no
return)
import [Link];
class AscendingWords {
void sort(String s) {
s = s + " ";
String w[] = new String[50];
int i, j, k = 0;
String word = "";
for(i = 0; i < [Link](); i++) {
char ch = [Link](i);
if(ch != ' ')
word = word + ch;
else {
w[k] = word;
k++;
word = "";
}
}
for(i = 0; i < k - 1; i++) {
for(j = i + 1; j < k; j++) {
if(w[i].compareToIgnoreCase(w[j]) > 0) {
String temp = w[i];
w[i] = w[j];
w[j] = temp;
}
}
}
for(i = 0; i < k; i++)
[Link](w[i] + " ");
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence:");
String s = [Link]();
AscendingWords obj = new AscendingWords();
[Link](s);
}
}
OUTPUT:
iii. Input a number and print its digits in ascending order. Has args/ no return
// Aim: To accept a number and print its digits in ascending order (has
argument / no return)
// Aim: To accept a number and print its digits in ascending order (has
argument / no return)
import [Link].*;
class AscendingDigits {
// Function with argument and no return
public void sortDigits(int n) {
int a[] = new int[10];
int c = 0;
// extract digits
while(n != 0) {
a[c] = n % 10;
n = n / 10;
c++;
}
// sort digits (ascending)
for(int i = 0; i < c - 1; i++) {
for(int j = i + 1; j < c; j++) {
if(a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
// print digits
[Link]("Digits in ascending order: ");
for(int i = 0; i < c; i++) {
[Link](a[i]);
}
[Link]();
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
AscendingDigits obj = new AscendingDigits();
[Link]("Enter a number: ");
int n = [Link]();
[Link](n);
}
}
OUTPUT:
15. Recursion
// Aim: To find factorial of a number using recursion
import [Link].*;
class Fact {
// recursive function
public int fact(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
Fact f1 = new Fact();
[Link]("Enter a number: ");
int n = [Link]();
int ans = [Link](n);
[Link]("Factorial = " + ans);
}
}
OUTPUT:
16. Function overloading
// Aim: To demonstrate function overloading using different string operations
// Aim: To demonstrate function overloading using different string operations
import [Link].*;
class OverloadFun {
public void fun(String s, char c, char c1) {
String ns = "";
for(int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if(ch == c)
ns = ns + c1;
else
ns = ns + ch;
}
[Link]("New string: " + ns);
}
public void fun(String s) {
int sp = -1, spc = -1;
for(int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if(ch == ' ' && sp == -1)
sp = i;
else if( && ch != ' ' && spc == -1)
spc = i;
}
[Link]("First space at position: " + sp);
[Link]("First special character at position: " + spc);
}
public void fun(String s1, String s2, int x) { // used int just to make unique
signature
char f1 = [Link](0);
char l1 = [Link]([Link]()-1);
char f2 = [Link](0);
char l2 = [Link]([Link]()-1);
String nw = "" + f1 + l1 + f2 + l2;
[Link]("New word: " + nw);
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
OverloadFun ob = new OverloadFun();
[Link]("1. Replace character");
[Link]("2. Find positions");
[Link]("3. Make new word");
[Link]("Enter your choice: ");
int ch = [Link]();
[Link]();
if(ch == 1) {
[Link]("Enter a string: ");
String s = [Link]();
[Link]("Enter character to replace: ");
char c = [Link]().charAt(0);
[Link]("Enter new character: ");
char c1 = [Link]().charAt(0);
[Link](s, c, c1);
}
else if(ch == 2) {
[Link]("Enter a string: ");
String s = [Link]();
[Link](s);
}
else if(ch == 3) {
[Link]("Enter first word: ");
String s1 = [Link]();
[Link]("Enter second word: ");
String s2 = [Link]();
[Link](s1, s2, 1);
}
else
[Link]("Invalid choice");
}
}
OUTPUT:
17. Class / Class overloading
Aim: //To design a class to overload the function series() to perform different
operations
import [Link];
class SeriesOverload {
void series(int x, int n) {
int sum = 0;
for(int i = 1; i <= n; i++) {
sum = sum + (int)[Link](x, i);
}
[Link]("Sum of series: " + sum);
}
void series(int p) {
for(int i = 1; i <= p; i++) {
int term = (int)[Link](i, 3) - 1;
[Link](term);
if(i < p)
[Link](", ");
}
[Link]();
}
void series() {
for(int i = 2; i <= 10; i++) {
[Link]("1/" + i);
if(i < 10)
[Link](" + ");
}
[Link]();
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
SeriesOverload obj = new SeriesOverload();
[Link]("Enter x and n:");
int x = [Link]();
int n = [Link]();
[Link](x, n);
[Link]("Enter number of terms (p):");
int p = [Link]();
[Link](p);
[Link]();
}
}
OUTPUT:
18. Constructor overloading
// Program to calculate rent of a Mobike
import [Link].*;
class Mobike
{
int bno, phno, days, charge;
String name;
Mobike() // default constructor
{bno = 0;
phno = 0;
days = 0;
charge = 0;
name = "";
}
void input() // input details
{Scanner sc = new Scanner([Link]);
[Link]("Enter bike number: ");
bno = [Link]();
[Link]("Enter phone number: ");
phno = [Link]();
[Link]();
[Link]("Enter customer name: ");
name = [Link]();
[Link]("Enter number of days: ");
days = [Link]();
}
void compute() // calculate rent
{
if(days <= 5)
charge = days * 500;
else if(days <= 10)
charge = 5 * 500 + (days - 5) * 400;
else
charge = 5 * 500 + 5 * 400 + (days - 10) * 200;
}
void display() // display details
{ [Link]("\nBike No: " + bno);
[Link]("Customer Name: " + name);
[Link]("Phone No: " + phno);
[Link]("Days: " + days);
[Link]("Charge: Rs. " + charge);
}
public static void main(String args[])
{ Mobike m = new Mobike();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
19) Number type
import [Link];
class ProductDifference {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first integer: ");
int a = [Link]();
[Link]("Enter second integer: ");
int b = [Link]();
int product = a * b;
int difference = a - b;
[Link]("Product: " + product);
[Link]("Difference: " + difference);
}
}
OUTPUT: