You are on page 1of 64

NPS INTERNATIONAL CHENNAI

Record

YEAR: 2018-2019

NAME : Siddhartha Mukherjee

GRADE : 10

ROLL NO : 30

SUBJECT : COMPUTER APPLICATIONS

TOPICS : PROGRAMS

1
COMPUTER RECORD
Siddhartha Mukherjee
Grade 10-

LIST OF PROGRAMS

S.No. Title of Program Page No.

1. Checking if a given number is a Magic Number or not 3

2. Menu-Driven List to perform 3 functions 6

3. Checking if 3 given numbers are Pythagorean Triplets or not 10

4. Checking if a given number is a Prime Number or not 12

5. Finding the sums of all odd numbers, and all even numbers, below a given number 15

6. To find the sum of various series 17

7. To find the first 15 terms of the Pell Series 22

8. To find whether a given pair of numbers is an Amicable Pair or not 23

9. To find whether a number is an Armstrong Number or not 26

10. To find whether a number is a Happy Number or not 28

11. To find whether a number is a Palindrome Number or not 31

12. To find the Lowest Common Multiple of 2 or more given numbers 35

13. Overloading a function to find the sum of two series 38

14. Changing the cases of all the letters in a given String 41

15. Checking if a word is a Palindrome or not 43

16. Finding the Pig-Latin version of an English word 45

17. Sorting the even numbers in a given array in ascending order using Bubble-Sort 47

18. Receiving a sentence and arranging its words in alphabetical order 50

19. Arranging an array of city names in alphabetical order 54

20. Producing various patterns on the screen using nested for-loops 57

2
Program 1: Checking if a number is a Magic Number or not
A magic number is a number whose continuous sum of digits equals 1. For example:

127  1+2+7 = 10  1+0  1

Hence, 127 is a magic number.

CODE:
importjava.util.*;

class P1_MagicNumber

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

public static intNumber,Sum;

public static void main(String[] args)

System.out.println("Enter a number to check if it is a happy number or not.");

int Number = in.nextInt();

if (isHappynumber(Number))

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

else

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

private static boolean isMagicnumber(int Number)

3
int Check1, Check2;

Check1 = Number; Check2 = Number;

do

Check1 = numSquareSum(Check1);

Check2 = numSquareSum(numSquareSum(Check2));

while (Check1 != Check2);

if(Check1==1)

return true;

else

return false;

private static intnumSquareSum(int n)

int Sum = 0;

while (n!= 0)

Sum += n%10;

n /= 10;

return Sum;

4
}

WORKING OF THE PROGRAM:


The program takes a number entered by the user in the method public static void main(String[] args), and
sends the number to the method private static boolean isMagicnumber(int Number)to check if it is a
happy number. Then, in this method, two integers take up the value of the entered number and
simultaneously check if the sum of the digits of the number equals 1. However, the second integer has 1
iteration more than the first. Both numbers keep having the sum of their digits checked until they both are
1-digit numbers. At the end of each check, both numbers are tested to see if they are equal to 1. If they are,
then the given number is a happy number. However, if both numbers are unequal and are one-digit
numbers, then the number is not a happy number.

OUTPUT:

5
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Number int To accept input from the user
Stores the value of one iteration
Check1 int of the addition of the digits of the
given number
Stores the value of two iterations
Check2 int of the addition of the digits of the
given number
Stores the sum of the digits of the
Sum int
number
Stores the value of the number in
n int
the units position in each iteration

Program 2: Menu-Driven program to perform 3 functions


This program performs 3 functions. If the user enters 1, the program asks for another number and checks
if it is an odd number or even number. If the user enters 2, the program asks for another number and
checks if it is a multiple of 17 or not. If the user enters 3, the program asks for another number and checks
if it is a 2-digit number or not.

CODE:
importjava.util.Scanner;

public class two

public void display()

Scanner in=new Scanner(System.in);

System.out.println("ENTER A NUMBER");

intnum=in.nextInt();

