[go: up one dir, main page]

0% found this document useful (0 votes)
19 views24 pages

ANSHMAIN

Uploaded by

aaravsinghal432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

ANSHMAIN

Uploaded by

aaravsinghal432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ACKNOWLEDGEMENT

I would like to express my sincere gratitude to my


esteemed Computer Science teacher, Mr. Amit
Srivastava, for his invaluable guidance, support, and
encouragement throughout the development of this
computer project. His insightful feedback and
constructive criticism have been instrumental in shaping
this project into its current form.
I am deeply grateful for the opportunity to learn and
explore the fascinating world of Java programming
under his expert guidance. His dedication and passion
for teaching have inspired me to strive for excellence in
this field.
I would also like to thank my fellow friends for their
support and assistance during this project.
This project would not have been possible without their
unwavering support and encouragement.
Table of Contents
 Introduction
 Program 1: [Number Type (Abundant,Perfect,Deficient)]
 Program 2: [Heteromeric or Pronic]
 Program 3: [Disarium Number]
 Program 4:[Menu Driven(Palindrome and Perfect Number)]
 Program 5: [Automorphic Number]
 Program 6: [Piglatin Word]
 Program 7: [Consecutive Pairs of Alphabets]
 Program 8: [File Path :Name and Extension]
 Program 9:[Library (Accesion number and fine calculation)]
 Program 10: [Digits and Their Frequency]
 Program 11: [Name and Weight of Friends (SST)]
 Program 12: [Overload Polygon]
 Program 13: [Switch Case Menu(Fibonacci and sum of
Digits)]
 Program 14: [Cities and their STDs]
 Program 15: [2D Array Matrix]
 Program 16: [2D Array Matrix]
 Program 17: [20 Numbers Bubble Sort Technique]
 Program 18: [Animal Names (Selection Sort Technique)]
 Program 19: [Short Form of a Sentence]
 Program 20: [2D Array String Palindrome]
 Conclusion
 Acknowledgement
PROGRAM-1:-

import java.util.Scanner;
class Number {
int n;
String numtype;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
n = sc.nextInt();
}
void checkType() {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}

if (sum == 2 * n) {
numtype = "perfect";
} else if (sum > 2 * n) {
numtype = "abundant";
} else {
numtype = "deficient";
}
}
void display() {
System.out.println("Number: " + n);
System.out.println("Number Type: " + numtype);
}
public static void main(String[] args) {
Number num = new Number();
num.input();
num.checkType();
num.display();
}
}
PROGRAM-2:-

import java.util.*;
public class check
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = scanner.nextInt();

if (num < 0) {
System.out.println(num + " is not a Pronic Number” };
else {
boolean isPronic = false;
for (int i = 0; i * (i + 1) <= num; i++) {
if (i * (i + 1) == num) {
isPronic = true;
break;
}
}

if (isPronic) {
System.out.println(num + " is a Pronic Number.");
} else {
System.out.println(num + " is not a Pronic Number.");
}
}
PROGRAM-3:-

import java.util.Scanner;

public class check {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();

if (isDisarium(num)) {
System.out.println(num + " is a Disarium number.");
} else {
System.out.println(num + " is not a Disarium number.");
}
}

static boolean isDisarium(int num) {


int originalNum = num;
int sum = 0;
int numDigits = String.valueOf(num).length();

while (num > 0) {


int lastDigit = num % 10;
sum += Math.pow(lastDigit, numDigits);
num /= 10;
numDigits--;
}

return sum == originalNum;


}
}
PROGRAM-4:-
import java.util.Scanner;
public class numcheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
System.out.println("\n1. Palindrome\n2. Perfect");
System.out.print("Choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter number: ");
int n = sc.nextInt();
int original = n, rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if (original == rev) {
System.out.println(original + " is Palindrome");
} else {
System.out.println(original + " is not Palindrome");
}
break;
case 2:
System.out.print("Enter number: ");
n = sc.nextInt();
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum == n) {
System.out.println(n + " is Perfect");
} else {
System.out.println(n + " is not Perfect");
}
break;
default:
System.out.println("Invalid choice.");
}
} }
PROGRAM-5:-

import java.util.Scanner;

