2s2s2 221113 225621

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Acknowledgement

Firstly, I would like to thank our computer


teacher MR. INDRAJEET SIR because he
always supported and guided us while
doing this project. He very well cleared all
the doubts I had regarding this project.
Also, I would like to especially thank my
parents and friends who helped me a lot to
complete this project within the limited
time.

The journey of making this project has been


beautiful, as well as knowledgeable for me
and I have learned a lot from it.

Once again, thanks to everyone who was


involved with this project from beginning to
end.

I have included 20 good programs of JAVA


in this project and I have completed it with
lots of hardwork.
1. PROGRAM TO CHECK ADAM NUMBER IN JAVA
ALGORITHM
1. Find the square of the number.

2. Find the reverse of the square.

3. Reverse the number N.

4. Find the square of the reversed number.

5. If the reverse of the square of the number is equal to the square of the reverse
of the number, the number is an Adam number, else not.

6. Stop

Sourcecode
import java.util.Scanner;
public class AdamNumberExample
{
static int reverseDigits(int num)
{
int rev = 0;
while (num > 0)
{
rev = rev * 10 + num % 10;
num = num / 10;
}
return rev;
}
static int square(int num)
{
return (num * num);
}
static boolean isAdamNumber(int num)
{
int a = square(num);
int b = square(reverseDigits(num));
if (a == reverseDigits(b))
return true;
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = sc.nextInt();
if (isAdamNumber(num))
System.out.println(num+ " is an Adam number.");
else
System.out.println(num+ " not an Adam number.");
}
}
Output
Enter the number :32
Is an adam number
___________________________________________________________________

2. PROGRAM TO CHECK ARMSTRONG NUMBER IN


JAVA
ALGORITHM
1. Take an integer variable x
2. Value is assigned to the variable x
3. The digits of the value are split
4. The cube value of each digit is found
5. Add the values of all the cubes
6. The output is saved to sum variable
7. If sum=X, print Armstrong number
8. If sum != X, print not Armstrong number
9. Stop

Sourcecode

import java.util.Scanner;

class ArmstrongNum

public static void main(String[] args)

int originalNum, digit, cubeSum = 0,num;

System.out.println("ENTER THE NUMBER :");

Scanner sc = new Scanner(System. in );

num = sc.nextInt();

originalNum = num;

while (num!= 0)

digit = num % 10;

cubeSum += Math.pow(digit, 3);

num /= 10;

if(cubeSum == originalNum)

System.out.println(originalNum+ " IS AN ARMSTRONG NUMBER ");


else

System.out.println(originalNum+ " IS NOT AN ARMSTRONG NUMBER ");

}
}

Output
Enter the number: 153

Yes the entered number 153 is an Armstrong number.

3. A class Rearrange has been defined to modify a word by


bringing all the vowels in the. word at the beginning
followed by the consonants. WRITE A JAVA PROGRAM TO
ENABLE ABOVE TASK

ALGORITHM
1 Traverse the array from start to end.
2 For every index increment the element by array[array[index] ] %
n. To get the ith element find the modulo with n, i.e
array[index]%n.
3 Again Traverse the array from start to end
4 Print the ith element after dividing the ith element by n, i.e.
array[i]/n.
5 Stop

Sourcecode
import java.util.*;

public class Rearrange

String wrd,newwrd;

static Scanner x=new Scanner(System.in);

Rearrange(){}

void readword()

System.out.println("Enter a word" );

wrd=x.next();

wrd=wrd.toUpperCase();

void freq_vow_con()

int s=0,s1=0;

char ch;

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

ch=wrd.charAt(i);

if("AEIOU".indexOf(ch)!=-1)

s++;

s1= wrd.length()-s;

System.out.println("vowels = "+ s);

System.out.println("consonants = " + s1);


}

void arrange()

char ch;

String p="",q="";

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

ch=wrd.charAt(i);

if("AEIOU".indexOf(ch)!=-1)

p +=ch;

else

q +=ch;

newwrd= p+q;

void display()

System.out.println("Original word = "+ wrd);

System.out.println("Rearranged word = "+ newwrd);

static void main()