System.out.println("ENTER 1 TO CHECK WHETHER THE ENTERED NUMBER IS ODD OR


EVEN");

System.out.println("ENTER 2 TO CHECK WHETHER THE ENTERED NUMBER IS A MULTIPLE


OF SEVENTEEN OR NOT");
6
System.out.println("ENTER 3 TO CHECK WHETHER THE ENTERED NUMBER IS A TWO DIGIT
NUMBER OR NOT");

int choice=in.nextInt();

switch(choice)

case 1:

if(num%2==0)

System.out.println("THE ENTERED NUMBER IS EVEN");

else

System.out.println("THE ENTERED NUMBER IS ODD");

break;

case 2:

if(num%17==0)

System.out.println("THE ENTERED NUMBER IS A MULTIPLE OF SEVENTEEN");

else

System.out.println("TH ENTERED NUMBER IS NOT A MULTIPLE OF SEVENTEEN");

break;

7
case 3:

if(num>=100 &&num<=999 || num>=-999 &&num<=-100)

System.out.println("THE ENTERED NUMBER IS A TWO DIGIT NUMBER");

else

System.out.println("THE ENTERED NUMBER IS NOT A TWO DIGIT NUMBER");

break;

default:

System.out.println("INVALID OPTION");

break;

WORKING OF THE PROGRAM:


Initially, the program displays the options and asks the user to enter an appropriate value. A do-while loop
ensures that the program will not progress until the user enters a valid number. Once the user does, the
switch-case block of statements executes the code as per the desire of the user. There is no default: in the
switch-case block of statements as the do-while loop already ensured that no invalid input has been
entered.

OUTPUT:

8
9
Variable Description:

PROGRAM 2

VARIABLE DESCRIPTION

int num to store entered value

int choice to store choice of user

Program 3: Checking if 3 entered numbers


are Pythagorean Triplets or not
In this program, the user enters 3 digits to check if they are Pythagorean Triplets. The program then
checks if they are Pythagorean Triplets. If they are, the program declares so. If they are not Pythagorean
Triplets, the program declares that they are not.

CODE:
import java.util.*;

class P3_PythagoreanTriplets

10
public static Scanner in = new Scanner(System.in);

public static int N1,N2,N3;

public static void main(String[] args)

System.out.println("Enter three numbers to check if they are Pythagorean Triplets.");

N1= in.nextInt();

N2= in.nextInt();

N3= in.nextInt();

if((N1*N1)==(N2*N2)+(N3*N3))

System.out.println(N1+", "+N2+", and "+N3+" are Pythagorean Triplets.");

System.out.println("This is because the square of "+N1+" equals the sum of squares of "+N2+"
and "+N3+".");

else if((N2*N2)==(N1*N1)+(N3*N3))

System.out.println(N1+", "+N2+", and "+N3+" are Pythagorean Triplets.");

System.out.println("This is because the square of "+N2+" equals the sum of squares of "+N1+"
and "+N3+".");

else if((N3*N3)==(N1*N1)+(N2*N2))

System.out.println(N1+", "+N2+", and "+N3+" are Pythagorean Triplets.");

System.out.println("This is because the square of "+N3+" equals the sum of squares of "+N1+"
and "+N2+".");

else

11
{

System.out.println(N1+", "+N2+", and "+N3+" are not Pythagorean Triplets.");

WORKING OF THE PROGRAM:


The user enters 3 numbers. Three if-statements then check if the square of one of the numbers equals the
sum of squares of the other two numbers. If the square of one of the numbers equals the sum of squares of
the other two numbers, the program specifies the number and states that the three numbers are
Pythagorean Triplets. If no such number is present within the three, the program states that the three
numbers are not Pythagorean Triplets.

OUTPUT:

VARIABLE DESCRPITION:
12
Variable Name Data Type Purpose
N1 int To store the value of the first
digit entered by the user
N2 int To store the value of the second
digit entered by the user
N3 int To store the value of the third
digit entered by the user

Program 4: Checking if a number is a Prime Number


In this program, the user enters a number and the program checks is the numbers is a prime number or not.
A prime number is a number that has no factors other than itself and 1. If the number is a prime number,
the program states that it is. If it is a composite number, the program states so and also lists all its factors
(excluding itself and 1).

CODE:
importjava.util.Scanner;

public class four

public void display()

Scanner in=new Scanner(System.in);

System.out.println("ENTER A NUMBER TO CHECK WHETHER IT IS A PRIME NUMBER OR


NOT");

int x=in.nextInt();

int c=0,i;

for(i=2;i<x;i++)

if(x%i==0)

c++;

13
}

if(c!=0)

System.out.println("THE ENTERED NUMBER IS NOT A PRIME NUMBER");

else

System.out.println("THE ENTERED NUMBER IS A PRIME NUMBER");

WORKING OF THE PROGRAM:


The program takes a number entered by the user and then enters a for-loop. In this loop, another variable
takes the value of every number between 1 and that number to check if it has any factors. If, at any stage,
the variable’s value is a factor of the number, then the program declares that the number is a composite
number and proceeds to list all its factors one-by-one. If no such factor is found, the program declares that
the number is a prime number.

OUTPUT:

14
Variable Description:

PROGRAM 4

VARIABLE DESCRIPTION

int x to store entered number

int c it is used to keep count

int i it is a control variable


15
Program 5: Finding the sum of all odd numbers and all
even numbers below a given number
This program accepts a number entered by the user and finds the sum of a the odd numbers below that
number, and all the even numbers below that number seperately. The program then displays both.

CODE:
import java.util.*;

class P5_SumOfOddsAndEvens

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

public static int Limit,SumOdds=0,SumEvens=0,I;

public static void main(String args[])

System.out.println("Enter a number. The sum of all odd numbers and sum of all even numbers");

System.out.println("lesser than the entered number will be calculated.");

Limit = in.nextInt();

for(I=1;I<Limit;I++)

if(I%2!=0){SumOdds+=I;}

else if(I%2==0){SumEvens+=I;}

System.out.println("Sum of all odd numbbers below "+Limit+" is "+SumOdds);

System.out.println("Sum of all even numbbers below "+Limit+" is "+SumEvens);

WORKING OF THE PROGRAM:


16
After the user enters a number in the program, a for-loop takes control. A variable present in the loop
increases its own value by 1 every time the loop is executed. When the variable is odd, it is added to the
sum of odd numbers. When the variable is even, it is added to the sum of even numbers. This goes on until
the variable equals the entered number, at which point the loop terminates. The program then displays the
sum of all odd numbers, and the sum of all even numbers.

OUTPUT:

VARIABLE DESCRIPTION:
Variable Description Data Type Purpose
Stores the value of the number
entered by the user below which
Limit int
the sum of all odd and even
numbers have to be calculated
Stores the sum of all odd
SumOdds int numbers below the entered
number
Stores the sum of all even
SumEvens int numbers below the entered
number
I int Control variable

Program 6: Finding the sum of various series


This program finds and displays the sums of the various series listed below:

i) s=1*2+2*3+3*4+…+9*10
ii) s=1*2+3*4+5*6+…+9*10
iii) s=1*3+2*4+3*5+…+9*11
iv) s=1–2+3–4+5–6+…+19–20
17
v) s= 1+1/2+1/3+1/4+1/5+…+1/10
vi) s= 1/2+2/3+3/4+4/5+…+9/10
vii) s= 1-1/2+1/3-1/4+1/5-…-1/10