public class numcheck {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

int sq = num * num;


int temp = num;
int count = 0;
while (temp > 0) {
count++;
temp /= 10;
}
int lastDigits = sq % (int) Math.pow(10, count);
if (lastDigits == num) {
System.out.println(num + " is an Automorphic number.");
} else {
System.out.println(num + " is not an Automorphic number.");
}
}
}
PROGRAM-6:-

import java.util.Scanner;

public class p6 {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine().toUpperCase();
scanner.close();

int firstVowelIndex = -1;


for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
firstVowelIndex = i;
break;
}
}

String pigLatin;
if (firstVowelIndex == -1) {
pigLatin = word + "AY";
} else if (firstVowelIndex == 0) {
pigLatin = word + "AY";
}
else {
String firstPart = word.substring(firstVowelIndex);
String secondPart = word.substring(0, firstVowelIndex);
pigLatin = firstPart + secondPart + "AY";
}
System.out.println("Pig Latin: " + pigLatin);
}
}
PROGRAM-7:-

import java.util.*;//importing util package


class Couples//class declaration
{
void illegal()//function prototype
{
Scanner sc=new Scanner (System.in);
//creating an object of the scanner class
System.out.println("Enter to sentence to count the number of consecutive pairs of
alphabet"); String n=sc.nextLine();
String y=n.toUpperCase();//coverting to upper case
int c=0;
//variable to count the number of consecutive pairs
for(int i=0;i<y.length()-1;i++)
{ String x=Character.toString(y.charAt(i));//storing the current character
String w=Character.toString(y.charAt(i+1));//storing te next character
if(x.compareTo(w)==(-1)) //ASCII value difference between 2 consecutive alphabets is 1
{
c++;
}
}
System.out.println("No.of consecutive pairs of alphabets are "+c);
}

}
PROGRAM-8:-
import java.util.*;//importing util package
class FilePath//class declaration
{
void Extenions()//function prototype
{ Scanner sc=new Scanner(System.in);//object of the scanner class
System.out.println("Enter the file path");
String n=sc.nextLine();//taking the input from the user
int c=0;//to count the number of "/" in the filepath
int sl=n.lastIndexOf('/');//to store the location of slash
int dl=n.indexOf('.');//to store the location of dot
char ch;//to store characters
if(n.indexOf('/')==-1&&n.indexOf('.')==-1&&n.indexOf(':')==-1)
//checking if the File Path is valid .
{
System.out.println("Invalid File Path");
System.exit(0);
//Terminating the program if the File Path is invalid
}
System.out.println("Path:"+n.substring(0,sl));
//Printing the File Path
System.out.println("File name:"+n.substring(sl+1,dl));
//Printing the File name
System.out.println("Extension:"+n.substring(dl+1,n.length()));
//Printing the File Extension
}
}
PROGRAM-9:-

import java.util.Scanner;

class library {
int acc_num;
String title;
String author;

void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter accession number: ");
acc_num = scanner.nextInt();
scanner.nextLine(); // Consume newline left by nextInt()
System.out.print("Enter title: ");
title = scanner.nextLine();
System.out.print("Enter author: ");
author = scanner.nextLine();
}

void compute() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of days late: ");
int daysLate = scanner.nextInt();
int fine = daysLate * 2;
System.out.println("Fine charged: Rs. " + fine);
}

void display() {
System.out.println("Accession Number\t\tTitle\t\tAuthor");
System.out.println(acc_num + "\t\t" + title + "\t\t" + author);
}

public static void main(String[] args) {


library book = new library();
book.input();
book.compute();
book.display();
}
}
PROGRAM-10:-

import java.util.Scanner;

public class frequency {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long num = scanner.nextLong(); // Use long to handle larger numbers
scanner.close();

int[] frequency = new int[10]; // Array to store frequency of digits 0-9

// Count frequency of each digit


long temp = num;
while (temp > 0) {
int digit = (int) (temp % 10);
frequency[digit]++;
temp /= 10;
}

System.out.println("Digit\tFrequency");
//Print only present digits
for (int i = 0; i < 10; i++) {
if (frequency[i] > 0) {
System.out.println(i + "\t\t" + frequency[i]);
}
}
}
}
PROGRAM-11:-