Rearrange obj=new Rearrange();

obj.readword();

obj.freq_vow_con();

obj.arrange();

obj.display();

}
}

Output
ORIGINAL becomes OIIARGNL

4. Multiple Inheritance Program in Java

ALGORITHM
1 Declare and define the function get1() to get the salary details.

2 Define the function calculate() to find the net pay.

3 Define the function display().

4 Create the derived class object.

5 Read the number of employees.

6 Call the function get(),get1() and calculate() to each employee

7 Call the display().


8 Stop the program.

Sourcecode

class A {

public void methodA() {

System.out.println("method of Class A");

class B extends A{

public void methodB(){

System.out.println("method of Class B");

class C extends A {

public void methodC()

{
System.out.println("method of Class C");

class D extends A{

public void methodD(){

System.out.println("method of Class D");

class InheritncExm{

public static void main(String args[]) {

B obj1 = new B();

C obj2 = new C();

D obj3 = new D();

//All classes can access the method of class A

obj1.methodA();

obj2.methodA();
obj3.methodA();

5. JAVA PROGRAMV TO CHECK GOLDBACH NUMBER

ALGORITHM
1 Read or initialize a number N.

2 Store it in another variable.

3 Check if the given number N is even or not.

4 If the number is odd, print "Invalid input".

5 If the number is even, follow the procedure given below:

B. Define two arrays one for storing the prime numbers and the other for
calculating the prime number.

C. Find all prime numbers till the entered number using a for loop and store
it in an array.
D. In the second array, store only odd prime numbers using if statement.

E. Display the odd prime pairs.

6 By using the for loop, store the sum of two odd prime numbers in a variable
(sum).

7 Using the if statement, compare the sum with the original number N. If the
sum is equal to the original number, the given number N is Goldbach
Number, else not.

Sourcecode

import java.util.Scanner;

public class GoldbachNumber

public static boolean isPrime(int num) {

int c = 0;

for (int i = 1; i <= num; i++) {

if (num % i == 0) {

c++;

return c == 2;

public static void main(String args[]) {


Scanner in = new Scanner(System.in);

System.out.print("ENTER THE VALUE OF N: ");

int n = in.nextInt();

if (n <= 9 || n >= 50) {

System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");

return;

if (n % 2 != 0) {

System.out.println("INVALID INPUT. NUMBER IS ODD.");

return;

System.out.println("PRIME PAIRS ARE:");

int a = 3;

int b = 0;

while (a <= n / 2) {

b = n - a;

if (isPrime(a) && isPrime(b)) {

System.out.println(a + ", " + b);

a += 2;
} Output

6.JAVA PROGRAM TO CHECK KRISHNAMURTHI


NUMBER
A Krishnamurthy number is a number whose sum of the factorial of
digits is equal to the number itself. For example 145, sum of factorial of
each digits:
1! + 4! + 5! = 1 + 24 + 120 = 145

ALGORITHM
1 We first take a number.

2 We then find the factorial of each digit of the number.

3 We add the factorial of each digit and store it into another variable.
4 If the sum of the factorial of each digit is the same as the original number, the
number is a Krishnamurthy number

Sourcecode
import java.util.Scanner;

public class Krishnamurthy{

public static void main(String args[])

Scanner sc = NEW Scanner(System.in);

System.out.print("Please enter a number : ");

int number = sc.nextInt();

int temp = number, a = 0, sum = 0;

String s = Integer.toString(number);

int l = s.length();

while(temp>0)

a = temp % 10;

sum = sum + (int)Math.pow(a,l);

l--;

temp = temp / 10;

if(sum == number)

System.out.println(number+ " is a Krishnamurthy Number.");

else

System.out.println(number+" is Not a Krishnamurthy Number.");

}
}

Output

7. Program to print all Kaprekar numbers between 1 to 100


A Kaprekar number is a number whose square when divided into two
parts and such that sum of parts is equal to the original number and
none of the parts has value 0.

ALGORITHM
6 STEP 1: START

7 STEP 2: REPEAT STEP 3 UNTIL (i<10)

8 STEP 3: if(Kaprekar(i)) then PRINT i

9 STEP 4: END

Kaprekar(int n)

7. STEP 1: START
8. STEP 2: if(n==1) RETURN true

9. STEP 3: sq_n = n*n

10.STEP 4: SET count_digits = 0

11.STEP 5: REPEAT STEP 6 and 7 UNTIL (sq_n!=0)

12.STEP 6: count_digits++

13.STEP 7: sq_n/=10

14.STEP 8: sq_n = n*n

15.STEP 9: REPEAT STEP 10 to 13 UNTIL r_digits<count_digits

16.STEP 10: eq_parts = (10)r_digits

17.STEP 11: if(eq_parts==n) then continue

18.STEP 12: sum = sq_n/eq_parts + sq_n%eq_parts

19.STEP 13: then RETURN true

20.STEP 14: RETURN false.

21.STEP 15: END

Sourcecode
public class Kaprekar_Number {
1 static boolean kaprekar(int n)

2 {
3 if (n == 1)

4 return true;
5 int sq_n = n * n;

6 int count_digits = 0;
7 while (sq_n != 0)

8 {
9 count_digits++;

10 sq_n /= 10;
11 }

12 sq_n = n*n;
13 for (int r_digits=1; r_digits<count_digits; r_digits++)

14 {
15 int eq_parts = (int) Math.pow(10, r_digits);

16 if (eq_parts == n)
17 continue;

18 int sum = sq_n/eq_parts + sq_n % eq_parts;


19 if (sum == n)

20 return true;
21 }

22 return false;
23 }

24 public static void main (String[] args)


25 {

26 for (int i=1; i<100; i++)


27 if (kaprekar(i))

28 System.out.print(i + " ");


29 }

30 }

Output
8. Java Program to Print Diamond Pattern
Sourcecode
import java.util.Scanner;

public class Diamond

public static void main(String[] args)

Scanner sc=new Scanner(System.IN);

System.out.println("Enter N : ");

int n=sc.nextInt();

System.out.print("Enter Symbol : ");


char c = sc.next().charAt(0);

FOR(int i=1;i<=n;i++)

FOR(int j=1;j<=n-i;j++)

System.out.print(" ");

FOR(int j=1;j<=i*2-1;j++)

System.out.print(c);

System.out.println();

FOR(int i=n-1;i>0;i--)

FOR(int j=1;j<=n-i;j++)

System.out.print(" ");

FOR(int j=1;j<=i*2-1;j++)

System.out.print(c);

System.out.println();

}
Output
Enter N : $ javac Diamond.java

$ java Diamond

Enter the number of rows: 5

***

*****

*******

*********

*******

*****

***

______________________________________________________________________________________________

9.Java Program to find the transpose of a


given matrix
ALGORITHM
1 STEP 1: START

2 STEP 2: DEFINE rows, cols

3 STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}

4 STEP 4: rows = a.length

5 STEP 5: cols = a[0].length

6 STEP 6: t[][] = [cols][rows]


7 STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<cols
// for(i=0; i<cols; i++)

8 STEP 8: REPEAT STEP 9 UNTIL j<rows


// for(j=0; j<rows; j++)

9 STEP 9: t[i][j] = a[j][i]

10 STEP 10: PRINT "Transpose of given matrix"

11 STEP 11: REPEAT STEP 12 to STEP 14 UNTIL i<cols


//for(i=0; i<cols; i++)

12 STEP 12: REPEAT STEP 13 UNTIL j<rows


//for(j=0; j<rows; j++)

13 STEP 13: PRINT t[i][j]

14 STEP 14: PRINT new line

15 STEP 15: END

Sourcecode
10 public class Transpose
11 {

12 public static void main(String[] args) {


13 int rows, cols;

14 int a[][] = {
15 {1, 2, 3},

16 {4, 5, 6},
17 {7, 8, 9}

18 };

19 rows = a.length;
20 cols = a[0].length;
21 int t[][] = new i

22 for(int i = 0; i < cols; i++)


23 {
24 for(int j = 0; j < rows; j++)

25 {

26 t[i][j] = a[j][i];

27 }
28 }

29
30 System.out.println("Transpose of given matrix: ");

31 for(int i = 0; i < cols; i++){


32 for(int j = 0; j < rows; j++){

33 System.out.print(t[i][j] + " ");


34 }

35 System.out.println();
36 }

37 }
38 }

Output
Result matrix is

1234

1234

1234
10. Xylem and Phloem Number Java Program
ALGORITHM
• Read or initialize a number N.

• Find the extreme digits of N.b B

• Sum up the extreme digits and store the sum in a variable (extreme_sum).

• Find the mean digits of N.

• Sum up the mean and store the sum in a variable (mean_sum).

• Compare both sums (that we get from steps 3 and 5).

o If they are equal, the number is a xylem

o Else, the number is a phloem

Sourcecode
1 import java.io.*;

2 public class XylemPhloemExample


3 {

4 public static void main(String args[])


5 {

6 int num, extreme_sum = 0, mean_sum = 0, n;


7 Scanner sc= new Scanner (System.in);
8 System.out.print("Enter a number: ");
9 num = sc.nextInt();
10 num = Math.abs(num);
11 n = num;
12 while(n != 0)
13 {

14 Yh

15 extreme_sum = extreme_sum + n % 10;

16 else
17 mean_sum = mean_sum + n % 10;
18 n = n / 10;
19 }

20 825122
21 System.out.println("The sum of mean digits: " + mean_7. );

22 if(extreme_sum == mean_sum)

23 System.out.println(num + " is a xylem number.");

24 else
25 System.out.println(num + " is a phloem number.");
26 }

Output
Enter a number: 825122
The sum of extreme digits: 10
The sum of mean digits: 10
825122 is a xylem number.
11.WRITE A JAVA PROGRAM TO CHECK
WHETHER A NUMBER IS STRONTIO
NUMBER OR NOT
Strontio numbers are those four digits numbers when multiplied by 2 give the same
digit at the hundreds and tens place.

ALGORITHM
1. Read or initialize a number N.

2. Multiply the given number (N) by 2.

3. Find remainder by dividing the resultant (from step 2) by 1000. Divide the
resultant (from step 2) by 1000. It removes the first digit from the resultant.

4. Divide the resultant (from step 3) by 10. It removes the last digit from the
resultant and gives a two-digit number.

5. Again, divide the two-digit number (from step 4) by 10, it gives the quotient.
By using the modulo operator find the remainder.

6. Compare both remainder and quotient. If they are equal, the given number (N)
is strontio, else not.

Sourcecode
7. import java.util.*;
8. public class StrontioNumberExample1

9. {
10. public static void main(String args[])

11. {
12. Scanner sc=new Scanner(System.in);

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


14. int num=sc.nextInt();
15. int n=num;

16. num=(num*2%1000)/10;
17. if(num%10==num/10)

18. System.out.println(n+ " is a strontio number.");

19. else
20. System.out.println(n+ " is not a strontio number.");
21. }

22. }

Output
12.WRITE A JAVA PROGRAM TO
CHECK WHETHER NUMBER IS
NELSON NUMBER OR NOT.
In cricket, the number 111 is sometimes called “a Nelson” after Admiral
Nelson, who allegedly only had “One Eye, One Arm, One Leg” near the end of
his life.
ALGORITHM
1.Start

2. Read or initialize a number (X).

3.Separate each digit from the given number (X).

4.Add all the n-digits. It gives the next term of the series.

5.Again, add the last n-terms of the series to find the next term.

6.Repeat step 4 until we get the term the same as the number we have taken.

7.Stop

Sourcecode
import java.util.Scanner;

public class Main

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

System.out.println("Enter a number");

int n=in.nextInt();

if(n==111 || n==222 || n==333 || n==444 ||

n==555 || n==666 || n==777 || n==888 || n==999){

System.out.println("Nelson Number");

} else {

System.out.println("Not a Nelson Number");

Output
Enteranumber
111
Nelson Number
13 Java Program to find the sum of each row
and each column of a matrix
ALGORITHM
39 STEP 1: START

40 STEP 2: DEFINE rows, cols, sumRow, sumCol

41 STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}

42 STEP 4: rows = a.length

43 STEP 5: cols = a[0].length

44 STEP 6: REPEAT STEP 7 to STEP 10 UNTIL i<rows


// for(i=0; i<rows; i++)

45 STEP 7: SET sumRow =0

46 STEP 8: REPEAT STEP 9 UNTIL j<cols

47 STEP 9: sumRow = sumRow + a[i][j]

48 STEP 10: PRINT i+1, sumRow

49 STEP 11: REPEAT STEP 12 to STEP 15 UNTIL i<cols


//for(i=0; i<cols; i++)

50 STEP 12: SET sumCol =0

51 STEP 13: REPEAT STEP 14 UNTIL j<rows


//for(j=0; j<rows; j++)

52 STEP 14: sumCol =sumCol + a[j][i]

53 STEP 15: PRINT i+1, sumCol


54 STEP 16: End

Sourcecode
55 public class SumofRowColumn
56 {

57 public static void main(String[] args) {


58 int rows, cols, sumRow, sumCol;

59 int a[][] = { {1, 2, 3},{4, 5, 6},{7, 8, 9}};


60 rows = a.length;

61 cols = a[0].length;
62 for(int i = 0; i < rows; i++){

63 sumRow = 0;
64 for(int j = 0; j < cols; j++){

65 sumRow = sumRow + a[i][j];


66 }

67 System.out.println("Sum of " + (i+1) +" row: " + sumRow);


68 }

69 for(int i = 0; i < cols; i++){


70 sumCol = 0;

71 for(int j = 0; j < rows; j++){


72 sumCol = sumCol + a[j][i];

73 }
74 System.out.println("Sum of " + (i+1) +" column: " + sumCol);

75 }
76 }
77 }

Output

14.Menu Driven Program in Java for


Calculator
ALGORITHM
Step 1: Start the program

Step 2: Read choice

Step 3: Repeat till a valid choice

Step 3a: if choice is 1

perform task

Step 3b: If choice is 2

perform task

Step 3c: If choice is

perform task default:

perform task

Step 4: Stop the program.

Sourcecode
import java.util.Scanner;

public class CalculatorMenuDrivenExample

public static void main(String[] args){

int a,b,

int choice;

Scanner scanner = NEW

while(true)
System.out.println("Press 1 for Addition");

System.out.println("Press 2 for Subtraction");

System.out.println("Press 3 for Multiplication");

System.out.println("Press 4 for Division");

System.out.println("Press 5 to Quit\n \n ");

System.out.println("Make your choice")

choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.println("Enter the first number ");

a = scanner.nextInt();

System.out.println("Enter the second number");

b = scanner.nextInt();

c = a + b;

System.out.println("The sum of the numbers is = " + c +"\n");

break;

case 2:

System.out.println("Enter the first number ");

a = scanner.nextInt();

System.out.println("Enter the second number");

b = scanner.nextInt();

c = a - b;

System.out.println("The difference of the numbers is = " + c +"\n");

break;

case 3:

System.out.println("Enter the first number");

a = scanner.nextInt();

System.out.println("Enter the second number");

b = scanner.nextInt();
c = a * b;

System.out.println("The product of the numbers is = " + c + "\n");

break;

case 4:

System.out.println("Enter the first number");

a = scanner.nextInt();

System.out.println("Enter the second number");

b = scanner.nextInt();

c = a / b;

System.out.println("The quotient is = " “);

case 5:

System.exit(0);

default:

System.out.println("Invalid choice!!! Please make a valid choice. \\n\\n");

}
Output
15. Java Program to find Reverse of the
string

ALGORITHM
78 STEP 1: START

79 STEP 2: DEFINE String string = "Dream big"

80 STEP 3: DEFINE reversedStr = " "

81 STEP 4: SET i =string.length()-1. REPEAT STEP 5 to STEP 6 UNTIL i>=0

82 STEP 5: reversedStr = reversedStr + string.charAt(i)

83 STEP 6: i = i - 1

84 STEP 7: PRINT string.

85 STEP 8: PRINT reversedStr.

86 STEP 9: END

Sourcecode
87 public class Reverse

88 {

89 public static void main(String[] args) {

90 String string = "Dream big";

91 //Stores the reverse of given string

92 String reversedStr = "";

93

94 //Iterate through the string from last and add each character to variable reversedStr

95 for(int i = string.length()-1; i >= 0; i--){


96 reversedStr = reversedStr + string.charAt(i);

97 }

98

99 System.out.println("Original string: " + string);

100 //Displays the reverse of given string

101 System.out.println("Reverse of given string: " + reversedStr);

102 }
16.Java program for banking management
system
ALGORITHM
1.Start

2.Display All

3.Search By Account

4.Deposit

5.Withdrawal

6.Exit

Sourcecode
import java.util.Scanner;

class Bank {

private String accno;

private String name;

private long balance;

Scanner KB = new Scanner(System.in);

//method to open an account

void openAccount() {

System.out.print("Enter Account No: ");

accno = KB.next();

System.out.print("Enter Name: ");


name = KB.next();

System.out.print("Enter Balance: ");

balance = KB.nextLong();

//method to display account details

void showAccount() {

System.out.println(accno + "," + name + "," + balance);

//method to deposit money

void deposit() {

long amt;

System.out.println("Enter Amount U Want to Deposit : ");

amt = KB.nextLong();

balance = balance + amt;

//method to withdraw money

void withdrawal() {

long amt;

System.out.println("Enter Amount U Want to withdraw : ");

amt = KB.nextLong();

if (balance >= amt) {

balance = balance - amt;

} else {

System.out.println("Less Balance..Transaction Failed..");

//method to search an account number


boolean search(String acn) {

if (accno.equals(acn)) {

showAccount();

return (true);

return (false);

public class ExBank {

public static void main(String arg[]) {

Scanner KB = new Scanner(System.in);

//create initial accounts

System.out.print("How Many Customer U Want to Input : ");

int n = KB.nextInt();

Bank C[] = new Bank[n];

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

C[i] = new Bank();

C[i].openAccount();

//run loop until menu 5 is not pressed

int ch;

do {

System.out.println("Main Menu\n1. Display All\n 2. Search By Account\n 3. Deposit\n 4. Withdrawal\n 5.E


xit ");

System.out.println("Ur Choice :"); ch = KB.nextInt();

switch (ch) {

case 1:

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


C[i].showAccount();

break;

case 2:

System.out.print("Enter Account No U Want to Search...: ");

String acn = KB.next();

boolean found = false;

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

found = C[i].search(acn);

if (found) {

break;

if (!found) {

System.out.println("Search Failed..Account Not Exist..");

break;

case 3:

System.out.print("Enter Account No : ");

acn = KB.next();

found = false;

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

found = C[i].search(acn);

if (found) {

C[i].deposit();

break;

if (!found) {
System.out.println("Search Failed..Account Not Exist..");

break;

case 4:

System.out.print("Enter Account No : ");

acn = KB.next();

found = false;

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

found = C[i].search(acn);

if (found) {

C[i].withdrawal();

break;

if (!found) {

System.out.println("Search Failed..Account Not Exist..");

break;

case 5:

System.out.println("Good Bye..");

break;

while (ch != 5);

Output
How Many Customer U Want to Input : 2
Enter Account No: 101

Enter Name: Chintu

Enter Balance: 25000

Enter Account No: 102

Enter Name: Alexander

Enter Balance: 30000

Main Menu

1.Display All

2.Search By Account

3.Deposit

4.Withdrawal

5.Exit

Ur Choice :

101,Chintu,25000

102,Alexander,30000

1
17.Java program to find if the given
number is a leap year?
ALGORITHM
1. Take integer variable year

2. Assign a value to the variable

3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"

4. Check if the year is divisible by 400, DISPLAY "leap year"

5. Otherwise, DISPLAY "not leap year"

Sourcecode
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args){
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();

if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))


System.out.println("Specified year is a leap year");
else
System.out.println("Specified year is not a leap year");
}
}

Output
Enter an Year ::
2020
Specified year is a leap year

18 Java Program to count the total number


of vowels and consonants in a string
ALGORITHM
STEP 1: START

STEP 2: SET vCount =0, cCount =0

STEP 3: DEFINE string str = "This is a really simple sentence".

STEP 4: CONVERT str to lowercase

STEP 5: SET i =0.

STEP 6: REPEAT STEP 6 to STEP 8 UNTIL i<str.length()

STEP 7: IF any character of str matches with any vowel then

vCount = vCount + 1.
STEP 8: IF any character excepting vowels lies BETWEEN a and z then
cCount = cCount =+1.

STEP 9: i = i + 1

STEP 10: PRINT vCount.

STEP 11: PRINT cCount.

STEP 12: END


Sourcecode
public class CountVowelConsonant {

public static void main(String[] args) {

//Counter variable to store the count of vowels and consonant

int vCount = 0, cCount = 0;

//Declare a string

String str = "This is a really simple sentence";

//Converting entire string to lower case to reduce the comparisons

str = str.toLowerCase();

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

//Checks whether a character is a vowel

if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.

charAt(i) == 'o' || str.charAt(i) == 'u') {

//Increments the vowel counter

vCount++;

//Checks whether a character is a consonant

else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {

//Increments the consonant counter

cCount++;
}

System.out.println("Number of vowels: " + vCount);

System.out.println("Number of consonants: " + cCount);

Output
Number of vowels: 10

Number of consonants: 17

19 Program to print all Kaprekar numbers


between 1 to 100
A non-negative integer having base(10) is said to be the Kaprekar number if the
representation of its square in its base can be split into two parts that add up to the
original number, with the condition that the part formed from the low-order digits
of the square must be non-zero-however, it is allowed to include leading zeroes.

ALGORITHM
STEP 1: START

STEP 2: REPEAT STEP 3 UNTIL (i<10)

STEP 3: if(Kaprekar(i)) then PRINT i

STEP 4: END
Sourcecode
103 public class Kaprekar_Number {
104 static boolean kaprekar(int n)

105 {
106 if (n == 1)

107 return true;


108 int sq_n = n * n;

109 int count_digits = 0;


110 while (sq_n != 0)

111 {
112 count_digits++;

113 sq_n /= 10;


114 }

115 sq_n = n*n;


116 for (int r_digits=1; r_digits<count_digits; r_digits++)

117 {
118 int eq_parts = (int) Math.pow(10, r_digits);

119 if (eq_parts == n)
120 continue;

121 int sum = sq_n/eq_parts + sq_n % eq_parts;


122 if (sum == n)

123 return true;


124 }

125 return false;


126 }

127 public static void main (String[] args)


128 {

129 for (int i=1; i<100; i++)


130 if (kaprekar(i))

131 System.out.print(i + " ");


132 }

133 }

Output
8 10 45 55 99

20 Implementation of bubble sort in Java


ALGORITHM
Step1: Repeat step 1 to 4 for i=0 to n

Step2: For j=0 to n

Step3: if(arr[j]>arr[j+1]

Step4: swap(arr[j],arr[j+1])

Step5: End

Sourcecode
134 public class BubbleSortExample {
135 static void bubbleSort(int[] arr) {
136 int n = arr.length;
137 int temp = 0;

138 for(int i=0; i < n; i++){


139 for(int j=1; j < (n-i); j++){

140 if(arr[j-1] > arr[j]){


141 //swap elements

142 temp = arr[j-1];


143 arr[j-1] = arr[j];

144 arr[j] = temp;


145 }

146
147 }

148 }
149

150 }
151 public static void main(String[] args) {

152 int arr[] ={3,60,35,2,45,320,5};


153

154 System.out.println("Array Before Bubble Sort");


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

156 System.out.print(arr[i] + " ");


157 }

158 System.out.println();
159

160 bubbleSort(arr);//sorting array elements using bubble sort


161

162 System.out.println("Array After Bubble Sort");


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

164 System.out.print(arr[i] + " ");


165 }

166
167 }

168

169

Output
Bibliography
I have taker help from the following sites
Sites:
1. Google.com
2. YouTube.com
3.JAVA school tutorial org
Books:
1. Sumita Arora dass 12th computer book
People':
1. Our computer teacher Indrajeet sir
2. My parents
3. My helpful friends

Thank you

You might also like