CODE:

public class six

public void one()

inti,j, s=0;

for(i=1;i<10;i++)

j=i+1;

s+=i*j;

System.out.println("SUM OF THE SERIES 1*2+2*3+3*4+4*5....+9*10 ="+s);

public void two()

inti,j,s=0;

for(i=1;i<10;i+=2)

j=i+1;

s+=i*j;

System.out.println("SUM OF THE SERIES 1*2+3*4+5*6+7*8....+9*10 ="+s);

18
}

public void three()

inti,j,s=0;

for(i=1;i<10;i++)

j=i+2;

s+=i*j;

System.out.println("SUM OF THE SERIES 1*3+2*4+3*5+4*6....+9*11 ="+s);

public void four()

inti,j,s=0;

for(i=1;i<20;i+=2)

j=i+1;

s+=i-j;

System.out.println("SUM OF THE SERIES 1-2+3-4+5-6+7-8....+19-20 ="+s);

public void five()

inti,j,s=0;

for(i=1;i<10;i++)

19
j=1/i;

s+=j;

System.out.println("SUM OF THE SERIES 1/1+1/2+1/3+1/4....+1/10 ="+s);

public void six()

inti,j,s=0;

for(i=1;i<10;i+=2)

j=i+1;

s+=i/j;

System.out.println("SUM OF THE SERIES 1/2+2/3+3/4+4/5....+9/10 ="+s);

public void seven()

inti,j,k,s=0;

for(i=1;i<10;i+=2)

j=1/i;

k=1/(i+1);

s+=j-k;

System.out.println("SUM OF THE SERIES 1/1-1/2+1/3-1/4....-1/10 ="+s);

20
}

WORKING OF THE PROGRAM:

The program contains seven methods, each programmed to find the sum of a particular series. The name
of the method corresponds to the series as mentioned above. Each method uses the same 2 global
variables.

OUTPUT:

21
Variable Description:
PROGRAM 6
VARIABLE DESCRIPTION
int i it is a control variable
int j
it is used to calculate and store the individual elements of therespective series
int sum it stores the sum of the respective series

Program 7: Finding the first 15 terms of the Pell Series


The Pell Series is a Series whose first two terms are 0 and 1. The next term is obtained by adding twice the
previous term with the term before the previous term. This program prints the first 15 terms of the Pell
Series.

CODE:
import java.util.*;