import java.util.Scanner;
public class Friendweightsorter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int NUM_FRIENDS = 20;
String[] name = new String[NUM_FRIENDS];
double[] weight = new double[NUM_FRIENDS];
for (int i = 0; i < NUM_FRIENDS; i++) {
System.out.print("Enter name of friend " + (i + 1) + ": ");
name[i] = scanner.nextLine();
System.out.print("Enter weight of " + name[i] + ": ");
weight[i] = scanner.nextDouble();
scanner.nextLine();
}
scanner.close();
for (int i = 0; i < NUM_FRIENDS - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < NUM_FRIENDS; j++) {
if (weight[j] < weight[minIndex]) {
minIndex = j;
}
}
double tempWeight = weight[minIndex];
weight[minIndex] = weight[i];
weight[i] = tempWeight;

String tempName = name[minIndex];


name[minIndex] = name[i];
name[i] = tempName;
}
System.out.println("\nFriends sorted by weight (ascending):");
System.out.println("-------------------------------------");
System.out.println("Name\t\tWeight"); // Using tabs for spacing
System.out.println("-------------------------------------");
for (int i = 0; i < NUM_FRIENDS; i++) {
System.out.println(name[i] + "\t\t" + weight[i]); // Using tabs for spacing
}
}
}
PROGRAM-12:-
class overload {

void polygon(int n, char ch) {


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(ch);
}
System.out.println();
}
}
void polygon(int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print("@");
}
System.out.println();
}
}

void polygon() {
for (int i = 1; i <= 3; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for asterisks in each row
System.out.print("*");
}
System.out.println(); // Newline after each row
}
}

public static void main(String[] args) {


overload p = new overload();
p.polygon(2, 'O');
System.out.println();
p.polygon(2, 5);
System.out.println();
p.polygon();
}
}
PROGRAM-13:-
import java.util.Scanner;

public class menudriven {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Menu:");
System.out.println("1. Generate and display Fibonacci series");
System.out.println("2. Find sum of digits of an integer");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
int count = 10; // Generate first 10 terms
int num1 = 0, num2 = 1;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= count; ++i) {
System.out.print(num1 + " ");
int num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
System.out.println();
break;
case 2:
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int sum = 0;

while (number > 0) {


sum += number % 10;
number /= 10;
}
System.out.println("Sum of digits: " + sum);
break;
default:
System.out.println("Invalid choice!");
}
}
}
PROGRAM-14:-

import java.util.Scanner;
public class stdcode {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Declare arrays to store city names and STD codes


String[] cities = new String[10];
int[] stdCodes = new int[10];
// Input city names and STD codes
System.out.println("Enter 10 cities and their STD codes:");
for (int i = 0; i < 10; i++) {
System.out.print("Enter city: ");
cities[i] = scanner.nextLine();
System.out.print("Enter STD code: ");
stdCodes[i] = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
}
// Input the city name to search
System.out.print("Enter the city name to search: ");
String searchCity = scanner.nextLine();
// Search for the city in the list
boolean found = false;
int index = -1;
for (int i = 0; i < 10; i++) {
if (cities[i].equalsIgnoreCase(searchCity)) {
found = true;
index = i;
break;
}
}
// Display the result
if (found) {
System.out.println("Search Successful");
System.out.println("City: " + cities[index]);
System.out.println("STD Code: " + stdCodes[index]);
} else {
System.out.println("Search Unsuccessful, No such city in the list.");
}
}
}
PROGRAM-15:-
import java.util.Scanner;

public class a {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[4][4];

// Input elements for the matrix


System.out.println("Enter elements for the 4x4 matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("Enter element at (" + i + ", " + j + "): ");
matrix[i][j] = scanner.nextInt();
}
}

// Display the matrix


System.out.println("\nMatrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

// Calculate sum of main and right diagonal elements


int mainDiagonalSum = 0;
int rightDiagonalSum = 0;
for (int i = 0; i < 4; i++) {
mainDiagonalSum += matrix[i][i];
rightDiagonalSum += matrix[i][3 - i]; // Calculate right diagonal index
}

// Display the sum of diagonals


System.out.println("\nSum of main diagonal elements: " + mainDiagonalSum);
System.out.println("Sum of right diagonal elements: " + rightDiagonalSum);
}
}
PROGRAM-16:-