import java.awt.*;

22
class P7_PellSeries

public static int T1=1,T2=2,T3;

public static void main(String[] args)

System.out.print(T1+", ");

System.out.print(T2+", ");

for(int I=1;I<=13;I++)

T3=(2*T2)+T1;

System.out.print(T3+", ");

T1=T2;

T2=T3;

WORKING OF THE PROGRAM:


The program prints the first two terms of the Pell Series – 0 and 1. It then enters a for-loop to print the
next 13 terms. The loop contains a variable T3, which serves as the newest term in the series. It equals
(2*T2 + T1), where the values ofT2 and T1 increase after each iteration. The value of T3 is printed each
loop until 15 terms have been printed.

OUTPUT:

23
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Stores the value of the first term
T1 int in the Pell Series and every
following term
Stores the value of the second
term in the Pell Series and
T2 int
every following term just after
T1
Stores the value of the latest
T3 int term in the Pell Series which is
calculated on the spot
Control variable which runs the
I int
for-loop

Program 8: To check if two numbers are an Amicable Pair


A pair of numbers is said to be an amicable pair when the sum of factors of one number (excluding the
number itself) equals the other number, and the sum of factors of the other number (excluding the number
itself) equals the first number. This program checks whether two numbers are an amicable pair or not.

CODE:
importjava.util.Scanner;

public class eight

public void display()

Scanner in=new Scanner(System.in);

intx,y,sumx=0,sumy=0,i;

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

x=in.nextInt();

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

y=in.nextInt();

for(i=1;i<x;i++)

24
{

if(x%i==0)

sumx+=i;

for(i=1;i<y;i++)

if(y%i==0)

sumy+=i;

if(x==sumy)

System.out.println("THE ENTERED NUMBERS ARE A AMICABLE PAIR");

else

System.out.println("THE ENTERED NUMBERS ARE NOT A AMICABLE PAIR");

25
WORKING OF THE PROGRAM:
The program first accepts two numbers from the user. Then, a for-loop finds the factors of one number
and another for-loop does the same to the other number. An if-statement then checks if the sum of factors
of each number equals the other number. If yes, then the pair is displayed as an amicable pair.

OUTPUT:

Variable Description:

PROGRAM 8
VARIABLE DESCRIPTION
int i it is a control variable
int x it is used to store the first number
int y it is used to store the second number
sum x it stores the sum of digits of the value stored in the variable x

sum y it stores the sum of digits of the value stored in the variable x

26
Program 9: Checking if a number is an Armstrong Number
A number is said to be an Armstrong Number when the sum of cubes of its digits is equal to the number
itself. This program accepts a number and checks if it is an Armstrong Number.

CODE:
import java.util.*;

class P9_ArmstrongNumber

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

public static int Number,Copy,Sum=0,I;

public static void main(String[] args)

System.out.println("Enter a number to check if it is an Armstrong Number or not.");

Number = in.nextInt();

Copy=Number;

do

I=Number%10;

Sum+=I*I*I;

Number=Number/10;

while(Number!=0);

if(Sum==Copy){System.out.println(Copy+" is an Armstrong number.");}

else{System.out.println(Copy+" is not an Armstrong number.");}

WORKING OF THE PROGRAM:


27
The program accepts a number. A for-loop then obtains each individual digit in the number and adds their
cubes together. It then compares the sum of cubes of the digit to the digit itself. If they are equal, the
program states that the number is an Armstrong number. If they are unequal, the program states that the
number is not an Armstrong Number.

OUTPUT:

VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To store the value of the number
Number int
entered by the user
To duplicate the value entered by
Copy int the user for comparison in the
end
Stores the sum of cubes of the
Sum int
digits of the given number
Stores the value of each
I int individual number in the digit to
obtain its cubes

28
Program 10: Checking if a number is a Happy Number
A number is said to be a Happy Number when its continuous sum of squares of its digits eventually equals
1. For example:

97  92 + 72  130  12 + 32 + 02 10  12 + 02  1

Hence, 97 is a happy number.

This program checks if a number is a happy number or not.

CODE:
importjava.util.Scanner;

public class ten

public void display()

Scanner in=new Scanner(System.in);

System.out.println("ENTER A NUMBER TO CHECK WHETHER IT IS A HAPPY NUMBER OR


NOT");

int x=in.nextInt();

boolean flag=false,loop=true;

int k=0,sum=0;

do

do

k=x%10;

x=x/10;

29
sum+=k*k;

}while(x>0);

x=sum;

sum=0;

}while(x>=10);

if(x==1)

System.out.println("THE GIVEN NUMBER IS A HAPPY NUMBER");

else

System.out.println("THE GIVEN NUMBER IS NOT A HAPPY NUBMER");

WORKING OF THE PROGRAM:


The program takes a number entered by the user in the method public static void main(String[] args), and
sends the number to the method private static boolean isHappynumber(int Number)to check if it is a
happy number. Then, in this method, two integers take up the value of the entered number and
simultaneously check if the sum of the digits of the number equals 1. However, the second integer has 1
iteration more than the first. Both numbers keep having the sum of their digits checked until they both are
1-digit numbers. At the end of each check, both numbers are tested to see if they are equal to 1. If they are,
then the given number is a happy number. However, if both numbers are unequal and are one-digit
numbers, then the number is not a happy number.

OUTPUT:

30
Variable Description:

PROGRAM 10
VARIABLE DESCRIPTION
int x it is used to store the vaalue entered by the user
int k it is used to store the last digit of the number
int sum it used to store the calculated sum

Program 11: Checking if a number is a Palindrome number


31
A number is said to be a Palindrome number when it is the same forwards and backwards. For example,
14641 is a Palindrome number. In one method, this program accepts a number and checks if it is a
palindrome number or not. In the second method, the program accepts 10 numbers and displays the largest
Palindrome number, if any.

CODE:
import java.util.*;

class P11_PalindromeNumber

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

public static int Copy=0,I=0,Sum=0,Largest=0,A;

public static int Numbers[] = new int [10];

public static boolean Found = false;

public static boolean Palindrome(int Number)

Copy=Number;

while(Number>0)

I = Number%10;

Number = Number/10;

Sum = (Sum * 10) + I;

if(Sum==Copy){return true;}

else{return false;}

public static void main (String[] args)

System.out.println("Enter 10 numbers to find the largest palindrome number, if any.");

32
for(I=0;I<10;I++)

Numbers[I]=in.nextInt();

Copy=Numbers[I];

while(Numbers[I]>0)

A = Numbers[I]%10;

Numbers[I] = Numbers[I]/10;

Sum = (Sum * 10) + A;

if(Copy==Sum)

Found=true;

if(Copy>Largest)

Largest=Copy;

Sum=0;

if(Found==true)

System.out.println("Largest palindrome number: "+Largest);

else

33
System.out.println("None of the numbers entered is a palindrome number.");

WORKING OF THE PROGRAM:


The program has two methods. In the method public static boolean Palindrome(int Number), the user
enters a number and the method returns true if the number is a palindrome number. Else, it returns false.

In the method public static void main (String[] args), the user enters 10 numbers. The program checks if
each number is a Palindrome Number, and finally displays the largest Palindrome number. If no
Palindrome numbers were entered, the program states that no Palindrome numbers was entered.

OUTPUT:

34
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To duplicate the number entered
Copy int
so that the tests can be compared
Stores the value of the unit’s digit
I int
in each iteration
Stores the value of the reversed
Sum int
number
Stores the value of the largest
Largest int
palindrome number
Stores the value of the unit’s digit
A int
in each iteration
Stores the ten numbers which are
Numbers int array
to be checked
To check if at least one
Found boolean palindrome number has been
entered

Program 12: Finding the LCM of the given numbers


35
The LCM (Lowest Common Multiple) of two or more numbers is the smallest number which leaves no
reminder when divided by the two or more numbers. For example, 252 is the LCM of 36 and 7 as it leaves
no reminder when divided by the two numbers.

CODE:
importjava.util.Scanner;

public class twelve

publicint LCM(inta,int b)

inti=0,temp;

if(b<a)

temp=a;

a=b;

b=temp;

for(i=a;i<=a*b;i++)

if(i%a==0 &&i%b==0)

break;

returni;

public void call()