import java.util.*;
class Matrix
{
int m[][] = new int[3][3];
void getdata() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
System.out.println("Enter the Element of row no." + (i + 1) + " and column
no." + (k + 1));
m[i][k] = sc.nextInt();
}
}
System.out.println("The original Matrix is:-");
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < 3; k++)
{
System.out.print(m[i][k] + "\t");
}
System.out.println();
}
}
void rowsum()
{
int sum;
for (int i = 0; i < 3; i++)
{
sum = 0;
for (int k = 0; k < 3; k++)
{
sum = sum + m[i][k];
}
System.out.println("Sum of row no." + (i + 1) + " is " + sum);
}
}
void colsum()
{
int sum;
for (int i = 0; i < 3; i++)
{
sum = 0;
for (int k = 0; k < 3; k++)
{
sum = sum + m[k][i];
}
System.out.println("Sum of column no." + (i + 1) + " is " + sum);
}
}

public static void main(String[] args)


{
Matrix obj = new Matrix();
obj.getdata();
obj.rowsum();
obj.colsum();
}
}
PROGRAM-17:-
import java.util.Scanner;

public class BubbleSort {

public static void main(String[] args) {


int[] arr = new int[20];
Scanner sc = new Scanner(System.in);

// Input array elements


System.out.println("Enter 20 elements:");
for (int i = 0; i < 20; i++) {
arr[i] = sc.nextInt();
}

// Bubble Sort
for (int i = 0; i < 20 - 1; i++) {
for (int j = 0; j < 20 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// Print sorted array


System.out.println("Sorted array:");
for (int i = 0; i < 20; i++) {
System.out.print(arr[i] + " ");
}
}
}
PROGRAM-18:-
import java.util.Scanner;

public class SelectionSortAnimals {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of animals: ");


int n = sc.nextInt();
sc.nextLine(); // Consume the newline character

String[] animals = new String[n];

System.out.println("Enter the names of the animals:");


for (int i = 0; i < n; i++) {
animals[i] = sc.nextLine();
}

// Selection Sort
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (animals[j].compareTo(animals[minIndex]) < 0) {
minIndex = j;
}
}

// Swap animals[i] and animals[minIndex]


String temp = animals[i];
animals[i] = animals[minIndex];
animals[minIndex] = temp;
}

// Print sorted animal names


System.out.println("Sorted animal names:");
for (int i = 0; i < n; i++) {
System.out.print(animals[i] + ", ");
}
}
}
PROGRAM-19:-
import java.util.Scanner;

public class CapitalsInSentence {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence: ");


String sentence = sc.nextLine() + " ";

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


char ch = sentence.charAt(i);

if (ch != ' ') {


// Build the word until a space is encountered
StringBuilder word = new StringBuilder();
while (i < sentence.length() && sentence.charAt(i) != ' ') {
word.append(sentence.charAt(i));
i++;
}

// Convert the first letter to uppercase and print with a dot


System.out.print(Character.toUpperCase(word.charAt(0)) + ".");
}
}
}
}
PROGRAM-20:-
import java.util.Scanner;

public class StringPalindrome {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String[] words = new String[5];

System.out.println("Enter 5 words:");
for (int i = 0; i < 5; i++) {
words[i] = sc.nextLine();
}

System.out.print("The Palindrome Strings are: ");


for (int i = 0; i < 5; i++) {
String reversed = new StringBuilder(words[i]).reverse().toString();
if (words[i].equalsIgnoreCase(reversed)) {
System.out.print(words[i] + ", ");
}
}
}
}
Conclusion
This computer project, comprising 20 Java programs, has
provided me with a valuable opportunity to deepen my
understanding of core Java programming concepts. Through
the development of these programs, I have gained practical
experience in various areas, including:
 Fundamental programming concepts: Variables, data

types, operators, control flow (loops, conditional


statements), arrays, and methods.
 Object-Oriented Programming (OOP) principles:

Classes, objects, inheritance, polymorphism,


encapsulation, and abstraction.
 Input/Output operations: Reading user input and

writing output to the console.


 String manipulation: Working with strings, including

character extraction, concatenation, and comparison.


 Data structures: Implementing basic data structures like

arrays and strings.


 Problem-solving: Analyzing problems, designing

algorithms, and translating them into effective Java code.


This project has not only enhanced my programming skills
but also fostered my critical thinking, problem-solving, and
analytical abilities. I believe that the knowledge and
experience gained through this project will be invaluable in
my future academic and professional endeavours.

You might also like