{
36
Scanner in=new Scanner(System.in);

System.out.println("ENTER 10 INTEGERS TO FIND THEIR LEAST COMMON MULTIPLE");

intx,y=1;

for(int j=0;j<10;j++)

x=in.nextInt();

y=LCM(x,y);

System.out.println("LCM OF THE GIVEN NUMBERS IS "+y);

WORKING OF THE PROGRAM:


There are two methods in this program. In the first method, the computer accepts two numbers and finds
their LCM using a for-loop. In the second, the computer accepts ten numbers and finds their LCM using
an identical for-loop.

OUTPUT:

Result of Method 1

37
Result of Method 2

PROGRAM 12
VARIABLE DESCRIPTION
int a stores the first value passed onto the function
int b stores the second value passed onto the function
int i it is a control variable
int temp it is used in swapping values
int x it is used to store te value entered by the user
int y it is used to store the LCM
int j it is a control variable

Program 13: Finding the sum of various Series


This program overloads the function Series() twice. In the first method, it accepts two numbers (for
example, x and n) and finds the sum of the following series:

x+x 2 /2! +x 3 /3! +x 4 /4! +…+x n /n!

The second method accepts one number (for example, n) and finds the sum of the following series:

1+1/2! +1/3! +1/4! +…1/n!

CODE:
import java.math.*;

38
class P13_SeriesOverload

public static double Sum=0,I,I2,FS=1,D,Sum2=1;

public static double series(double x,double n)

Sum=0;

I2=0;

for(I=1;I<=n;I++)

for(I2=I;I2>0;--I2)

FS=FS*I2;

Sum+=(Math.pow(x,I))/FS;

FS=1;

I2=0;

return Sum;

public static double series(double n)

Sum=0;Sum2=1;D=0;

for(I=1;I<=n;++I)

for(D=I;D>0;--D)

39
Sum2=Sum2*D;

Sum=Sum+(1/Sum2);

Sum2=1;

D=0;

return Sum;

WORKING OF THE PROGRAM:


In the first method, the program accepts two numbers and enters a for-loop where a control variable’s
value increases for each iteration. Another variable then finds the factorial of the control variable at that
point to use as the denominator. The sum is then found by repeatedly dividing the (first number ^ control
variable) by the (factorial of the control variable). The sum is then displayed.

In the second method, the program accepts one number and enters a for-loop where a control variable’s
value increases for each iteration. Another variable then finds the factorial of the control variable at that
point to use as the denominator. The sum is then found by repeatedly dividing 1 by the factorial of that
number. The sum is then displayed.

OUTPUT:

40
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Sum int Stores the sum of the first series
Stores the sum of the second
Sum2 int
series
I int Control variable for both series
Control variable for the first
I2 int
series exclusively
Stores the sum of the factorial of
FS int
each number
Value depending on which the
x int
sum of series is executed
Value depending on which the
n int
sum of series is executed

Program 14: Reversing all the letters in a sentence


The program accepts a sentence from the user. All the letters in Upper Case are changed to Lower Case,
and all the letters in Lower Case are changed to Upper Case. Any other input (like numbers or symbols)
are left unchanged.

CODE:
importjava.util.Scanner;

public class fourteen

41
public void display()

Scanner in=new Scanner(System.in);

System.out.println("ENTER A SENTENCE");

String x=in.nextLine();

String rev="";

char ac=' ';

for(inti=0;i<x.length();i++)

ac=x.charAt(i);

if(ac>='A' && ac<='Z')

ac=(char)((int)ac+32);

rev+=ac;

else

if(x.charAt(i)>='a' &&x.charAt(i)<='z')

ac=(char)((int)ac-32);

rev+=ac;

else

rev+=ac;

WORKING OF THE PROGRAM:


42
The program accepts a sentence from the user and enters a for-loop where each individual character of the
word is checked. If the character is an alphabet, its case is changed. If it is not an alphabet, it is left
unchanged.

OUTPUT:

Variable Description:

PROGRAM 14
VARIABLE DESCRIPTION
String x it is used to store the entered sentence
String rev it is used to store the reversed case sentence
char ac it is used to store esch character of the sentence
int i it is a control variable

Program 15: Checking if a given word is a palindrome


word or not
A palindrome word is a word that reads the same forwards and backwards. The program accepts a word
from the user and checks if it is a palindrome or not. It then displays a message accordingly.

CODE:
import java.util.*;

class P15_PalindromeWord

{
43
public static Scanner in = new Scanner(System.in);

public static String Sentence,ReversedSentence="";

public static char C;

public static int I;

public static void main(String[] args)

System.out.println("Enter a word to check if it is a palindrome.");

Sentence=in.nextLine();

for(I=0;I<Sentence.length();I++)

C=Sentence.charAt(I);

ReversedSentence=C+ReversedSentence;

if(Sentence.equals(ReversedSentence))

System.out.println(Sentence+" is a palindrome.");

else if(Sentence.equalsIgnoreCase(ReversedSentence)&&Sentence!=ReversedSentence)

System.out.println(Sentence+" is a palindrome if you ignore case.");

else

System.out.println(Sentence+" is not a palindrome.");

44
}

WORKING OF THE PROGRAM:


The program accepts a word and then enters a for-loop where another word is constructed. This word is
the exact reverse of the word entered by the user. These words are then checked to see if they are equal. If
they are, the entered word is a palindrome word. If not, the word is not a palindrome word.

OUTPUT:

VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Stores the sentence entered by the
Sentence String
word
Stores the value of the reversed
ReversedSentence String
word
Stores the value of each letter
C char individually, one at a time, to add
to the reverse of that word
I int Control variable

Program 16: Finding the Pig-Latin version of a word


45
The Pig-Latin equivalent of an English word is found thus:

1. The letters which are present before the first vowel are removed and placed at the end of the word.

2. The suffix “ay” is added to the word.

For example, “trash” becomes “ashtray”.

The program accepts a word and displays its Pig-Latin equivalent.

CODE:
importjava.util.Scanner;

public class sixteen

public void display()

Scanner in=new Scanner(System.in);

System.out.println("ENTER A WORD");

String x=in.nextLine();

x=x.toUpperCase();

charch=' ';

String a,b;

for(inti=0;i<x.length();i++)

ch=x.charAt(i+1);

if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' )

a=x.substring(0,i+1);

b=x.substring(i+1,x.length());

x=b+a+"AY";

break;

46
}

System.out.println(x);

WORKING OF THE PROGRAM:


The program accepts a word from the user. It then peruses each letter in the word until it finds the first
vowel. It then adds the part of the word before the vowel to the end of the word. After adding “ay” to the
newly formed word, the Pig-Latin version of the word is formed and is displayed on the screen.

OUTPUT:

Variable Description:

PROGRAM 16
VARIABLE DESCRIPTION
String x it stores the entered word
char ch stores the respective letter
String a it stores letters until the first consonant
String b it stores the letters other than that of the first consonant

int i control variable

47
Program 17: Accepting 10 numbers and arranging
only the even numbers in ascending order
The program accepts an array of 10 numbers. It arranges only the even numbers in ascending number and
prints only the even numbers.

CODE:
import java.util.*;

class P17_EvenBubbleSort

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

public static int Array[]=new int[10];

public static int I,J,Temp,EC=0;

public static void main(String[] args)

System.out.println("Enter 10 numbers and the even ones will be sorted in ascending order.");

for(I=0;I<10;I++)

Array[I]=in.nextInt();

for(I=0;I<10;I++)

if(Array[I]%2==0)

EC=EC+1;

}
48
System.out.println();

for(I=0;I<Array.length;I++)

for(J=1;J<Array.length;J++)

if(Array[J-1]%2==1 && Array[J]%2==0)

if(Array[J]%2==0)

Temp = Array[J-1];

Array[J-1] = Array[J];

Array[J] = Temp;

if(Array[J-1]> Array[J])

if(Array[J]%2==0)

Temp = Array[J-1];

Array[J-1] = Array[J];

Array[J] = Temp;

for(I=0;I<EC;I++)

49
{

if(I+1 != EC)

System.out.print(Array[I]+", ");

else

System.out.print("and "+Array[I]+".");

WORKING OF THE PROGRAM:


The program accepts 10 numbers from the user and counts the number of even numbers in the set. This is
necessary as only the even numbers must be printed. Next, the program arranges all the even numbers
before the odd numbers. After doing so, it arranges the even numbers in ascending order. It then displays
only the even numbers on the screen.

OUTPUT:

50
VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
Array int array Stores ten numbers
Control variable, runs the outer
I int
for-loop
Control variable, runs the inner
J int
for-loop
Store the number of even
EC int numbers and prints only that
amount of numbers
Helps in exchanging the position
Temp int
of two variables

Program 18: Arranging the words of a sentence


in alphabetical order
The program accepts a sentence from the user and arranges all its words in alphabetical order. It then
prints each word one after the other.

CODE:
importjava.util.Scanner;

public class eighteen


51
{

public void display()

Scanner in=new Scanner (System.in);

System.out.println("ENTER A SENTENCE");

String x =in.nextLine();

x=" "+x+" ";

inti,j=0,k,words=0;

String temp="";

for(i=0;i<x.length()-1;i++)

if(x.charAt(i)==' ' &&x.charAt(i+1)!=' ')

words++;

String str[]=new String[words];

System.out.println("THERE ARE "+words+" NUMBER OF WORDS IN THE ENTERED


SENTENCE");

for(i=0;i<x.length()-1;i++)

if(x.charAt(i)==' ' &&x.charAt(i+1)!=' ')

k=i;

do

{
52
k++;

}while(x.charAt(k+1)!=' ');

str[j]=x.substring(i+1,k+1);

j++;

for(i=str.length-1;i>=0;i--)

for(j=0;j<i;j++)

if(str[j].compareTo(str[j+1])>0)

temp=str[j];

str[j]=str[j+1];

str[j+1]=temp;

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

System.out.println(str[i]);

53
WORKING OF THE PROGRAM:
The program accepts a word from the user. It then enters a for-loop to count the number of words in the
sentence. A String array to store all the words is then created. Next, another for-loop takes each word
separately and stores each word in the array. Then, using Bubble Sort, each word is sorted in alphabetical
order. All words are converted to upper case before doing so. This is because any word that begins with a
capital letter will be arranged before all other words irrespective of the letter (for example, “Why” will be
arranged before “do” as the ASCII code of capital letters are lower than that of lower case letters) . After
all the words have been converted to upper case and arranged in alphabetical order, the words are printed
in alphabetical order.

OUTPUT:

Variable Description:

PROGRAM 18
VARIABLE DESCRIPTION
String x stores the entered words
int i it is a control variable
int j it is a control variabele
int k it is used toreach the position of last letter of the word

int words number of words in the sentence


String temp used in swapping
String str[] stores the words is alphabetical order

54
Program 19: Arranging the contents of a
given String array in alphabetical order
A String array is given with the names of 5 cities – Delhi, Bangalore, Agra, Mumbai and Calcutta. The
contents of this array are then arranged in alphabetical order and displayed.

CODE:
import java.util.*;

class P19_AlphabetizingCity

public static void main(String[] args)

String CityNames[] = {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"};

String Temp;

int I,J;

System.out.println("Set of city names before being arranged in alphabetical order:");

for(I=0;I<CityNames.length;I++)

if(I>=0&&I<4)

System.out.print(CityNames[I]+", ");

else

System.out.print("and "+CityNames[I]+".");

55
System.out.println();

System.out.println();

for(I=0;I<CityNames.length;I++)

for(J=0;J<CityNames.length-1;J++)

if(CityNames[J].compareTo(CityNames[J+1])>=0)

Temp=CityNames[J];

CityNames[J]=CityNames[J+1];

CityNames[J+1]=Temp;

System.out.println();

System.out.println("Set of city names after being arranged in alphabetical order:");

for(I=0;I<CityNames.length;I++)

if(I>=0&&I<4)

System.out.print(CityNames[I]+", ");

else

System.out.print("and "+CityNames[I]+".");

56
}

WORKING OF THE PROGRAM:


A String array is given with the names of 5 cities – Delhi, Bangalore, Agra, Mumbai and Calcutta. The
program then displays the contents of the array as is – without arranging them. The program then arranges
the cities in alphabetical order and displays the contents of the array after sorting the array.

OUTPUT:

VARIABLE DESCRIPTION:
Variable Name Data Type Purpose
To store the name of the cities to
CityNames String array
be sorted
I int Control variable
J int Control variable
Helps in exchanging the positions
Temp String
of two variables

57
Program 20: Printing various patterns on the screen
The program has two methods. The first method prints the following pattern on the screen:

* *

* *

* *

* *

The second method prints the following pattern on the screen:

543212345

4321234

32123

212

CODE:
public class twenty

public void pattern_1()

inti,j;

for(i=6;i>0;i--)

for(j=6;j>i;j--)

System.out.print(" ");

58
for(j=i;j>0;j--)

System.out.print(j);

for(j=2;j<=i;j++)

System.out.print(j);

System.out.println();

public void pattern_2()

inti,j;

for(i=0;i<3;i++)

for(j=0;j<i;j++)

System.out.print(" ");

System.out.print("*");

for(j=i+2;j<5-i;j++)

System.out.print(" ");

if(j+1>=5-i)

59
{

System.out.print("*");

break;

System.out.println();

for(i=2;i>0;i--)

for(j=1;j<i;j++)

System.out.print(" ");

System.out.print("*");

for(j=i;j<5-i;j++)

System.out.print(" ");

if(j+1>=5-i)

System.out.print("*");

break;

System.out.println();

60
}

WORKING OF THE PROGRAM:


In the first method, there are two control variables - I and J. There is a nested for-loop where the program
checks when to print an asterisk. If both I and J equal each other or have a sum of 6, an asterisk is printed.
Else, a space is printed.

In the second method, there is one for-loop with three for-loops nested within it. The first for-loop prints
spaces as an inverted pyramid is to be achieved. The pyramid is then divided into two parts – each being
printed by a separate for-loop. In the end, the result is achieved and an inverted pyramid is displayed on
the screen.

OUTPUT:

Variable Description:

PROGRAM 20

VARIABLE DESCRIPTION

int i it is a control variable

int j it is a control variable

61
TEACHER’S REMARKS

1. Examiner I:

2. Examiner II:

3. Total Marks:

Principal’s Signature

62
63
64

You might also like