0% found this document useful (0 votes)
37 views313 pages

Ilovepdf Merged (1) Merged

Uploaded by

ribhuthe12
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)
37 views313 pages

Ilovepdf Merged (1) Merged

Uploaded by

ribhuthe12
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/ 313

NAME- ESHAAN ROY

CLASS- XII

SECTION-A

U.I.D.- 7753831

TOPIC - ISC COMPUTER


PROJECT
1. Write a program to display the factors of a number:

Algorithm:

Start

Step 1: input n

Step 2: i=1

Step 3: while i<n [Repeat 4 &5]

Step 4: if n % i==0 then print i

Step 5: i= i+1

Stop

Code:

import java.util.*;

class factors

public static void main()

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int i=1;

System.out.println(“Factors are:”);

while(i<=n)

if(n%i==0)

{
System.out.println(i);

i=i+1;

} }

Output:

Enter the number

15

Factors are:

15

Variable List:

Variable Data Description


Name Type

n int Stores an integer number

i int Loop variable

2. Write a program to check whether a number is prime or not

Algorithm:

Start

Step 1:input n

Step 2:i=1,counter=0
Step 3:while i<n [repeat 4&5]

Step 4:if n%i==0 then counter++

Step 5:i=i+1

Step 6:print counter

Stop

Code:

import java.util.*;

class prime

public static void main()

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int i=1,c=0;

while(i<=n)

if(n%i==0)

c++;

i=i+1;

if(c==2)

{
System.out.println("It is a prime number");

else

System.out.println("Not a prime number");

Output

Enter the number

It is a prime number

Variable list

Name Type Description

n int Number to be checked by the


user

I int Iteration number for loop

c int Counter

3. Write a program to check whether a number is perfect or not

Algorithm:

Start
Step 1:input n

Step 2:i=1,s=0

Step 3:while i<n [repeat 4&5]

Step 4:if n%i==0 then s=s+i

Step 5:i=i+1

Step 6:if s==n then print “perfect number”

Step 7:else print “not a perfect number”

Stop

Code:

import java.util.Scanner;

class perfect

public static void main()

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int i=1,s=0;

while(i<n)

if(n%i==0)

s=s+i;

i=i+1;
}

if(s==n)

System.out.println("Perfect number");

else

System.out.println("Not a perfect number");

Output

Enter the number

Perfect number

Variable list

Name Type Description

n int accepts the number to be checked


from user

i int iteration number for loop

c int stores the sum

4. Write a program to check whether a number is twin-prime or


not:

Algorithm:

Start

Step 1:input n and m


Step 2:i=1,c=0

Step 3:while i<=n [repeat 4&5]

Step 4:if(n%i==0) then c++

Step 5:i=i+1

Step 6:j=1,d=0

Step 7:while j<=m [repeat 8 &9]

Step 8:if(m%j==0) then d++

Step 9:j=j+1

Step 10:if(c==2 && d==2) then if(m-n==2||n-m==2),then print


“twin-prime”

Step 11: else print “not twin-prime”

Stop

Code:

import java.util.Scanner;

class twin_prime

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter two numbers");

int n=sc.nextInt();

int m=sc.nextInt();

int i=1,c=0;

while(i<=n)

if(n%i==0)
{

c++;

i=i+1;

int j=1,d=0;

while(j<=m)

if(m%j==0)

d++;

j=j+1;

if(c==2 && d==2)

if(m-n==2||n-m==2)

System.out.println("It's a twin-prime number");

else

System.out.println("It's not a twin-prime


number");

}
}

Output:

Enter two numbers

11

13

It's a twin-prime number

Variable list

Name Type Description

n int Number to inputted to checked

m int Number to inputted to checked

i int Iteration for loop

c int 1st counter

d int 2nd counter

5. Write a program to check whether a number is Kaprekar or


not:

Algorithm:

Start

Step 1:input n

Step 2:t=n

Step 3:c=0

Step 4:while n>0 [repeat 5 &6]

Step 5:n=n/10

Step 6:c=c+1

Step 7:v=t*t
Step 8:d1=v%10^c

Step 9:d2=v/10^c

Step 10:if (t=d1+d2) then print “Kaprekar”

Step 11: else print “It's not a Kaprekar number"

Code:

import java.util.Scanner;

class Kaprekar

public static void main()

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int t,c=0;

t=n;

while(n>0)

n=n/10;

c=c+1;

int v=t*t;

int d1=v%(int)Math.pow(10,c);

int d2=v/(int)Math.pow(10,c);

if(t==d1+d2)

{
System.out.println("It's a Kaprekar number");

else

System.out.println("It's not a Kaprekar number");

Output:

Enter a number

45

It's a Kaprekar number

Variable list:

Name Type Description

n int stores the number entered by the


user

t int stores the value of n

c int counter

v int stores sqaure of the number


entered by user

d1 int stores value of 1st calculation

d2 int stores value of 2nd calculation

6. Write a program to check whether a number is automorphic


or not:
Algorithm:

Step 1:input n

Step 2:t=n

Step 3:c=0

Step 4:while n>0 [repeat 5 & 6]

Step 5:n=n/10

Step 6:c=c+1

Step 7:v=t*t

Step 8:if(t==v%10^c) then print “Automorphic” otherwise not

Code:

import java.util.Scanner;

class automorphic

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number to be checked");

int n=sc.nextInt();

int t=n;

int c=0;

while(n>0)

n=n/10;

c=c+1;
}

int v=t*t;

if(t==v%(int)Math.pow(10,c))

System.out.println("Automorphic number");

else

System.out.println("Not an Automorphic number");

} }

Output:

Enter the number to be checked

25

Automorphic number

Variable list:

Name Type Description

n int stores the number to be checked

T int stores temporary value for n

c int Counter

v int stores the square of t

7. Write a program to check whether a number is Disarium or


not:

Algorithm:
Step 1:input n

Step 2:t=n

Step 3:c=0

Step 4:while t>0 [repeat 5 & 6]

Step 5:t=t/10

Step 6:c=c+1

Step 7:t=n

Step 8:s=0

Step 9:while t>0 [repeat 10 to 13]

Step 10:d=t%10

Step 11:s=s+d^ c

Step 12:t=t/10

Step 13:c=c-1

Step 14:if s==n print ”Disarium number”

Step 15:else print “Not a disarium number”

Code:

import java.util.Scanner;

class disarium

void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value to be checked");

int n=sc.nextInt();

int t=n;
int c=0;

while(t>0)

t=t/10;

c=c+1;

t=n;

int s=0;

while(t>0)

int d=t%10;

s=s+(int)Math.pow(d,c);

t=t/10;

c=c-1;

if(s==n)

System.out.println("it is a Disarium number");

else

System.out.println("it is not a Disarium number");

}
Output:

Enter the value to be checked

135

it is a Disarium number

Variable list:

Name Type Description

n int stores the number to be checked

t int stores temporary value for n

c int counter

s int stores and calculates the sum

8. Write a program to check whether a number is a Fascinating


number or not:

Algorithm:

Start

Step 1: input n

Step 2: n2=n* 2

Step 3: n3=n*3

Step 4: string s=n1+””n2+n3

Step 5: boolean f=true

Step 6: for (char a='1';a<='9';a++) [repeat 6 to 15]

Step 7:c=0

Step 9: for(int i=0;i<s.length();i++)[repeat 9 to 12]

Step 10: char ch=s.charAt(i);

Step 11:if(ch==a)
Step 12:c++

Step 13:if(c>1 || c==0)

Step 14:f=false

Step 15:break

Step 16: if(f) then print “Fascinating number” else not

Stop

Code:

import java.util.Scanner;

class Fascinating

void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number to be checked");

int n=sc.nextInt();

int n2=n*2;

int n3=n*3;

String s=n+""+n2+n3;

boolean f=true;

for(char a='1';a<='9';a++)

int c=0;

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

{
char ch=s.charAt(i);

if(ch==a)

c++;

if(c>1 || c==0)

f=false;

break;

if(f)

System.out.println("it is a Fascinating number");

else

System.out.println("it is not a Fascinating number");

} }

Output:

Enter the number to be checked

192

it is a Fascinating number.

Variable list:

Name Type Description

n int stores the original number from


the user

n2 int original number’s value


multiplied by 2
n3 int original number’s value
multiplied by 3

s String concatenates values of n,n2,n3

f boolean flag

a char loop initiator

i int positon of letter(index)

c int count

9. Write a program to check whether a number is a Smith number


or not:

Algorithm:

Step 1: define a function boolean isSmithnumber(n)

Step 2: int digitSum = 0

Step 3: temp=n

Step 4: while(temp>0) [repeat 5 to 6]

Step 5: digitSum=digitSum+temp % 10;

Step 6: temp=temp/10;

Step 7: primefsum=0

Step 8: f=2

Step 9: while (n > 1)

Step 10: if n % f == 0 then primefSum += getDigitSum(f);

Step 10: else f++

Step 11: return (digitSum == primefSum)

Step 12: define a function getdigitsum(n)

Step 13: sum=0


Step 14: while(n>0) [repeat 15 & 16]

Step 15: sum=sum+n%10;

Step 16: n=n/10;

Step 17: return sum

Step 18: input num

Step 19: if (isSmithnumber(num)) then print it is a smith


number

Step 20: else print it is not a smith number

Code:

import java.util.*;

class Smith

boolean isSmithnumber(int n)

int digitSum = 0;

int temp = n;

while (temp > 0)

digitSum=digitSum+temp % 10;

temp=temp/10;

int primefSum = 0;

int f = 2;
while (n > 1)

if (n % f == 0)

primefSum += getDigitSum(f);

n=n/f;

else

f++;

return (digitSum == primefSum);

int getDigitSum(int n)

int sum = 0;

while (n > 0)

sum=sum+n%10;

n=n/10;

return sum;

void main()
{

Scanner sc = new Scanner(System.in);

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

int num = sc.nextInt();

if (isSmithnumber(num))

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

else

System.out.println(num + " is not a Smith


number.");

Output:

Enter a number: 22

22 is a Smith number.

Variable List:

Variable Data Type Description


Name

n int number to be used for


checking

Digitsum int sum of the digits of the


number

Temp int stores temporary value for


n
Primefsum int stores sum of the prime
factors of the number

f int Flag variable

Sum int stores sum of

Num int number to be checked by the


user

isSmithnumb boolean function to check smith


er number

getDigitSum Int stores sum of the digits of


the number

10.Write a program to check if it is a triangular number or


not

Algorithm:

Start

Step 1:void display() (rep.

Step 2: input n

Step 3:int i,j,s

Step 4:for (i = 1; i <= n; i++)

Step 5:s=0

Step 6:for (j = 1; j <= i; j++)

Step 7:s = s + j

Step 8:if(s<=n) print(s)

Stop

Code:

import java.util.Scanner;

class Triangular
{

void display()

Scanner sc = new Scanner(System.in);

System.out.println("Enter the limit");

int n = sc.nextInt();

int i, j, s;

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

s = 0;

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

s = s + j;

if (s <= n)

System.out.println("Triangular numbers upto


"+n+" is"+s);

Output:

Enter the limit:15

Triangular Numbers upto 15


1
3
6
10
15

Variable list:

Variable Name Data Type Description


n int Number to be checked
i int Loop variable
j int Loop variable
s int Stores sum.

11.Check whether a number is a Dudeney number or not:

Algorithm:

Step 1: input n

Step 2: t=n,s=0

Step 3: while(t>0)

Step 4: d=t%10

Step 5: s+=d

Step 6: t=t/10

Step 7: if s==Math.cbrt(n), then print Dudeney number

Code:

import java.util.Scanner;

class Dudeney

void display()
{

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int t=n,s=0;

while(t>0)

int d=t%10;

s=s+d;

t=t/10;

if(s==(Math.cbrt(n)))

System.out.println("It is a Dudeney number");

else

System.out.println("It is a Dudeney number");

} }

Output:

Enter a number

512

It is a Dudeney number

Variable List:
Variable Data Description
Name Type

n int Accepts number from user

t int Stores a copy of n

d int Digit separator

s int Stores the calculation for


the number

12. Write a program to print the factorials of a number.

Algorithm:

Start

Step 1: int n,f

Step 2: num() [line 3]

Step 3: n=0,f=1

Step 4: void input() [line 5]

Step 5: input n

Step 6: void fact() [line 7-10]

Step 7: i=1,f=1

Step 8: while(i<=1) [line 9-10]

Step 9: f=f*i

Step 10:i++

Step 11:void display() [line 12-13]

Step 12:fact()

Step 13:print("Factorial is:"+f)

Stop

Code:

import java.util.Scanner;
class nfactor

public static void main()

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt();

int i=1;

while(i<n)

if(n%i==0)

System.out.println(i);

i=i+1;

Output:

Enter the number:8

Factors of the number:1,2,4

Variable List:

Variable Name Data Type Description


n int Accepts an
integer
f int Stores the
factorial
i int Loop variable

13.Write a program to print the sum of factors of a number.

Algorithm:

Start

Step 1: num1() [line 2-3]

Step 2: s=0,n=0

Step 3: void input() [line 4]

Step 4: input n

Step 5: void sumfact() [line 6-8]

Step 61: for(i=1;i<=n;i++) [Rep: 7-8]

Step 7: if(n%i==0) [rep 8]

Step 8: s=s+i

Step 9: void display() [line 10-11]

Step 10:sumfact()

Step 11:print("Sum of factors is:"+s)

Stop

Code:

import java.util.Scanner;

class num1

int n,s;

void num()

s=0;

n=0;
}

void input()

Scanner sc=new Scanner(System.in);

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

n=sc.nextInt();

void sumfact()

int i;

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

if(n%i==0)

s=s+i;

} }

void display()

{ s=0;

sumfact();

System.out.println("The sum of the factors of "+n+"


is "+s);

} }

Output:

The sum of the factors of 5 is 6

Variable list:

Variable Name Data Type Description


n int Stores number to be checked
s int Stores sum of factors
i int Loop variable

14.Write a program to print the reverse of a number.

Algorithm:

Start

Step1:num2() line-2

Step2:n=1,r=1

Step3:void input() line4

Step4:input n

Step5:void rev() line 6-9

Step6:while(n!=0) rep line 7-9

Step7:d=n%10

Step8:r=r*10+d

Step9:n=n/10

Step10:void display() line11-12

Step11:print("Reversed number"+r)

Stop

Code:

import java.util.Scanner;

class num2

int n,r;

num2()
{

n=1;

r=1;

void input()

Scanner sc=new Scanner(System.in);

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

n=sc.nextInt();

void rev()

while(n!= 0)

int d = n % 10;

r = r * 10 + d;

n=n/10;

void display()

rev();

System.out.println(" Reversed number:"+r);

} }

Output:

Enter a number:

451
Reversed number:154

Variable List:

Variable Name Data Type Description


n int Stores number
r int Stores the last digits

15.Write a program to accept a number and print the number of


factors.

Algorithm:

Start

Step 1: num3()

Step 2: c=0;

Step 3: n=0;

Step 4: i=0;

Step 5: void input()

Step 6: input n

Step 7: void count()

Step 8: c=0;

Step 9: i=1;

Step 10: while(i<=n) (Rep.11to12)

Step 11: if(n%i==0) then c++;

Step 12: i++;

Step 13: void display()

Step 14: count();

Step 15: print("The no. of factors is:"+c);

Stop

Code:
import java.util.Scanner;

class num3

int n,c,i;

void num()

c=0;

n=0;

void input()

Scanner sc=new Scanner(System.in);

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

n=sc.nextInt();

void count()

c=0;

i=1;

while(i<=n)

if(n%i==0)

c++;

i++;

}
}

void display()

c=0;

count();

System.out.println("The no. of factors is:"+c);

} }

Output:

Enter the number:

The no. of factors is:2

Variable List:

Variable Name Data Type Description


n int Stores number to be checked
c int counter
i int Loop variable

16: Write a program to accept a number and check whether it is


a sumproduct number or not.

Algorithm:

Start

Step 1:int n

Step 2:sumproduct() (rep. 2 to 3)

Step 3:n=0
Step 4:void readnum() (rep. 4 to 5)

Step 5:input n

Step 6:int sum(int v) (rep. 6 to 12)

Step 7:d=0,s=0

Step 8:while(v>0) (rep. 9 to 11)

Step 9:d=v%10

Step 10:s=s+d

Step 11:v=v/10

Step 12:return s

Step 13:int product(int v) (rep. 13 to 19)

Step 14:d=0,p=1

Step 15:while(v>0) (rep. 15 to 18)

Step 16:d=v%10

Step 17:p=p*d

Step 18:v=v/10

Step 19:return p

Step 20:void check()

Step 21:int a=sum(n)

Step 22:int b=product(n)

Step 23:if(a*b==n) then print ("It is a sumproduct number")

Step 20:else print ("It is not a sumproduct number")

Stop

Code:

import java.util.Scanner;

class sumproduct

int n;
sumproduct()

n=0;

void readnum()

Scanner sc=new Scanner(System.in);

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

n=sc.nextInt();

int sum(int v)

int d=0,s=0;

while(v>0)

d=v%10;

s=s+d;

v=v/10;

return s;

int product (int v)

int d=0,p=1;

while(v>0)

d=v%10;
p=p*d;

v=v/10;

return p;

void check()

int a=sum(n);

int b=product(n);

if(a*b==n)

System.out.println("It is a sumproduct number");

else

System.out.println("It is not a sumproduct


number");

Output:

Enter a number

144

It is a sumproduct number

Variable List:

Variable name Data Type Description


n int Stores input from user
v int Formal parameter
d int Stores remainder
s int Stores sum of remainder

v int Stores quotient of the


number
p int Stores product of the
remainder
a int Stores the sum
b int Stores the product

17.Write a program to enter a number and check if it is an


emirp number or not.

Algorithm:

Start

Step 1:int n,rev,f

Step 2:void input() (rep. 2 to 3)

Step 3:input n

Step 4:void isPrime() (rep. 4 to 7)

Step 5:int i

Step 6:for(i=2;i<=n/2;i++) (rep. 6 to 7)

Step 7:if(n%i==0) then f=1 and return

Step 8:void isEmirp() (rep. 8 to 20)

Step 9:int t=n,rem

Step 10:rev=0

Step 11:while(n!=0) (rep. 11 to 14)

Step 12:rem=n%10

Step 13:rev=rev*10+rem

Step 14:n=n/10

Step 15:Emirp obj = new Emirp()


Step 16:obj.n = rev

Step 17: obj.isPrime()

Step 18:if (f == 0 && obj.f == 0) then print("Both the


original number and the reversed number are prime");

Step 19:else if(f == 0 && obj.f != 0) then print("The


original number is prime but the reversed number is not
prime")

Step 20:else if(f != 0 && obj.f == 0) then print ("The


original number is not prime but the reversed number is
prime");

Step 21:else print("Both the original number and the reversed


number are not prime");

Stop

Code:

import java.util.Scanner;

class Emirp

int n,rev,f;

void input()

Scanner sc=new Scanner(System.in);

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

n=sc. nextInt();

void isPrime()

int i;

for (i=2;i<=n/2;i++)

{
if(n%i==0)

f=1;

return;

void isEmirp()

int t = n,rem;

rev = 0;

while (n != 0)

rem=n%10;

rev=rev*10+rem;

n=n/10;

Emirp obj = new Emirp();

obj.n = rev;

obj.isPrime();

if (f == 0 && obj.f == 0)

System.out.println("Both the original number and the


reversed number are prime.It is an EMIRP number");

else if (f == 0 && obj.f != 0)

{
System.out.println("The original number is prime
but the reversed number is not prime");

else if (f != 0 && obj.f == 0)

System.out.println("The original number is not


prime but the reversed number is prime");

else

System.out.println("Both the original number and


the reversed number are not prime");

Output:

1.) Enter the number

35

The original number is not prime but the reversed number


is prime.

2.) Enter the number

11

Both the original number and the reversed number are


prime.It is an EMIRP number.

Variable List:

Variable List Data Type Description


n int Stores the number from the
user
rev int Stores reverse of the number
f int Flag variable
i int Loop variable

18.Write a program to input a number and print it along with


its octal equivalent.

Algorithm:

Start

Step 1: int n,oct

Step 2: DeciOct() (rep. 2 to 3)

Step 3: oct=0,n=0;

Step 4: void getnum() (rep. 4 to 6)

Step 5: print"Enter a number"

Step 6: input n

Step 7: void deci_oct() (rep. 7 to 15)

Step 8: t=n,s=0;

Step 9: while(t>0) (rep. 9 to 12)

Step 10: d=t%8;

Step 11: s=s*10+d;

Step 12: t=t/8;

Step 13: while(s>0) (rep. 13 to 15)

Step 14: oct=oct*10+(s%10);

Step 15: s=s/10;

Step 16: void show() (rep. 16 to 20)

Step 17: oct=0;

Step 18:deci_oct();

Step 19: print ("Decimal no. : "+n);

Step 20: print ("Octal equivalent no. : "+oct);


Stop

Code:

import java.util.Scanner;

class DeciOct

int n,oct;

DeciOct()

n=0;

oct=0;

void getnum()

Scanner sc=new Scanner(System.in);

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

n=sc. nextInt();

void deci_oct()

int t=n,s=0;

while(t>0)

int d=t%8;

s=s*10+d;

t=t/8;

}
while(s>0)

oct=oct*10+(s%10);

s=s/10;

void show()

{ oct=0;

deci_oct();

System.out.println("Decimal no. : "+n);

System.out.println("Octal equivalent no. : "+oct);

} }

Output:

Enter a number

55

Decimal no. : 55

Octal equivalent no. : 67

Variable List:

Variable list Data type Description


n int Stores number from the user
oct int Stores octal number
t int Temporary variable storing
quotient
s int Stores sum of remainder
d int Stores the remainder
19.Write a program to print the sum of the given series.

x^2/1! + x^4/3! + x^6/5! +........x^n/(n-1)!

Algorithm:

Start

Step 1: int x,n;

Step 2: double sum;

Step 3: SeriesSum(int xx,int nn) (rep. 3 to 5)

Step 4: x=xx;

Step 5: n=nn;

Step 6: double findfact(int m) (rep. 6 to 10)

Step 7: i, f=1;

Step 8: for(i=1;i<=m;i++)

Step 9: f=f*i;

Step 10: return f;

Step 11: double findpower(int x,int y) (rep. 11 to 13)

Step 12: p=(int)(Math.pow(x,y));

Step 13: return p;

Step 14: void calculate() (rep. 14 to 18)

Step 15: for(int i=2;i<=n;i++)

Step 16: a= findpower(x,i);

Step 17: b= findfact(i-1);

Step 18: sum=a/b;

Step 19: void display() (rep. 19 to 21)

Step 20: calculate();

Step 21: print sum;

Stop

Code:
import java . util .*;

class SeriesSum1 {

int x; // integer number

int n; // number of terms

double sum; // sum of the series

public SeriesSum1(int xx, int nn) {

x = xx;

n = nn;

sum = 0.0; // Initialize sum to 0

// Calculate factorial of a number

double findFact(int m) {

if (m == 0) {

return 1.0;

} else {

return m * findFact(m - 1);

// Calculate power of x raised to the power y

double findPower(int x, int y) {

return Math.pow(x, y);

// Calculate the sum of the series


void calculate() {

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

double term = findPower(x, i) / findFact(i - 1);

sum += term;

// Display the sum of the series

void display() {

System.out.println("Sum of the series: " + sum);

static void main(String[] args) {

int x = 2; // Set your desired values

int n = 5;

SeriesSum series = new SeriesSum(x, n);

series.calculate();

series.display();

Output:
Sum of the series: 1.2404667449597834E14

Variable List:

Variable Name Data Description


Type
x int Formal parameter
n int Formal parameter
sum double Stores sum
m int Formal parameter
i int Loop variable
f int factorial
p int Stores the value of the power
a int Stores the values raised to
their respective powers
b int Stores factorial

20.Write a program to accept a number and check if it is a Spy


Number or not.

Algorithm:

Start

Step 1:int sum=0,pro=1,l

Step 2:void main() (rep. 2 to 10)

Step 3:input n

Step 4:while(n<0) (rep. 4 to 8)

Step 5:l=n%10

Step 6:sum=sum+l

Step 7:pro=pro*l

Step 8:n=n/10

Step 9:if(sum==pro) then print ("its a spy number")

Step 10:else ("its not a spy number")

Stop

Code:

import java . util . *;


class spynum

int sum=0,pro=1, l;

void main()

Scanner sc = new Scanner(System.in);

System.out.println("pls enter number");

int n= sc.nextInt();

while(n>0)

l = n%10;

sum =sum+l;

pro=pro*l;

n = n/10;

if( sum == pro )

System.out.println("its a spy number");

else

System.out.println("its not a spy number");

} } }

Output:

Pls enter number

1124

It’s a spy number


Variable List:

Variable Name Data Type Description


sum int Stores sum of remainder
pro int Stores product of remainder
l int Stores remainder
n int Stores the number from the
user

21.Write a program to accept a number and check whether it is


an Armstrong number or not.

Algorithm:

Start

Step 1:public static void main() (rep. 1 to 8)

Step 2:input longnum

Step 3:long b=num, len=Long.toString(num).length(),sum=0

Step 4:while(b!=0) (rep. 4 to 6)

Step 5:sum=sum+ (long)Math.pow(b % 10, len)

Step 6: b =b/ 10;

Step 7:if(sum == num) then print (num+ “ is Armstrong”)

Step 8:else print (num + " is not Armstrong")

Stop

Code:

import java.util.*;

class Armstrong

{ int n, s;

Armstrong ()
{n=0;

s=0;

void input ()

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

System.out.println("Enter a no.: ");

n = sc. nextInt();

void sumcal()

{ int sn=n;

while (sn>0)

{ int r=sn%10;

s = s + (int)Math.pow(r,3);

sn = sn / 10;

void check()

{ if (s == n)

System.out.print ("No. is Amstrong");

else

System.out.println("No. ain't Armstrong");

void main()

{ Armstrong as=new Armstrong();

as.input();

as.sumcal();

as.check();
}

Output:

Enter a no:

153

No. is Amstrong

Variable List:

Variable List Data Type Description


n int Stores the number from the user
s int Stores sum of the cubes of the
digits
sn int copy variable(stores value of
n))
r int Stores the remainder

22.Write a program to accept a number and check whether it is


an evil number or not.

Algorithm:

Start

Step 1:public static void main() (rep.1 to

Step 2:input n

Step 3:if(n<0) then print("Invalid Input") return;

Step 4:int count=0

Step 5:int p=0

Step 6:int binNum=0

Step 7:while(n>0) (rep. 7 to 12)


Step 8:int d=n%2

Step 9:if(d==1) then count++

Step 10:binNum+=(int)(d * Math.pow(10, p))

Step 11:p++

Step 12:n /= 2

Step 13:print("Binary Equivalent: " + binNum)

Step 14:print("No. of 1's: " + count)

Step 15:if (count % 2 == 0) then print("Output: Evil Number")

Step 16:else print("Output: Not an Evil Number")

Stop

Code:

import java.util.Scanner;

class EvilNumber

void main()

Scanner sc = new Scanner(System.in);

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

int n =sc.nextInt();

if (n < 0)

System.out.println("Invalid Input");

return;

int count = 0;

int p = 0;

int binNum = 0;
while (n > 0) {

int d = n % 2;

if (d == 1)

count++;

binNum += (int)(d * Math.pow(10, p));

p++;

n /= 2;

System.out.println("Binary Equivalent: " + binNum);

System.out.println("No. of 1's: " + count);

if (count % 2 == 0)

System.out.println("Output: Evil Number");

else

System.out.println("Output: Not an Evil Number");

} }

Output:

Enter a positive number: 15

Binary Equivalent: 1111

No. of 1's: 4

Output: Evil Number

Variable List:

Variable list Data type Description


n int Stores number from user
count int Stores the count
p int Stores power
binNum int Stores the binary number
d int Stores the remainder

23.Write a program to accept a sentence and display each word


in the sentence along with the number of vowels in that word.

Algorithm:

Start

Step 1:String Line

Step 2:Sentence() (rep. 2 to 3)

Step 3:input line

Step 4:void count() (rep. 4 to 19)

Step 5:int startIndex=0

Step 6:int lastInd=0

Step 7:while(lastInd<line.length()) (rep. 7 to 19)

Step 8:if (line.charAt(lastInd)=='


'||lastInd==line.length()-1)

Step 9:if (lastInd== line.length()-1)

Step 10:lastInd++;

Step 11:String word = line.substring(startIndex,lastInd);

Step 12:int vowelCount = 0;

Step 13:for (char ch : word.toLowerCase().toCharArray())

Step 14:if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

Step 15:vowelCount++

Step 16:System.out.println("No of vowels in " + word + "-" +


vowelCount)

Step 18:startIndex=lastInd+1

Step 19:lastInd++

Step 20: void main() (rep. 21 to 23)


Step 21:Sentence obj = new Sentence()

Step 22:obj.input()

Step 23:obj.count()

Stop

Code:

import java.util.Scanner;

class Sentence

String line;

Sentence()

line="";

void input()

Scanner sc = new Scanner(System.in);

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

line = sc.nextLine();

void count()

int startIndex=0;

int lastInd=0;

while (lastInd<line.length())

if (line.charAt(lastInd)=='
'||lastInd==line.length()-1)
{

if (lastInd== line.length()-1)

lastInd++;

String word =
line.substring(startIndex,lastInd);

int vowelCount = 0;

for (char ch :
word.toLowerCase().toCharArray())

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

vowelCount++;

System.out.println("No of vowels in " + word


+ "-" + vowelCount);

startIndex=lastInd+1;

lastInd++;

public static void main()

Sentence obj = new Sentence();

obj.input();
obj.count();

} }

Output:

Enter a sentence:

Kolkata is my city

No of vowels in Kolkata-3

No of vowels in is-1

No of vowels in my-0

No of vowels in city-1

Variable List:

Variable Name Data Type Description


line String Stores a string
lastInd int Stores last index position of
character
startIndex int Stores first index position of
character
vowelCount int Counts number of vowels

24.Write a program to input a full name and then print the


initials along with the surname.

Algorithm:

Start

Step 1: input str

Step 2: str= str.trim();

Step 3: str = " "+str;

Step 4: c1=0,c2=0;

Step 5: for(i=0;i<str.length();i++) (Rep.6to7)


Step 6: if(Character.isWhitespace(str.charAt(i)))

Step 7: if(i!=str.lastIndexOf(' ')) then


print(str.charAt(i+1)+".");

Step 8: print(" "+str.substring(str.lastIndexOf(' ')+1));

Stop

Code:

import java.util.*;

class Initials

void display()

Scanner sc = new Scanner(System.in);

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

String str = sc.nextLine();

str= str.trim();

str = " "+str;

int i,j;

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

if(Character.isWhitespace(str.charAt(i)))

if(i!=str.lastIndexOf(' '))

System.out.print(str.charAt(i+1)+".");

}
System.out.print(" "+str.substring(str.lastIndexOf('
')+1));

} }

Output:

Enter a name

Mohandas Karamchand Gandhi

M.K. Gandhi

Variable List:

Variable Name Data Type Description


str int Stores a string
i int Loop variable
j int Loop variable

25.Write a program to input two numbers and then merge the two
numbers.

Algorithm:

Start

Step 1: long n1,n2,mergNum;

Step 2: merger() (line 3to5)

Step 3: n1=0;

Step 4: n2=0;

Step 5: mergNum=0;

Step 6: void readNum() (line 7to9)

Step 7: print ("Enter two numbers");

Step 8: n1=sc.nextLong();

Step 9: n2=sc.nextLong();

Step 10: joinNum() (line 11to16)

Step 11: num1="";


Step 12: num2="";

Step 13: num1=Long.toString(n1);

Step 14: num2=Long.toString(n2);

Step 15: String merg=num1.concat(num2);

Step 16: mergNum=Integer.valueOf(merg);

Step 17: void show() (line 18to20)

Step 18: print ("1st number: "+n1);

Step 19: print ("2nd number "+n2);

Step 20: print ("merged number:"+mergNum);

Step 21: void main() (line 22to25)

Step 22: merger ob=new merger();

Step 23: ob.readNum();

Step 24: ob.joinNum();

Step 25: ob.show();

Stop

Code:

import java.util.*;

class merger

Scanner sc=new Scanner(System.in);

long n1,n2,mergNum;

merger()

n1=0;

n2=0;

mergNum=0;
}

void readNum()

System.out.println("Enter two numbers");

n1=sc.nextLong();

n2=sc.nextLong();

void joinNum()

String num1="";

String num2="";

num1=Long.toString(n1);

num2=Long.toString(n2);

String merg=num1.concat(num2);

mergNum=Integer.valueOf(merg);

void show()

System.out.println("1st number: "+n1);

System.out.println("2nd number "+n2);

System.out.println("merged number:"+mergNum);

void main()
{

merger ob=new merger();

ob.readNum();

ob.joinNum();

ob.show();

} }

Output:

Enter two numbers

45

54

1st number: 45

2nd number 54

merged number:4554

Variable List:

Variable Data type Description


list
n1 long Actual parameter
n2 long Actual parameter
mergNum long Stores the merged
number
num1 String Coverts the value
from long to string
num2 String Coverts the value
from long to string
merg String Merges the two
strings
26.Design a class Exchange to accept a sentence and
interchange the first letter with the last letter of each word
of that sentence with single letter word remaining
unchanged.The words in the input sentence are separated by a
single blank space and terminated by a full stop.

Algorithm:

Start

Step 1:int size

Step 2:String rev,sent

Step 3:Exchange() (rep. 3 to 6)

Step 4:size=0

Step 5:sent=” “

Step 6:rev=””

Step 7:void readsentence() (rep. 7 to 8)

Step 8:input sent

Step 9:void exfirstlast() (rep. 9 to 20)

Step 10:readsentence();

Step 11:StringTokenizer st = new StringTokenizer(sent)

Step 12:while (st.hasMoreTokens()) (rep. 12 to 19)

Step 13:String word = st.nextToken()

Step 14:if (word.length() > 1)

Step 15:char first = word.charAt(0);

Step 16:char last = word.charAt(word.length() - 1)

Step 17:String middle = word.substring(1, word.length() - 1)

Step 18:rev += last + middle + first + " "

Step 19:rev += word + " "


Step 20:rev=rev.trim()

Step 21:void display() (rep. 21 to 24)

Step 22:exfirstlast();

Step 23:print("Original sentence: " + sent)

Step 24:print("New Sentence: " + rev)

Stop

Code:

import java.util.*;

class Exchange

int size;

String rev,sent;

Exchange()

size = 0;

sent = "";

rev = "";

void readsentence() {

Scanner sc = new Scanner(System.in);

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

sent = sc.nextLine();

void exfirstlast()

readsentence();

StringTokenizer st = new StringTokenizer(sent);


while (st.hasMoreTokens())

String word = st.nextToken();

if (word.length() > 1)

char first = word.charAt(0);

char last = word.charAt(word.length() - 1);

String middle = word.substring(1, word.length()


- 1);

rev += last + middle + first + " ";

else

rev += word + " ";

rev = rev.trim();

void display()

exfirstlast();

System.out.println("Original sentence: " + sent);

System.out.println("New Sentence: " + rev);

} }

Output:

Enter a sentence

It is a warm day
Original sentence: It is a warm day

New Sentence: tI si a marw yad

Variable List:

Variable Name Data Type Description


size int Stores length of string
sent String Stores a sentence
rev String Stores new sentence

27.Enter a number and print the number of vowels in the


string.

Algorithm:

Start
Step 1:String s
Step 2:str() (rep. 2 to 3)
Step 3:s=””
Step 4:void input() (rep. 4 to 6)
Step 5:Boolean check(char ch) (rep.5 to 7)
Step6:if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'
||ch=='e'||ch=='i'||ch=='o'||ch=='u') then return true
Step 7: else return false
Step 8:void count() (rep. 8 to 17)
Step 9:int l=s.length()
Step 10:int c=0
Step 11:boolean a
Step 12:for(int i=0;i<l;i++) (rep. 12 to 16)
Step 13:char ch1=s.charAt(i)
Step 14:a=check(ch1)
Step 15:if(a==true)
Step 16:c=c+1
Step 17:print("No of vowels: "+c)

Code:

import java.util.Scanner;

class str {

String s;

str() {
s = "";

void input() {

Scanner scanner = new Scanner(System.in);

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

s = scanner.nextLine();

boolean check(char ch) {

ch = Character.toLowerCase(ch);

return ch == 'a' || ch == 'e' || ch == 'i' || ch ==


'o' || ch == 'u';

void count() {

int vowelCount = 0;

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

if (check(s.charAt(i))) {

vowelCount++;

System.out.println("Number of vowels in the string: "


+ vowelCount);

void main()

str ob = new str();

ob.input();

ob.count();
}

Output:

Enter a string: elephant

Number of vowels in the string: 3

Variable List:

Variable Name Data Type Description


s string Store the string from the user
ch character Stores the character of the
string
vowelcount int Count the number of vowels

28.Enter a string and check if the string is palindrome or


not.

Algorithm:

Start

Step 1: String s;

Step 2: str2() (line 3)

Step 3: s="";

Step 4: void input() (line 5to6)

Step 5: println("Enter a String");

Step 6: input s

Step 7: String reverse(String x)

Step 8: rev = "";

Step 9: l = x.length();

Step 10: while(l!=0) (Rep. line 11to12 )

Step 11: rev = rev + x.charAt(l-1);

Step 12: l--;


Step 13: return rev;

Step 14: void check()

Step 15: rev = reverse(s)

Step 16: if(rev.equals(s)) then print (s+" is a Palindrome


string");

Step 17: else print (s+" is not a Palindrome string");

Stop

Code:

class str2

String s;

str2()

s="";

void input()

Scanner sc = new Scanner(System.in);

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

s = sc.next();

String reverse(String x)

String rev = "";

int l = x.length();

while(l!=0)

{
rev = rev + x.charAt(l-1);

l--;

return rev;

void check()

String rev = reverse(s);

if(rev.equals(s))

System.out.println(s+" is a Palindrome string");

else

System.out.println(s+" is not a Palindrome


string");

Output:

Input -> EYE

EYE is a Palindrome string

Variable List:

Variable Name Data Type Description


s String Accepts and stores a string
rev String Stores the reverse of the string
l int Stores length of string
29.Enter 16 values in a 4x4 matrix and find out the maximum
element of each row and minimum element of each column.

Algorithm:

Start

Step 1:int a[][]=new int[4][4]

Step 2:void input() (rep. 2 to 7)

Step 3:int i,j

Step 4:print("Enter 16 values for array")

Step 5:for(i=0;i<4;i++)

Step 6:for(j=0;j<4;j++)

Step 7:a[i][j]=sc.nextInt()

Step 8:void findMax() (rep. 8 to 14)

Step 9:for(int r=0;r<4;r++)

Step 10:int max=a[r][0]

Step 11:for(int c=0;c<4;c++)

Step 12:if(a[r][c]>max)

Step 13:max=a[r][c]

Step 14:print max

Step 15:void findMin() (rep. 15 to 21)

Step 16:for(int r=0;r<0;r++)

Step 17:int min=a[0][r]

Step 18:for(int c=0;c<4;c++)

Step 19:if(a[c][r]<min)

Step 20:min=a[c][r]

Step 21:print min

Step 22: void main() (rep. 22 to


Step 23:matrix_maxmin ob=new matrix_maxmin()

Step 24:ob.input();

Step 25: ob.findMax();

Step 26: ob.findMin();

Stop

Code:

import java.util.*;

class matrix_maxmin

Scanner sc=new Scanner(System.in);

int a[][]=new int[4][4];

void input()

int i,j;

System.out.println("Enter 16 values for array");

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

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

a[i][j]=sc.nextInt();

void findMax()

for(int r=0;r<4;r++)
{

int max=a[r][0];

for(int c=0;c<4;c++)

if(a[r][c]>max)

max=a[r][c];

System.out.println(max);

void findMin()

for(int r=0;r<0;r++)

int min=a[0][r];

for(int c=0;c<4;c++)

if(a[c][r]<min)

min=a[c][r];

}
void main()

matrix_maxmin ob=new matrix_maxmin();

ob.input();

ob.findMax();

ob.findMin();

Output:
Enter 16 values for array

10

11

12

13

23

23

43
23

result:

13

43

Variable List:

Variable list Data type Description


a int matrix
i int Loop variable
j int Loop variable
r int Loop for the rows
c int Loop for the column
max int Stores the max
value
min int Stores the min
value

30.Enter 16 values in a 4x4 matrix and print only the prime


numbers from that matrix

Algorithm:

Start

Step 1: int i,j;

Step 2: int a[][]=new int[4][4];

Step 3: void input() (Rep:4-7)

Step 4: print ("Enter 16 array elements");


Step 5:for (int i = 0; i < 4; i++)
(Rep:6-7)

Step 6:for (int j = 0; j < 4; j++) (Rep:7)

Step 7:a[i][j]=sc.nextInt()

Step 10:void check()


(Rep:11-23)

Step 11:print(“prime numbers in the matrix”)

Step 12:for (int r = 0; r < 4; r++)


(Rep:13-17)

Step 13:for (int c = 0; c < 4; c++)


(Rep:14-17)

Step 14:int v = a[r][c]

Step 15:if (v <= 1)


(Step:16)

Step 16: continue

Step 17: boolean isPrime = true

Step 18:for (int i = 2; i <= Math.sqrt(v);i++)


(Rep:19-23)

Step 19:if (v % i == 0)
(Rep:20-23)

Step 20:isPrime = false

Step 21:break

Step22:if (isPrime)
(Rep:23)

Step23:print(v)

Step24:void main()
(Rep:25-27)

Step25:MatrixPrime ob = new MatrixPrime()

Step26:ob.input()

Step27:ob.check()
Stop

Code:

import java.util.*;

class MatrixPrime

Scanner sc = new Scanner(System.in);

int a[][] = new int[4][4];

void input() {

System.out.println("Enter 16 values for the array");

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

a[i][j] = sc.nextInt();

void check()

System.out.println("Prime numbers in the matrix:");

for (int r = 0; r < 4; r++) {

for (int c = 0; c < 4; c++) {

int v = a[r][c];

if (v <= 1) {

continue;

boolean isPrime = true;

for (int i = 2; i <= Math.sqrt(v); i++) {

if (v % i == 0) {
isPrime = false;

break;

if (isPrime) {

System.out.println(v);

void main()

MatrixPrime ob = new MatrixPrime();

ob.input();

ob.check();

} }

Output:

Enter 16 values for the array

9
10

11

12

13

14

15

16

Prime numbers in the matrix:

11

13

Variable List:

Variable list Data Type Description


a int Array Variable
i int Loop variable
j int Loop variable
r int Loop variable
c int Loop variable
v int Stores the prime
numbers

31.Input 9 elements in a 3x3 matrix and check whether the


square matrix is a magic square or not.

[A magic square matrix is one in which the sum of all the


rows, all the columns and both the diagonals are equal].
Algorithm:

Start

Step 1: int i,j;

Step 2: int s1=0,s2=0,s3=0,s4=0;

Step 3: int a[][]=new int[4][4];

Step 4: void display()

Step 5: print ("Enter 16 array elements");

Step 6: for(i=0;i<4;i++) (Rep. line 7to8)

Step 7: for(j=0;j<4;j++) (Rep. line 8)

Step 8: a[i][j]=sc.nextInt();

Step 9: for(i=0;i<4;i++) (Rep. line 10to13)

Step 10: s1=0;

Step 11: for(j=0;j<4;j++) (Rep. line 12)

Step 12: s1=s1+a[i][j];

Step 13: print("Sum of row "+i+" is "+s1);

Step 14: for(i=0;i<4;i++) (Rep. line 15to18)

Step 15: s1=0;

Step 16: for(j=0;j<4;j++) (Rep. line 17)

Step 17: s1=s1+a[i][j];

Step 18: print("Sum of column"+i+" is "+s2);

Step 19: for(i=0;i<4;i++) (Rep. line20)

Step 20: s3=s3+a[i][i];

Step 21: print("Sum of left diagonal is "+s3);

Step 22: for(i=0;i<4;i++) (Rep. line23)

Step 23: s4=s4+a[i][3-i];

Step 24: print ("Sum of right diagonal is "+s4);


Step 25: if(s1==s2 && s1==s3 && s1==s4 && s2==s3 && s2==s4
&& s3==s4)

Step 26: then print ("It is a magic square");

Step 27: else print ("It is not a magic square");

Stop

Code:

import java.util.*;

class magic_square

Scanner sc=new Scanner(System.in);

int i,j;

int s1=0,s2=0,s3=0,s4=0;

int a[][]=new int[4][4];

void display()

System.out.println("Enter 16 array elements");

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

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

a[i][j]=sc.nextInt();

//sum of rows

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

{ s1=0;
for(j=0;j<4;j++)

s1=s1+a[i][j];

System.out.println("Sum of row "+i+" is "+s1);

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

{ s2=0;

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

s2=s2+a[j][i];

System.out.println("Sum of column "+i+" is


"+s2);

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

s3=s3+a[i][i];

System.out.println("Sum of left diagonal is "+s3);

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

s4=s4+a[i][3-i];

System.out.println("Sum of right diagonal is "+s4);


if(s1==s2 && s1==s3 && s1==s4 && s2==s3 && s2==s4 &&
s3==s4)

System.out.println("It is a magic square");

else

System.out.println("It is not a magic square");

} }

Output:

Enter 16 array elements

11

12

13

14
15

16

Sum of row 0 is 10

Sum of row 1 is 26

Sum of row 2 is 33

Sum of row 3 is 58

Sum of column 0 is 28

Sum of column 1 is 23

Sum of column 2 is 36

Sum of column 3 is 40

Sum of left diagonal is 34

Sum of right diagonal is 25

It is not a magic square

Variable List:

Variable Name Data Type Description


s1 int Stores sum of row
s2 int Stores sum of column
s3 int Stores Sum of left diagonal
s4 int Stores Sum of right diagonal
a int Array variable
i int Loop variable
j int Loop variable
32.Enter 9 values each in two 3x3 matrices and calculate their
sum and store it in a third matrix. Display all three
matrices.

Algorithm:

Start

Step 1: int m1[][]=new int[3][3];

Step 2: int m2[][]=new int[3][3];

Step 3: int m3[][]=new int[3][3];

Step 4: int i,j,s=0;

Step 5: public void main() (line 6 to 31)

Step 6: print ("Enter 9 values in the first matrix");

Step 7: for(i=0;i<3;i++) (rep. 7 to 9)

Step 8: for(j=0;j<3;j++)

Step 9: m1[i][j]=sc.nextInt();

Step 10: print ("Enter 9 values in the second matrix");

Step 11: for(i=0;i<3;i++) (rep. 11 to 13)

Step 12: for(j=0;j<3;j++)

Step 13: m2[i][j]=sc.nextInt();

Step 14: for(i=0;i<3;i++) (rep. 14 to 16)

Step 15: for(j=0;j<3;j++)

Step 16: m3[i][j]=m1[i][j]+m2[i][j];

Step 17: print ("first matrix:");

Step 18: for(i=0;i<3;i++)

Step 19: for(j=0;j<3;j++)

Step 20: print(m1[i][j]+" ");

Step 21: System.out.println();

Step 22: print ("second matrix:");


Step 23: for(i=0;i<3;i++)

Step 24: for(j=0;j<3;j++)

Step 25: print(m2[i][j]+" ");

Step 26: System.out.println();

Step 27: print ("final matrix:");

Step 28: for(i=0;i<3;i++)

Step 29: for(j=0;j<3;j++)

Step 30: print(m3[i][j]+" ");

Step 31: System.out.println();

Stop

Code:

import java.util.*;

class matrix_sum

Scanner sc=new Scanner(System.in);

int m1[][]=new int[3][3];

int m2[][]=new int[3][3];

int m3[][]=new int[3][3];

int i,j,s=0;

public void main()

System.out.println("Enter 9 values in the first


matrix");

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

for(j=0;j<3;j++)
{

m1[i][j]=sc.nextInt();

System.out.println("Enter 9 values in the second


matrix");

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

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

m2[i][j]=sc.nextInt();

//Adding both arrays

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

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

m3[i][j]=m1[i][j]+m2[i][j];

System.out.println("first matrix:");

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

for(j=0;j<3;j++)
{

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

System.out.println();

System.out.println("second matrix:");

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

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

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

System.out.println();

System.out.println("final matrix:");

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

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

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

System.out.println();

} } }

Output:

Enter 9 values in the first matrix


1

Enter 9 values in the second matrix

first matrix:

1 2 3

4 5 6

7 8 9

second matrix:

1 2 3

4 5 6

7 8 9
final matrix:

2 4 6

8 10 12

14 16 18

Variable List:

Variable List Data type Description


m1 int Matrix array
m2 int Matrix array
m3 int Matrix array
i int Loop variable
j int Loop variable

33.Enter values in a matrix whose number of rows and columns


is to be input by user and print its Saddle Point.(Saddle
Point of a matrix is the value which is lowest of its row and
the highest of its column).

Algorithm:

Start

Step 1: int i,j,m,n;

Step 2: int row_min,colIndex;

Step 3: void display() (line 3 to 27)

Step 4: print ("Enter the number of rows and columns");

Step 5: m=sc.nextInt();

Step 6: n=sc.nextInt();

Step 7: int a[ ][ ]=new int[m][n];

Step 8: print ("Enter array elements");

Step 9: for(i=0;i<m;i++) (Rep. line 9 to 11)

Step 10: for(j=0;j<n;j++) (Rep. line 11)

Step 11: a[i][j]=sc.nextInt();


Step 12: print ("The array:");

Step 13: for(i=0;i<m;i++) (Rep. line 14 to 16)

Step 14: for(j=0;j<n;j++) (Rep. line 15)

Step 15: print (a[i][j]+" ");

Step 16: print ();

Step 17: for (i=0;i<m;i++)

Step 18: row_min=a[i][0];

Step 19: colIndex = 0;

Step 20: for (j=1;j<n; j++)

Step 21: if(a[i][j] < row_min)

Step 22: row_min = a[i][j];

Step 23: colIndex = j;

Step 24: for (j=0;j<n;j++) (Rep. line 24 to 26)

Step 25: if(a[j][colIndex]>row_min) then print("There is no


saddle point");

Step 26: then break;

Step 27: print("Saddle point: "+row_min);

Stop

Code:

import java.util.*;

class saddle_DDA

Scanner sc=new Scanner(System.in);

int i,j,m,n;

int row_min,colIndex;

void display()

{
System.out.println("Enter the number of rows and
columns");

m=sc.nextInt();

n=sc.nextInt();

int a[ ][ ]=new int[m][n];

System.out.println("Enter array elements");

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

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

a[i][j]=sc.nextInt();

System.out.println("The array:");

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

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

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

System.out.println();

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

row_min=a[i][0];
colIndex = 0;

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

if(a[i][j] < row_min)

row_min = a[i][j];

colIndex = j;

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

if(a[j][colIndex]>row_min)

System.out.println("There is no saddle
point");

break;

System.out.println("Saddle point: "+row_min);

} }

Output:

Enter the number of rows and columns

Enter array elements


1

The array:

1 2 3

4 5 6

7 8 9

Saddle point: 7

Variable List:

Variable Name Data Type Description


a int Array variable
i int Loop variable
j int Loop variable
m int Stores number of rows
n int Stores number of columns
row_min int Stores minimum element of row
colIndex int Stores the index number of
column

34.Design a class eqMat to check if two matrices are equal or


not.Assume that the two matrices have the same dimensions.

Algorithm:
Start

Step 1: i=0,j=0,flag=0

Step 2: input r

Step 3: input c

Step 4: int A[][]=new int[r][c];

Step 5: int B[][]=new int[r][c];

Step 6: p=r*c

Step 7: print("Enter "+p+" values for array A row-wise");

Step 8: for(i=0;i<r;i++) (Rep.8 to 10)

Step 9: for(j=0;j<c;j++) (Rep.10)

Step 10: A[i][j]=sc.nextInt();

Step 11: print("Enter "+p+" values for array B row-wise");

Step 12: for(i=0;i<r;i++) (Rep.12 to 14)

Step 13: for(j=0;j<c;j++) (Rep.14)

Step 14: B[i][j]=sc.nextInt();

Step 15: for (i=0;i<r;i++) (Rep.15 to 17)

Step 16: for (j=0;j<c;j++)(Rep.17)

Step 17: if (A[i][j] == B[i][j]) then flag++;

Step 18: if(flag>0) then print("The two matrixes are


equal");

Step 19: else print("The two matrixes are not equal");

Stop

Code:

import java.util.Scanner;

class eqMat

public static void main()


{

int i=0,j=0,flag=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter no of rows");

int r=sc.nextInt();

System.out.println("Enter no of columns");

int c=sc.nextInt();

int A[][]=new int[r][c];

int B[][]=new int[r][c];

int p=r*c;

System.out.println("Enter "+p+" values for array A


row-wise");

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

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

A[i][j]=sc.nextInt();

System.out.println("Enter " + p + " values for array


B row-wise");

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

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

B[i][j]=sc.nextInt();

}
}

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

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

if (A[i][j] == B[i][j])

flag++;

if(flag>0)

System.out.println("The two matrixes are equal");

else

System.out.println("The two matrixes are not


equal");

} } }

Output:

Enter no of rows

Enter no of columns

Enter 9 values for array A row-wise

1
2

Enter 9 values for array B row-wise

The two matrices are equal

Variable List:

Variable list Data type Description


i int Loop variable
j int Loop variable
flag int Flag variable
r int Stores number of
rows
c int Stores number of
columns
int Matrix
a variable
b int Matrix variable

35.Write a program to declare a square matrix M[][] of order


‘N’ where ‘N’ must be greater than 3 and less than 10.Allow
the user to accept three different characters from the
keyboard and fill the array accordingly:

(i) Fill four corners of the square matrix by 1 character.

(ii) Fill the boundary elements of the matrix(Except Corners)


with character 2.

(iii) Fill the non-boundary elements of the matrix by


character 3.

Algorithm:

Start

Step 1: input N

Step 2: if(N>3&&N<10)

Step 3: input ch1,ch2,ch3

Step 4: char ch[][]= new char[N][N]

Step 5: ch[0][0] = ch1;

Step 6: ch[0][N-1] = ch1;

Step 7: ch[N-1][0] = ch1;

Step 8: ch[N-1][N-1] = ch1;

Step 9: for(i=1;i<N-1;i++) (Rep.8 to 10)

Step 10: for(j=1;j<N-1;j++) (Rep.10)

Step 11: ch[i][j] = ch3;

Step 12: for(i=0;i<=N-1;i+=N-1)(Rep.8 to 10)

Step 13: for(j=1;j<N-1;j++)(Rep.10)

Step 14: ch[i][j] = ch2;


Step 15: for(i=0;i<=N-1;i+=N-1)(Rep.8 to 10)

Step 16: for(j=1;j<N-1;j++)(Rep.10)

Step 17: ch[j][i] = ch2;

Step 18: for(i=0;i<=N-1;i++)(Rep.8 to 10)

Step 19: for(j=0;j<=N-1;j++)(Rep.10)

Step 20: System.out.print(ch[i][j]);

Step 21: System.out.println();

Step 22: else print("The dimensions are inappropiate");

Stop

Code:

import java.util.Scanner;

public class SquareMatrix

public static void main()

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the order 'N' of the square


matrix (greater than 3 and less than 10): ");

int N=scanner.nextInt();

if (N > 3 && N < 10)

{ System.out.println("Invalid input for N. It must be


greater than 3 and less than 10.");

System.out.print("Enter the first character: ");

char char1 = scanner.next().charAt(0);

System.out.println();

System.out.print("Enter the second character: ");

char char2 = scanner.next().charAt(0);


System.out.println();

System.out.print("Enter the third character: ");

char char3 = scanner.next().charAt(0);

System.out.println();

char matrix[ ][ ] = new char[N][N];

for (int i = 0; i < N; i++)

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

if ((i == 0 && j == 0) || (i == 0 && j == N - 1)


||(i == N - 1 && j == 0) || (i == N - 1 && j == N - 1))

matrix[i][j] = char1;

else if (i == 0 || i == N - 1 || j == 0 || j ==
N - 1)

matrix[i][j] = char2;

else

matrix[i][j] = char3;

System.out.println("Resultant Matrix:");

for (int i = 0; i < N; i++)


{

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

System.out.print(matrix[i][j]);

System.out.println();

else

System.out.println("Invalid input");

} } }

Output:

Enter the order 'N' of the square matrix (greater than 3 and
less than 10): 4

Invalid input for N. It must be greater than 3 and less than


10.

Enter the first character: @

Enter the second character: ?

Enter the third character: #

Resultant Matrix:

@??@

?##?

?##?

@??@

Variable List:

Variable Name Data Type Description


matrix int 2-D Array
j int Loop variable
i int Loop variable
sum int Used for summation
N int Accepts

36.Input 16 values in a 4x4 matrix.Display only the boundary


elements.

Calculate their sum.

Algorithm:

Start

Step 1: int A[][]= new int[4][4];

Step 2: input A

Step 3: int A[][]= new int[4][4];

Step 4: sum=0,N=A.length;

Step 5: sum+=A[0][0]+A[0][N-1]+A[N-1][0]+A[N-1][N-1];

Step 6: for(i=0;i<=N-1;i+=N-1)(Rep.13 to 14)

Step 7: for(j=1;j<N-1;j++)(Rep.14)

Step 8: sum+=A[i][j];

Step 9: for(i=0;i<=N-1;i+=N-1)(Rep.16 to 17)

Step 10: for(j=1;j<N-1;j++)(Rep.17)

Step 11: sum+=A[j][i];

Step 12: print(sum);

Step 13: System.out.println();

Stop

Code:

import java.util.*;

class Boundary
{

void display()

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

System.out.println("Enter values row-wise");

int A[][]= new int[4][4];

int i,j,sum=0,N=A.length;

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

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

A[i][j]=sc.nextInt();

sum+=A[0][0]+A[0][N-1]+A[N-1][0]+A[N-1][N-1];

for(i=0;i<=N-1;i+=N-1)

for(j=1;j<N-1;j++)

sum+=A[i][j];

for(i=0;i<=N-1;i+=N-1)

for(j=1;j<N-1;j++)

sum+=A[j][i];

}
}

System.out.println(sum);

} }

Output:

Enter values row-wise

Enter the 3 characters serially

24

Variable List:

Variable Name Data Type Description


A int 2-D Array
i int Loop variable
j int Loop variable
sum int Used for summation
N int Stores length of the array
A
1. Write a program to display the factors of a number:
Algorithm:
Start
Step 1: input n
Step 2: i=1
Step 3: while i<n [Repeat 4 &5]
Step 4: if n % i==0 then print i
Step 5: i= i+1
Stop
Code:
import java.util.*;
class factors
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
int i=1;
System.out.println(“Factors are:”);
while(i<=n)
{
if(n%i==0)
{
System.out.println(i);
}
i=i+1;
}
} }
Output:
Enter the number
15
Factors are:
1
3
5
15

Variable List:

Variable Data Description


Name Type

n int Stores an integer number

i int Loop variable

2. Write a program to check whether a number is prime or not


Algorithm:
Start
Step 1:input n
Step 2:i=1,counter=0
Step 3:while i<n [repeat 4&5]
Step 4:if n%i==0 then counter++
Step 5:i=i+1
Step 6:print counter
Stop
Code:
import java.util.*;
class prime
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
int i=1,c=0;
while(i<=n)
{
if(n%i==0)
{
c++;
}
i=i+1;
}
if(c==2)
{
System.out.println("It is a prime number");
}
else
{
System.out.println("Not a prime number");
}
}
}
Output
Enter the number
7
It is a prime number
Variable list

Name Type Description

n int Number to be checked by the user

I int Iteration number for loop

c int Counter
3. Write a program to check whether a number is perfect or not

Algorithm:
Start
Step 1:input n
Step 2:i=1,s=0
Step 3:while i<n [repeat 4&5]
Step 4:if n%i==0 then s=s+i
Step 5:i=i+1
Step 6:if s==n then print “perfect number”
Step 7:else print “not a perfect number”
Stop
Code:
import java.util.Scanner;
class perfect
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
int i=1,s=0;
while(i<n)
{
if(n%i==0)
{
s=s+i;
}
i=i+1;
}
if(s==n)
{
System.out.println("Perfect number");
}
else
{
System.out.println("Not a perfect number");
}
}
}
Output
Enter the number
6
Perfect number
Variable list

Name Type Description

n int accepts the number to be checked from user

i int iteration number for loop

c int stores the sum

4. Write a program to check whether a number is twin-prime or not:


Algorithm:
Start
Step 1:input n and m
Step 2:i=1,c=0
Step 3:while i<=n [repeat 4&5]
Step 4:if(n%i==0) then c++
Step 5:i=i+1
Step 6:j=1,d=0
Step 7:while j<=m [repeat 8 &9]
Step 8:if(m%j==0) then d++
Step 9:j=j+1
Step 10:if(c==2 && d==2) then if(m-n==2||n-m==2),then print “twin-prime”
Step 11: else print “not twin-prime”
Stop
Code:
import java.util.Scanner;
class twin_prime
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers");
int n=sc.nextInt();
int m=sc.nextInt();
int i=1,c=0;
while(i<=n)
{
if(n%i==0)
{
c++;
}
i=i+1;
}
int j=1,d=0;
while(j<=m)
{
if(m%j==0)
{
d++;
}
j=j+1;
}
if(c==2 && d==2)
{
if(m-n==2||n-m==2)
{
System.out.println("It's a twin-prime number");
}
else
{
System.out.println("It's not a twin-prime number");
}
}
}
}
Output:
Enter two numbers
11
13
It's a twin-prime number
Variable list

Name Type Description

n int Number to inputted to checked

m int Number to inputted to checked

i int Iteration for loop


st
c int 1 counter
nd
d int 2 counter

5. Write a program to check whether a number is Kaprekar or not:


Algorithm:
Start
Step 1:input n
Step 2:t=n
Step 3:c=0
Step 4:while n>0 [repeat 5 &6]
Step 5:n=n/10
Step 6:c=c+1
Step 7:v=t*t
Step 8:d1=v%10^c
Step 9:d2=v/10^c
Step 10:if (t=d1+d2) then print “Kaprekar”
Step 11: else print “It's not a Kaprekar number"
Code:
import java.util.Scanner;
class Kaprekar
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int t,c=0;
t=n;
while(n>0)
{
n=n/10;
c=c+1;
}
int v=t*t;
int d1=v%(int)Math.pow(10,c);
int d2=v/(int)Math.pow(10,c);
if(t==d1+d2)
{
System.out.println("It's a Kaprekar number");
}
else
{
System.out.println("It's not a Kaprekar number");
}
}
}
Output:
Enter a number
45
It's a Kaprekar number
Variable list:

Name Type Description

n int stores the number entered by the user

t int stores the value of n

c int counter

v int stores sqaure of the number entered by user

d1 int stores value of 1st calculation

d2 int stores value of 2nd calculation

6. Write a program to check whether a number is automorphic or not:

Algorithm:
Step 1:input n
Step 2:t=n
Step 3:c=0
Step 4:while n>0 [repeat 5 & 6]
Step 5:n=n/10
Step 6:c=c+1
Step 7:v=t*t
Step 8:if(t==v%10^c) then print “Automorphic” otherwise not
Code:
import java.util.Scanner;
class automorphic
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to be checked");
int n=sc.nextInt();
int t=n;
int c=0;
while(n>0)
{
n=n/10;
c=c+1;
}
int v=t*t;
if(t==v%(int)Math.pow(10,c))
{
System.out.println("Automorphic number");
}
else
{
System.out.println("Not an Automorphic number");
}
} }
Output:
Enter the number to be checked
25
Automorphic number
Variable list:

Name Type Description


n int stores the number to be checked

T int stores temporary value for n

c int Counter

v int stores the square of t

7. Write a program to check whether a number is Disarium or not:


Algorithm:
Step 1:input n
Step 2:t=n
Step 3:c=0
Step 4:while t>0 [repeat 5 & 6]
Step 5:t=t/10
Step 6:c=c+1
Step 7:t=n
Step 8:s=0
Step 9:while t>0 [repeat 10 to 13]
Step 10:d=t%10
Step 11:s=s+d^ c
Step 12:t=t/10
Step 13:c=c-1
Step 14:if s==n print ”Disarium number”
Step 15:else print “Not a disarium number”
Code:
import java.util.Scanner;
class disarium
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value to be checked");
int n=sc.nextInt();
int t=n;
int c=0;
while(t>0)
{
t=t/10;
c=c+1;
}
t=n;
int s=0;
while(t>0)
{
int d=t%10;
s=s+(int)Math.pow(d,c);
t=t/10;
c=c-1;
}
if(s==n)
{
System.out.println("it is a Disarium number");
}
else
{
System.out.println("it is not a Disarium number");
}
}
}
Output:
Enter the value to be checked
135
it is a Disarium number
Variable list:
Name Type Description

n int stores the number to be checked

t int stores temporary value for n

c int counter

s int stores and calculates the sum

8. Write a program to check whether a number is a Fascinating


number or not:
Algorithm:
Start
Step 1: input n
Step 2: n2=n* 2
Step 3: n3=n*3
Step 4: string s=n1+””n2+n3
Step 5: boolean f=true
Step 6: for (char a='1';a<='9';a++) [repeat 6 to 15]
Step 7:c=0
Step 9: for(int i=0;i<s.length();i++)[repeat 9 to 12]
Step 10: char ch=s.charAt(i);
Step 11:if(ch==a)
Step 12:c++
Step 13:if(c>1 || c==0)
Step 14:f=false
Step 15:break
Step 16: if(f) then print “Fascinating number” else not
Stop

Code:
import java.util.Scanner;
class Fascinating
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to be checked");
int n=sc.nextInt();
int n2=n*2;
int n3=n*3;
String s=n+""+n2+n3;
boolean f=true;
for(char a='1';a<='9';a++)
{
int c=0;
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch==a)
c++;
}
if(c>1 || c==0)
{
f=false;
break;
}
}
if(f)
System.out.println("it is a Fascinating number");
else
System.out.println("it is not a Fascinating number");
} }
Output:
Enter the number to be checked
192
it is a Fascinating number.
Variable list:

Name Type Description

n int stores the original number from the user

n2 int original number’s value multiplied by 2

n3 int original number’s value multiplied by 3

s String concatenates values of n,n2,n3

f boolean flag

a char loop initiator

i int positon of letter(index)

c int count

9. Write a program to check whether a number is a Smith number or


not:
Algorithm:
Step 1: define a function boolean isSmithnumber(n)
Step 2: int digitSum = 0
Step 3: temp=n
Step 4: while(temp>0) [repeat 5 to 6]
Step 5: digitSum=digitSum+temp % 10;
Step 6: temp=temp/10;
Step 7: primefsum=0
Step 8: f=2
Step 9: while (n > 1)
Step 10: if n % f == 0 then primefSum += getDigitSum(f);
Step 10: else f++
Step 11: return (digitSum == primefSum)
Step 12: define a function getdigitsum(n)
Step 13: sum=0
Step 14: while(n>0) [repeat 15 & 16]
Step 15: sum=sum+n%10;
Step 16: n=n/10;
Step 17: return sum
Step 18: input num
Step 19: if (isSmithnumber(num)) then print it is a smith number
Step 20: else print it is not a smith number

Code:
import java.util.*;
class Smith
{
boolean isSmithnumber(int n)
{
int digitSum = 0;
int temp = n;
while (temp > 0)
{
digitSum=digitSum+temp % 10;
temp=temp/10;
}
int primefSum = 0;
int f = 2;
while (n > 1)
{
if (n % f == 0)
{
primefSum += getDigitSum(f);
n=n/f;
}
else
{
f++;
}
}
return (digitSum == primefSum);
}
int getDigitSum(int n)
{
int sum = 0;
while (n > 0)
{
sum=sum+n%10;
n=n/10;
}
return sum;
}
void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (isSmithnumber(num))
{
System.out.println(num + " is a Smith number.");
}
else
{
System.out.println(num + " is not a Smith number.");
}
}
}
Output:
Enter a number: 22
22 is a Smith number.
Variable List:

Variable Name Data Type Description

n int number to be used for checking

Digitsum int sum of the digits of the number

Temp int stores temporary value for n

Primefsum int stores sum of the prime factors of the


number

f int Flag variable

Sum int stores sum of

Num int number to be checked by the user

isSmithnumber boolean function to check smith number

getDigitSum Int stores sum of the digits of the number

10.Write a program to check if it is a triangular number or not


Algorithm:
Start
Step 1:void display() (rep.
Step 2: input n
Step 3:int i,j,s
Step 4:for (i = 1; i <= n; i++)
Step 5:s=0
Step 6:for (j = 1; j <= i; j++)
Step 7:s = s + j
Step 8:if(s<=n) print(s)
Stop
Code:
import java.util.Scanner;
class Triangular
{
void display()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit");
int n = sc.nextInt();
int i, j, s;
for (i = 1; i <= n; i++)
{
s = 0;
for (j = 1; j <= i; j++)
{
s = s + j;
}
if (s <= n)
{
System.out.println("Triangular numbers upto "+n+" is"+s);
}
}
}
}

Output:
Enter the limit:15
Triangular Numbers upto 15
1
3
6
10
15
Variable list:

Variable Name Data Type Description

n int Number to be checked

i int Loop variable

j int Loop variable

s int Stores sum.

11.Check whether a number is a Dudeney number or not:


Algorithm:
Step 1: input n
Step 2: t=n,s=0
Step 3: while(t>0)
Step 4: d=t%10
Step 5: s+=d
Step 6: t=t/10
Step 7: if s==Math.cbrt(n), then print Dudeney number

Code:
import java.util.Scanner;
class Dudeney
{
void display()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int t=n,s=0;
while(t>0)
{
int d=t%10;
s=s+d;
t=t/10;
}
if(s==(Math.cbrt(n)))
{
System.out.println("It is a Dudeney number");
}
else
{
System.out.println("It is a Dudeney number");
}
} }
Output:
Enter a number
512
It is a Dudeney number
Variable List:

Variable Name Data Type Description

n int Accepts number from user

t int Stores a copy of n

d int Digit separator

s int Stores the calculation for the number

12. Write a program to print the factorials of a number.


Algorithm:
Start
Step 1: int n,f
Step 2: num() [line 3]
Step 3: n=0,f=1
Step 4: void input() [line 5]
Step 5: input n
Step 6: void fact() [line 7-10]
Step 7: i=1,f=1
Step 8: while(i<=1) [line 9-10]
Step 9: f=f*i
Step 10:i++
Step 11:void display() [line 12-13]
Step 12:fact()
Step 13:print("Factorial is:"+f)
Stop
Code:
import java.util.Scanner;
class nfactor
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
int i=1;
while(i<n)
{
if(n%i==0)
{
System.out.println(i);
}
i=i+1;
}
}
}
Output:
Enter the number:8
Factors of the number:1,2,4

Variable List:

Variable Name Data Type Description

n int Accepts an integer

f int Stores the factorial

i int Loop variable

13.Write a program to print the sum of factors of a number.


Algorithm:
Start
Step 1: num1() [line 2-3]
Step 2: s=0,n=0
Step 3: void input() [line 4]
Step 4: input n
Step 5: void sumfact() [line 6-8]
Step 61: for(i=1;i<=n;i++) [Rep: 7-8]
Step 7: if(n%i==0) [rep 8]
Step 8: s=s+i
Step 9: void display() [line 10-11]
Step 10:sumfact()
Step 11:print("Sum of factors is:"+s)
Stop
Code:
import java.util.Scanner;
class num1
{
int n,s;
void num()
{
s=0;
n=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void sumfact()
{
int i;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
s=s+i;
}}
}
void display()
{ s=0;
sumfact();
System.out.println("The sum of the factors of "+n+" is "+s);
} }
Output:
The sum of the factors of 5 is 6
Variable list:

Variable Name Data Type Description


n int Stores number to be checked

s int Stores sum of factors

i int Loop variable

14.Write a program to print the reverse of a number.


Algorithm:
Start
Step1:num2() line-2
Step2:n=1,r=1
Step3:void input() line4
Step4:input n
Step5:void rev() line 6-9
Step6:while(n!=0) rep line 7-9
Step7:d=n%10
Step8:r=r*10+d
Step9:n=n/10
Step10:void display() line11-12
Step11:print("Reversed number"+r)
Stop
Code:
import java.util.Scanner;
class num2
{
int n,r;
num2()
{
n=1;
r=1;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void rev()
{
while(n!= 0)
{
int d = n % 10;
r = r * 10 + d;
n=n/10;
}
}
void display()
{
rev();
System.out.println(" Reversed number:"+r);
} }
Output:
Enter a number:
451
Reversed number:154
Variable List:

Variable Name Data Type Description

n int Stores number

r int Stores the last digits


15.Write a program to accept a number and print the number of factors.
Algorithm:
Start
Step 1: num3()
Step 2: c=0;
Step 3: n=0;
Step 4: i=0;
Step 5: void input()
Step 6: input n
Step 7: void count()
Step 8: c=0;
Step 9: i=1;
Step 10: while(i<=n) (Rep.11to12)
Step 11: if(n%i==0) then c++;
Step 12: i++;
Step 13: void display()
Step 14: count();
Step 15: print("The no. of factors is:"+c);
Stop
Code:
import java.util.Scanner;
class num3
{
int n,c,i;
void num()
{
c=0;
n=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void count()
{
c=0;
i=1;
while(i<=n)
{
if(n%i==0)
{
c++;
}
i++;
}
}
void display()
{
c=0;
count();
System.out.println("The no. of factors is:"+c);
} }
Output:
Enter the number:
5
The no. of factors is:2

Variable List:

Variable Name Data Type Description


n int Stores number to be checked

c int counter

i int Loop variable

16: Write a program to accept a number and check whether it is a


sumproduct number or not.
Algorithm:
Start
Step 1:int n
Step 2:sumproduct() (rep. 2 to 3)
Step 3:n=0
Step 4:void readnum() (rep. 4 to 5)
Step 5:input n
Step 6:int sum(int v) (rep. 6 to 12)
Step 7:d=0,s=0
Step 8:while(v>0) (rep. 9 to 11)
Step 9:d=v%10
Step 10:s=s+d
Step 11:v=v/10
Step 12:return s
Step 13:int product(int v) (rep. 13 to 19)
Step 14:d=0,p=1
Step 15:while(v>0) (rep. 15 to 18)
Step 16:d=v%10
Step 17:p=p*d
Step 18:v=v/10
Step 19:return p
Step 20:void check()
Step 21:int a=sum(n)
Step 22:int b=product(n)
Step 23:if(a*b==n) then print ("It is a sumproduct number")
Step 20:else print ("It is not a sumproduct number")
Stop
Code:
import java.util.Scanner;
class sumproduct
{
int n;
sumproduct()
{
n=0;
}
void readnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
int sum(int v)
{
int d=0,s=0;
while(v>0)
{
d=v%10;
s=s+d;
v=v/10;
}
return s;
}
int product (int v)
{
int d=0,p=1;
while(v>0)
{
d=v%10;
p=p*d;
v=v/10;
}
return p;
}
void check()
{
int a=sum(n);
int b=product(n);
if(a*b==n)
{
System.out.println("It is a sumproduct number");
}
else
{
System.out.println("It is not a sumproduct number");
}
}
}
Output:
Enter a number
144
It is a sumproduct number
Variable List:

Variable name Data Type Description

n int Stores input from user

v int Formal parameter


d int Stores remainder

s int Stores sum of remainder

v int Stores quotient of the number

p int Stores product of the remainder

a int Stores the sum

b int Stores the product

17.Write a program to enter a number and check if it is an emirp


number or not.
Algorithm:
Start
Step 1:int n,rev,f
Step 2:void input() (rep. 2 to 3)
Step 3:input n
Step 4:void isPrime() (rep. 4 to 7)
Step 5:int i
Step 6:for(i=2;i<=n/2;i++) (rep. 6 to 7)
Step 7:if(n%i==0) then f=1 and return
Step 8:void isEmirp() (rep. 8 to 20)
Step 9:int t=n,rem
Step 10:rev=0
Step 11:while(n!=0) (rep. 11 to 14)
Step 12:rem=n%10
Step 13:rev=rev*10+rem
Step 14:n=n/10
Step 15:Emirp obj = new Emirp()
Step 16:obj.n = rev
Step 17: obj.isPrime()
Step 18:if (f == 0 && obj.f == 0) then print("Both the original number and
the reversed number are prime");
Step 19:else if(f == 0 && obj.f != 0) then print("The original number is prime
but the reversed number is not prime")
Step 20:else if(f != 0 && obj.f == 0) then print ("The original number is not
prime but the reversed number is prime");
Step 21:else print("Both the original number and the reversed number are
not prime");
Stop
Code:
import java.util.Scanner;
class Emirp
{
int n,rev,f;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc. nextInt();
}
void isPrime()
{
int i;
for (i=2;i<=n/2;i++)
{
if(n%i==0)
{
f=1;
return;
}
}
}
void isEmirp()
{
int t = n,rem;
rev = 0;
while (n != 0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
Emirp obj = new Emirp();
obj.n = rev;
obj.isPrime();
if (f == 0 && obj.f == 0)
{
System.out.println("Both the original number and the reversed
number are prime.It is an EMIRP number");
}
else if (f == 0 && obj.f != 0)
{
System.out.println("The original number is prime but the reversed
number is not prime");
}
else if (f != 0 && obj.f == 0)
{
System.out.println("The original number is not prime but the
reversed number is prime");
}
else
{
System.out.println("Both the original number and the reversed
number are not prime");
}
}
}
Output:
1.) Enter the number
35
The original number is not prime but the reversed number is prime.
2.) Enter the number
11
Both the original number and the reversed number are prime.It is an
EMIRP number.
Variable List:

Variable List Data Type Description

n int Stores the number from the user

rev int Stores reverse of the number

f int Flag variable

i int Loop variable

18.Write a program to input a number and print it along with its octal
equivalent.
Algorithm:
Start
Step 1: int n,oct
Step 2: DeciOct() (rep. 2 to 3)
Step 3: oct=0,n=0;
Step 4: void getnum() (rep. 4 to 6)
Step 5: print"Enter a number"
Step 6: input n
Step 7: void deci_oct() (rep. 7 to 15)
Step 8: t=n,s=0;
Step 9: while(t>0) (rep. 9 to 12)
Step 10: d=t%8;
Step 11: s=s*10+d;
Step 12: t=t/8;
Step 13: while(s>0) (rep. 13 to 15)
Step 14: oct=oct*10+(s%10);
Step 15: s=s/10;
Step 16: void show() (rep. 16 to 20)
Step 17: oct=0;
Step 18:deci_oct();
Step 19: print ("Decimal no. : "+n);
Step 20: print ("Octal equivalent no. : "+oct);
Stop

Code:
import java.util.Scanner;
class DeciOct
{
int n,oct;
DeciOct()
{
n=0;
oct=0;
}
void getnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc. nextInt();
}
void deci_oct()
{
int t=n,s=0;
while(t>0)
{
int d=t%8;
s=s*10+d;
t=t/8;
}
while(s>0)
{
oct=oct*10+(s%10);
s=s/10;
}
}
void show()
{ oct=0;
deci_oct();
System.out.println("Decimal no. : "+n);
System.out.println("Octal equivalent no. : "+oct);
} }
Output:
Enter a number
55
Decimal no. : 55
Octal equivalent no. : 67

Variable List:

Variable list Data type Description

n int Stores number from the user

oct int Stores octal number

t int Temporary variable storing quotient

s int Stores sum of remainder

d int Stores the remainder


19.Write a program to print the sum of the given series.
x^2/1! + x^4/3! + x^6/5! +........x^n/(n-1)!
Algorithm:
Start
Step 1: int x,n;
Step 2: double sum;
Step 3: SeriesSum(int xx,int nn) (rep. 3 to 5)
Step 4: x=xx;
Step 5: n=nn;
Step 6: double findfact(int m) (rep. 6 to 10)
Step 7: i, f=1;
Step 8: for(i=1;i<=m;i++)
Step 9: f=f*i;
Step 10: return f;
Step 11: double findpower(int x,int y) (rep. 11 to 13)
Step 12: p=(int)(Math.pow(x,y));
Step 13: return p;
Step 14: void calculate() (rep. 14 to 18)
Step 15: for(int i=2;i<=n;i++)
Step 16: a= findpower(x,i);
Step 17: b= findfact(i-1);
Step 18: sum=a/b;
Step 19: void display() (rep. 19 to 21)
Step 20: calculate();
Step 21: print sum;
Stop
Code:
import java . util .*;
class SeriesSum1 {
int x; // integer number
int n; // number of terms
double sum; // sum of the series
public SeriesSum1(int xx, int nn) {
x = xx;
n = nn;
sum = 0.0; // Initialize sum to 0
}

// Calculate factorial of a number


double findFact(int m) {
if (m == 0) {
return 1.0;
} else {
return m * findFact(m - 1);
}
}

// Calculate power of x raised to the power y


double findPower(int x, int y) {
return Math.pow(x, y);
}

// Calculate the sum of the series


void calculate() {
for (int i = 1; i <= n; i++) {
double term = findPower(x, i) / findFact(i - 1);
sum += term;
}
}

// Display the sum of the series


void display() {
System.out.println("Sum of the series: " + sum);
}
static void main(String[] args) {
int x = 2; // Set your desired values
int n = 5;

SeriesSum series = new SeriesSum(x, n);


series.calculate();
series.display();
}
}

Output:
Sum of the series: 1.2404667449597834E14
Variable List:

Variable Name Data Type Description

x int Formal parameter

n int Formal parameter

sum double Stores sum

m int Formal parameter

i int Loop variable

f int factorial

p int Stores the value of the power

a int Stores the values raised to their


respective powers

b int Stores factorial

20.Write a program to accept a number and check if it is a Spy Number


or not.
Algorithm:
Start
Step 1:int sum=0,pro=1,l
Step 2:void main() (rep. 2 to 10)
Step 3:input n
Step 4:while(n<0) (rep. 4 to 8)
Step 5:l=n%10
Step 6:sum=sum+l
Step 7:pro=pro*l
Step 8:n=n/10
Step 9:if(sum==pro) then print ("its a spy number")
Step 10:else ("its not a spy number")
Stop
Code:
import java . util . *;
class spynum
{
int sum=0,pro=1, l;
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("pls enter number");
int n= sc.nextInt();
while(n>0)
{
l = n%10;
sum =sum+l;
pro=pro*l;
n = n/10;
}
if( sum == pro )
{
System.out.println("its a spy number");
}
else
{
System.out.println("its not a spy number");
} } }
Output:
Pls enter number
1124
It’s a spy number

Variable List:

Variable Name Data Type Description

sum int Stores sum of remainder

pro int Stores product of remainder

l int Stores remainder

n int Stores the number from the user

21.Write a program to accept a number and check whether it is an


Armstrong number or not.
Algorithm:
Start
Step 1:public static void main() (rep. 1 to 8)
Step 2:input longnum
Step 3:long b=num, len=Long.toString(num).length(),sum=0
Step 4:while(b!=0) (rep. 4 to 6)
Step 5:sum=sum+ (long)Math.pow(b % 10, len)
Step 6: b =b/ 10;
Step 7:if(sum == num) then print (num+ “ is Armstrong”)
Step 8:else print (num + " is not Armstrong")
Stop
Code:
import java.util.*;
class Armstrong
{ int n, s;
Armstrong ()
{n=0;
s=0;
}
void input ()
{ Scanner sc = new Scanner (System.in);
System.out.println("Enter a no.: ");
n = sc. nextInt();
}
void sumcal()
{ int sn=n;
while (sn>0)
{ int r=sn%10;
s = s + (int)Math.pow(r,3);
sn = sn / 10;
}
}
void check()
{ if (s == n)
System.out.print ("No. is Amstrong");
else
System.out.println("No. ain't Armstrong");
}
void main()
{ Armstrong as=new Armstrong();
as.input();
as.sumcal();
as.check();
}
}
Output:
Enter a no:
153
No. is Amstrong

Variable List:

Variable List Data Type Description

n int Stores the number from the user

s int Stores sum of the cubes of the digits

sn int copy variable(stores value of n))

r int Stores the remainder

22.Write a program to accept a number and check whether it is an evil


number or not.
Algorithm:
Start
Step 1:public static void main() (rep.1 to
Step 2:input n
Step 3:if(n<0) then print("Invalid Input") return;
Step 4:int count=0
Step 5:int p=0
Step 6:int binNum=0
Step 7:while(n>0) (rep. 7 to 12)
Step 8:int d=n%2
Step 9:if(d==1) then count++
Step 10:binNum+=(int)(d * Math.pow(10, p))
Step 11:p++
Step 12:n /= 2
Step 13:print("Binary Equivalent: " + binNum)
Step 14:print("No. of 1's: " + count)
Step 15:if (count % 2 == 0) then print("Output: Evil Number")
Step 16:else print("Output: Not an Evil Number")
Stop
Code:
import java.util.Scanner;
class EvilNumber
{
void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n =sc.nextInt();
if (n < 0)
{
System.out.println("Invalid Input");
return;
}
int count = 0;
int p = 0;
int binNum = 0;
while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}
System.out.println("Binary Equivalent: " + binNum);
System.out.println("No. of 1's: " + count);
if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
} }
Output:
Enter a positive number: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number
Variable List:

Variable list Data type Description

n int Stores number from user

count int Stores the count

p int Stores power

binNum int Stores the binary number


d int Stores the remainder

23.Write a program to accept a sentence and display each word in the


sentence along with the number of vowels
in that word.
Algorithm:
Start
Step 1:String Line
Step 2:Sentence() (rep. 2 to 3)
Step 3:input line
Step 4:void count() (rep. 4 to 19)
Step 5:int startIndex=0
Step 6:int lastInd=0
Step 7:while(lastInd<line.length()) (rep. 7 to 19)
Step 8:if (line.charAt(lastInd)==' '||lastInd==line.length()-1)
Step 9:if (lastInd== line.length()-1)
Step 10:lastInd++;
Step 11:String word = line.substring(startIndex,lastInd);
Step 12:int vowelCount = 0;
Step 13:for (char ch : word.toLowerCase().toCharArray())
Step 14:if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
Step 15:vowelCount++
Step 16:System.out.println("No of vowels in " + word + "-" + vowelCount)
Step 18:startIndex=lastInd+1
Step 19:lastInd++
Step 20: void main() (rep. 21 to 23)
Step 21:Sentence obj = new Sentence()
Step 22:obj.input()
Step 23:obj.count()
Stop
Code:
import java.util.Scanner;
class Sentence
{
String line;
Sentence()
{
line="";
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
line = sc.nextLine();
}
void count()
{
int startIndex=0;
int lastInd=0;
while (lastInd<line.length())
{
if (line.charAt(lastInd)==' '||lastInd==line.length()-1)
{
if (lastInd== line.length()-1)
{
lastInd++;
}
String word = line.substring(startIndex,lastInd);
int vowelCount = 0;
for (char ch : word.toLowerCase().toCharArray())
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowelCount++;
}
}
System.out.println("No of vowels in " + word + "-" + vowelCount);
startIndex=lastInd+1;
}
lastInd++;
}
}
public static void main()
{
Sentence obj = new Sentence();
obj.input();
obj.count();
} }
Output:
Enter a sentence:
Kolkata is my city
No of vowels in Kolkata-3
No of vowels in is-1
No of vowels in my-0
No of vowels in city-1
Variable List:

Variable Name Data Type Description

line String Stores a string

lastInd int Stores last index position of character

startIndex int Stores first index position of character

vowelCount int Counts number of vowels


24.Write a program to input a full name and then print the initials
along with the surname.
Algorithm:
Start
Step 1: input str
Step 2: str= str.trim();
Step 3: str = " "+str;
Step 4: c1=0,c2=0;
Step 5: for(i=0;i<str.length();i++) (Rep.6to7)
Step 6: if(Character.isWhitespace(str.charAt(i)))
Step 7: if(i!=str.lastIndexOf(' ')) then print(str.charAt(i+1)+".");
Step 8: print(" "+str.substring(str.lastIndexOf(' ')+1));
Stop
Code:
import java.util.*;
class Initials
{
void display()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a name");
String str = sc.nextLine();
str= str.trim();
str = " "+str;
int i,j;
for(i=0;i<str.length();i++)
{
if(Character.isWhitespace(str.charAt(i)))
{
if(i!=str.lastIndexOf(' '))
System.out.print(str.charAt(i+1)+".");
}
}
System.out.print(" "+str.substring(str.lastIndexOf(' ')+1));
} }
Output:
Enter a name
Mohandas Karamchand Gandhi
M.K. Gandhi
Variable List:

Variable Name Data Type Description

str int Stores a string

i int Loop variable

j int Loop variable

25.Write a program to input two numbers and then merge the two
numbers.
Algorithm:
Start
Step 1: long n1,n2,mergNum;
Step 2: merger() (line 3to5)
Step 3: n1=0;
Step 4: n2=0;
Step 5: mergNum=0;
Step 6: void readNum() (line 7to9)
Step 7: print ("Enter two numbers");
Step 8: n1=sc.nextLong();
Step 9: n2=sc.nextLong();
Step 10: joinNum() (line 11to16)
Step 11: num1="";
Step 12: num2="";
Step 13: num1=Long.toString(n1);
Step 14: num2=Long.toString(n2);
Step 15: String merg=num1.concat(num2);
Step 16: mergNum=Integer.valueOf(merg);
Step 17: void show() (line 18to20)
Step 18: print ("1st number: "+n1);
Step 19: print ("2nd number "+n2);
Step 20: print ("merged number:"+mergNum);
Step 21: void main() (line 22to25)
Step 22: merger ob=new merger();
Step 23: ob.readNum();
Step 24: ob.joinNum();
Step 25: ob.show();
Stop
Code:
import java.util.*;
class merger
{
Scanner sc=new Scanner(System.in);
long n1,n2,mergNum;

merger()
{
n1=0;
n2=0;
mergNum=0;
}

void readNum()
{
System.out.println("Enter two numbers");
n1=sc.nextLong();
n2=sc.nextLong();
}
void joinNum()
{
String num1="";
String num2="";

num1=Long.toString(n1);
num2=Long.toString(n2);

String merg=num1.concat(num2);
mergNum=Integer.valueOf(merg);
}

void show()
{
System.out.println("1st number: "+n1);
System.out.println("2nd number "+n2);
System.out.println("merged number:"+mergNum);
}
void main()
{
merger ob=new merger();

ob.readNum();
ob.joinNum();
ob.show();
} }
Output:
Enter two numbers
45
54
1st number: 45
2nd number 54
merged number:4554
Variable List:

Variable list Data type Description

n1 long Actual parameter

n2 long Actual parameter

mergNum long Stores the merged


number

num1 String Coverts the value from


long to string

num2 String Coverts the value from


long to string

merg String Merges the two strings

26.Design a class Exchange to accept a sentence and interchange the


first letter with the last letter of each word of that sentence with single
letter word remaining unchanged.The words in the input sentence are
separated by a single blank space and terminated by a full stop.

Algorithm:
Start
Step 1:int size
Step 2:String rev,sent
Step 3:Exchange() (rep. 3 to 6)
Step 4:size=0
Step 5:sent=” “
Step 6:rev=””
Step 7:void readsentence() (rep. 7 to 8)
Step 8:input sent
Step 9:void exfirstlast() (rep. 9 to 20)
Step 10:readsentence();
Step 11:StringTokenizer st = new StringTokenizer(sent)
Step 12:while (st.hasMoreTokens()) (rep. 12 to 19)
Step 13:String word = st.nextToken()
Step 14:if (word.length() > 1)
Step 15:char first = word.charAt(0);
Step 16:char last = word.charAt(word.length() - 1)
Step 17:String middle = word.substring(1, word.length() - 1)
Step 18:rev += last + middle + first + " "
Step 19:rev += word + " "
Step 20:rev=rev.trim()
Step 21:void display() (rep. 21 to 24)
Step 22:exfirstlast();
Step 23:print("Original sentence: " + sent)
Step 24:print("New Sentence: " + rev)
Stop
Code:
import java.util.*;
class Exchange
{
int size;
String rev,sent;
Exchange()
{
size = 0;
sent = "";
rev = "";
}
void readsentence() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
sent = sc.nextLine();
}
void exfirstlast()
{
readsentence();
StringTokenizer st = new StringTokenizer(sent);
while (st.hasMoreTokens())
{
String word = st.nextToken();
if (word.length() > 1)
{
char first = word.charAt(0);
char last = word.charAt(word.length() - 1);
String middle = word.substring(1, word.length() - 1);
rev += last + middle + first + " ";
}
else
{
rev += word + " ";
}
}
rev = rev.trim();
}
void display()
{
exfirstlast();
System.out.println("Original sentence: " + sent);
System.out.println("New Sentence: " + rev);
} }
Output:
Enter a sentence
It is a warm day
Original sentence: It is a warm day
New Sentence: tI si a marw yad
Variable List:

Variable Name Data Type Description

size int Stores length of string

sent String Stores a sentence

rev String Stores new sentence

27.Enter a number and print the number of vowels in the string.


Algorithm:
Start
Step 1:String s
Step 2:str() (rep. 2 to 3)
Step 3:s=””
Step 4:void input() (rep. 4 to 6)
Step 5:Boolean check(char ch) (rep.5 to 7)
Step6:if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||c
h=='o'||ch=='u') then return true
Step 7: else return false
Step 8:void count() (rep. 8 to 17)
Step 9:int l=s.length()
Step 10:int c=0
Step 11:boolean a
Step 12:for(int i=0;i<l;i++) (rep. 12 to 16)
Step 13:char ch1=s.charAt(i)
Step 14:a=check(ch1)
Step 15:if(a==true)
Step 16:c=c+1
Step 17:print("No of vowels: "+c)
Code:
import java.util.Scanner;
class str {
String s;
str() {
s = "";
}
void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
s = scanner.nextLine();
}
boolean check(char ch) {
ch = Character.toLowerCase(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
void count() {
int vowelCount = 0;
for (int i = 0; i < s.length(); i++) {
if (check(s.charAt(i))) {
vowelCount++;
}
}
System.out.println("Number of vowels in the string: " + vowelCount);
}
void main()
{
str ob = new str();
ob.input();
ob.count();
}
}
Output:
Enter a string: elephant
Number of vowels in the string: 3
Variable List:

Variable Name Data Type Description

s string Store the string from the user

ch character Stores the character of the string


vowelcount int Count the number of vowels

28.Enter a string and check if the string is palindrome or not.


Algorithm:
Start
Step 1: String s;
Step 2: str2() (line 3)
Step 3: s="";
Step 4: void input() (line 5to6)
Step 5: println("Enter a String");
Step 6: input s
Step 7: String reverse(String x)
Step 8: rev = "";
Step 9: l = x.length();
Step 10: while(l!=0) (Rep. line 11to12 )
Step 11: rev = rev + x.charAt(l-1);
Step 12: l--;
Step 13: return rev;
Step 14: void check()
Step 15: rev = reverse(s)
Step 16: if(rev.equals(s)) then print (s+" is a Palindrome string");
Step 17: else print (s+" is not a Palindrome string");
Stop
Code:
class str2
{
String s;
str2()
{
s="";
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
s = sc.next();
}
String reverse(String x)
{
String rev = "";
int l = x.length();
while(l!=0)
{
rev = rev + x.charAt(l-1);
l--;
}
return rev;
}

void check()
{
String rev = reverse(s);
if(rev.equals(s))
System.out.println(s+" is a Palindrome string");
else
System.out.println(s+" is not a Palindrome string");
}
}
Output:
Input -> EYE
EYE is a Palindrome string

Variable List:

Variable Name Data Type Description


s String Accepts and stores a string

rev String Stores the reverse of the string

l int Stores length of string

29.Enter 16 values in a 4x4 matrix and find out the maximum element
of each row and minimum element of each column.

Algorithm:
Start
Step 1:int a[][]=new int[4][4]
Step 2:void input() (rep. 2 to 7)
Step 3:int i,j
Step 4:print("Enter 16 values for array")
Step 5:for(i=0;i<4;i++)
Step 6:for(j=0;j<4;j++)
Step 7:a[i][j]=sc.nextInt()
Step 8:void findMax() (rep. 8 to 14)
Step 9:for(int r=0;r<4;r++)
Step 10:int max=a[r][0]
Step 11:for(int c=0;c<4;c++)
Step 12:if(a[r][c]>max)
Step 13:max=a[r][c]
Step 14:print max
Step 15:void findMin() (rep. 15 to 21)
Step 16:for(int r=0;r<0;r++)
Step 17:int min=a[0][r]
Step 18:for(int c=0;c<4;c++)
Step 19:if(a[c][r]<min)
Step 20:min=a[c][r]
Step 21:print min
Step 22: void main() (rep. 22 to
Step 23:matrix_maxmin ob=new matrix_maxmin()
Step 24:ob.input();
Step 25: ob.findMax();
Step 26: ob.findMin();
Stop
Code:
import java.util.*;
class matrix_maxmin
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
void input()
{
int i,j;
System.out.println("Enter 16 values for array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
}

void findMax()
{
for(int r=0;r<4;r++)
{
int max=a[r][0];
for(int c=0;c<4;c++)
{
if(a[r][c]>max)
{
max=a[r][c];
}
}
System.out.println(max);
}
}

void findMin()
{
for(int r=0;r<0;r++)
{
int min=a[0][r];
for(int c=0;c<4;c++)
{
if(a[c][r]<min)
{
min=a[c][r];
}
}
}
}
void main()
{
matrix_maxmin ob=new matrix_maxmin();

ob.input();
ob.findMax();
ob.findMin();

}
}
Output:
Enter 16 values for array
2
3
4
5
6
7
8
9
10
11
12
13
23
23
43
23
result:
5
9
13
43

Variable List:

Variable list Data type Description

a int matrix

i int Loop variable

j int Loop variable


r int Loop for the rows

c int Loop for the column

max int Stores the max value

min int Stores the min value

30.Enter 16 values in a 4x4 matrix and print only the prime numbers
from that matrix
Algorithm:
Start
Step 1: int i,j;
Step 2: int a[][]=new int[4][4];
Step 3: void input() (Rep:4-7)
Step 4: print ("Enter 16 array elements");
Step 5:for (int i = 0; i < 4; i++) (Rep:6-7)
Step 6:for (int j = 0; j < 4; j++) (Rep:7)
Step 7:a[i][j]=sc.nextInt()
Step 10:void check() (Rep:11-23)
Step 11:print(“prime numbers in the matrix”)
Step 12:for (int r = 0; r < 4; r++) (Rep:13-17)
Step 13:for (int c = 0; c < 4; c++) (Rep:14-17)
Step 14:int v = a[r][c]
Step 15:if (v <= 1) (Step:16)
Step 16: continue
Step 17: boolean isPrime = true
Step 18:for (int i = 2; i <= Math.sqrt(v);i++) (Rep:19-23)
Step 19:if (v % i == 0) (Rep:20-23)
Step 20:isPrime = false
Step 21:break
Step22:if (isPrime) (Rep:23)
Step23:print(v)
Step24:void main() (Rep:25-27)
Step25:MatrixPrime ob = new MatrixPrime()
Step26:ob.input()
Step27:ob.check()
Stop
Code:
import java.util.*;
class MatrixPrime
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[4][4];
void input() {
System.out.println("Enter 16 values for the array");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt();
}
}
}
void check()
{
System.out.println("Prime numbers in the matrix:");
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
int v = a[r][c];
if (v <= 1) {
continue;
}
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(v); i++) {
if (v % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(v);
}
}
}
}
void main()
{
MatrixPrime ob = new MatrixPrime();
ob.input();
ob.check();
} }
Output:
Enter 16 values for the array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Prime numbers in the matrix:
2
3
5
7
11
13
Variable List:

Variable list Data Type Description

a int Array Variable

i int Loop variable

j int Loop variable

r int Loop variable

c int Loop variable

v int Stores the prime numbers

31.Input 9 elements in a 3x3 matrix and check whether the square


matrix is a magic square or not.
[A magic square matrix is one in which the sum of all the rows, all the
columns and both the diagonals are equal].

Algorithm:
Start
Step 1: int i,j;
Step 2: int s1=0,s2=0,s3=0,s4=0;
Step 3: int a[][]=new int[4][4];
Step 4: void display()
Step 5: print ("Enter 16 array elements");
Step 6: for(i=0;i<4;i++) (Rep. line 7to8)
Step 7: for(j=0;j<4;j++) (Rep. line 8)
Step 8: a[i][j]=sc.nextInt();
Step 9: for(i=0;i<4;i++) (Rep. line 10to13)
Step 10: s1=0;
Step 11: for(j=0;j<4;j++) (Rep. line 12)
Step 12: s1=s1+a[i][j];
Step 13: print("Sum of row "+i+" is "+s1);
Step 14: for(i=0;i<4;i++) (Rep. line 15to18)
Step 15: s1=0;
Step 16: for(j=0;j<4;j++) (Rep. line 17)
Step 17: s1=s1+a[i][j];
Step 18: print("Sum of column"+i+" is "+s2);
Step 19: for(i=0;i<4;i++) (Rep. line20)
Step 20: s3=s3+a[i][i];
Step 21: print("Sum of left diagonal is "+s3);
Step 22: for(i=0;i<4;i++) (Rep. line23)
Step 23: s4=s4+a[i][3-i];
Step 24: print ("Sum of right diagonal is "+s4);
Step 25: if(s1==s2 && s1==s3 && s1==s4 && s2==s3 && s2==s4 && s3==s4)
Step 26: then print ("It is a magic square");
Step 27: else print ("It is not a magic square");
Stop
Code:
import java.util.*;
class magic_square
{
Scanner sc=new Scanner(System.in);
int i,j;
int s1=0,s2=0,s3=0,s4=0;
int a[][]=new int[4][4];
void display()
{
System.out.println("Enter 16 array elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}

//sum of rows
for(i=0;i<4;i++)
{ s1=0;
for(j=0;j<4;j++)
{
s1=s1+a[i][j];
}
System.out.println("Sum of row "+i+" is "+s1);
}
for(i=0;i<4;i++)
{ s2=0;
for(j=0;j<4;j++)
{
s2=s2+a[j][i];
}
System.out.println("Sum of column "+i+" is "+s2);
}

for(i=0;i<4;i++)
{
s3=s3+a[i][i];
}
System.out.println("Sum of left diagonal is "+s3);

for(i=0;i<4;i++)
{
s4=s4+a[i][3-i];
}
System.out.println("Sum of right diagonal is "+s4);

if(s1==s2 && s1==s3 && s1==s4 && s2==s3 && s2==s4 && s3==s4)
{
System.out.println("It is a magic square");
}
else
{
System.out.println("It is not a magic square");
}
} }
Output:
Enter 16 array elements
1
2
3
4
5
6
7
8
9
1
11
12
13
14
15
16
Sum of row 0 is 10
Sum of row 1 is 26
Sum of row 2 is 33
Sum of row 3 is 58
Sum of column 0 is 28
Sum of column 1 is 23
Sum of column 2 is 36
Sum of column 3 is 40
Sum of left diagonal is 34
Sum of right diagonal is 25
It is not a magic square

Variable List:

Variable Name Data Type Description

s1 int Stores sum of row

s2 int Stores sum of column

s3 int Stores Sum of left diagonal

s4 int Stores Sum of right diagonal

a int Array variable

i int Loop variable


j int Loop variable

32.Enter 9 values each in two 3x3 matrices and calculate their sum and
store it in a third matrix. Display all three matrices.
Algorithm:
Start
Step 1: int m1[][]=new int[3][3];
Step 2: int m2[][]=new int[3][3];
Step 3: int m3[][]=new int[3][3];
Step 4: int i,j,s=0;
Step 5: public void main() (line 6 to 31)
Step 6: print ("Enter 9 values in the first matrix");
Step 7: for(i=0;i<3;i++) (rep. 7 to 9)
Step 8: for(j=0;j<3;j++)
Step 9: m1[i][j]=sc.nextInt();
Step 10: print ("Enter 9 values in the second matrix");
Step 11: for(i=0;i<3;i++) (rep. 11 to 13)
Step 12: for(j=0;j<3;j++)
Step 13: m2[i][j]=sc.nextInt();
Step 14: for(i=0;i<3;i++) (rep. 14 to 16)
Step 15: for(j=0;j<3;j++)
Step 16: m3[i][j]=m1[i][j]+m2[i][j];
Step 17: print ("first matrix:");
Step 18: for(i=0;i<3;i++)
Step 19: for(j=0;j<3;j++)
Step 20: print(m1[i][j]+" ");
Step 21: System.out.println();
Step 22: print ("second matrix:");
Step 23: for(i=0;i<3;i++)
Step 24: for(j=0;j<3;j++)
Step 25: print(m2[i][j]+" ");
Step 26: System.out.println();
Step 27: print ("final matrix:");
Step 28: for(i=0;i<3;i++)
Step 29: for(j=0;j<3;j++)
Step 30: print(m3[i][j]+" ");
Step 31: System.out.println();
Stop
Code:
import java.util.*;
class matrix_sum
{
Scanner sc=new Scanner(System.in);
int m1[][]=new int[3][3];
int m2[][]=new int[3][3];
int m3[][]=new int[3][3];
int i,j,s=0;

public void main()


{
System.out.println("Enter 9 values in the first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m1[i][j]=sc.nextInt();
}
}

System.out.println("Enter 9 values in the second matrix");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m2[i][j]=sc.nextInt();
}
}
//Adding both arrays
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}

System.out.println("first matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println();
}

System.out.println("second matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(m2[i][j]+" ");
}
System.out.println();
}

System.out.println("final matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(m3[i][j]+" ");
}
System.out.println();
} } }
Output:
Enter 9 values in the first matrix
1
2
3
4
5
6
7
8
9
Enter 9 values in the second matrix
1
2
3
4
5
6
7
8
9
first matrix:
123
456
789
second matrix:
123
456
789
final matrix:
246
8 10 12
14 16 18
Variable List:

Variable List Data type Description

m1 int Matrix array

m2 int Matrix array

m3 int Matrix array

i int Loop variable

j int Loop variable

33.Enter values in a matrix whose number of rows and columns is to be


input by user and print its Saddle Point.(Saddle Point of a matrix is the
value which is lowest of its row and the highest of its column).
Algorithm:
Start
Step 1: int i,j,m,n;
Step 2: int row_min,colIndex;
Step 3: void display() (line 3 to 27)
Step 4: print ("Enter the number of rows and columns");
Step 5: m=sc.nextInt();
Step 6: n=sc.nextInt();
Step 7: int a[ ][ ]=new int[m][n];
Step 8: print ("Enter array elements");
Step 9: for(i=0;i<m;i++) (Rep. line 9 to 11)
Step 10: for(j=0;j<n;j++) (Rep. line 11)
Step 11: a[i][j]=sc.nextInt();
Step 12: print ("The array:");
Step 13: for(i=0;i<m;i++) (Rep. line 14 to 16)
Step 14: for(j=0;j<n;j++) (Rep. line 15)
Step 15: print (a[i][j]+" ");
Step 16: print ();
Step 17: for (i=0;i<m;i++)
Step 18: row_min=a[i][0];
Step 19: colIndex = 0;
Step 20: for (j=1;j<n; j++)
Step 21: if(a[i][j] < row_min)
Step 22: row_min = a[i][j];
Step 23: colIndex = j;
Step 24: for (j=0;j<n;j++) (Rep. line 24 to 26)
Step 25: if(a[j][colIndex]>row_min) then print("There is no saddle point");
Step 26: then break;
Step 27: print("Saddle point: "+row_min);
Stop
Code:
import java.util.*;
class saddle_DDA
{
Scanner sc=new Scanner(System.in);
int i,j,m,n;
int row_min,colIndex;
void display()
{
System.out.println("Enter the number of rows and columns");
m=sc.nextInt();
n=sc.nextInt();
int a[ ][ ]=new int[m][n];
System.out.println("Enter array elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}

System.out.println("The array:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}

for (i=0;i<m;i++)
{
row_min=a[i][0];
colIndex = 0;
for (j=1;j<n; j++)
{
if(a[i][j] < row_min)
{
row_min = a[i][j];
colIndex = j;
}
}
}
for (j=0;j<n;j++)
{
if(a[j][colIndex]>row_min)
{
System.out.println("There is no saddle point");
break;
}
}
System.out.println("Saddle point: "+row_min);
} }
Output:
Enter the number of rows and columns
3
3
Enter array elements
1
2
3
4
5
6
7
8
9
The array:
123
456
789
Saddle point: 7
Variable List:

Variable Name Data Type Description


a int Array variable

i int Loop variable

j int Loop variable

m int Stores number of rows

n int Stores number of columns

row_min int Stores minimum element of row

colIndex int Stores the index number of column

34.Design a class eqMat to check if two matrices are equal or


not.Assume that the two matrices have the same dimensions.
Algorithm:
Start
Step 1: i=0,j=0,flag=0
Step 2: input r
Step 3: input c
Step 4: int A[][]=new int[r][c];
Step 5: int B[][]=new int[r][c];
Step 6: p=r*c
Step 7: print("Enter "+p+" values for array A row-wise");
Step 8: for(i=0;i<r;i++) (Rep.8 to 10)
Step 9: for(j=0;j<c;j++) (Rep.10)
Step 10: A[i][j]=sc.nextInt();
Step 11: print("Enter "+p+" values for array B row-wise");
Step 12: for(i=0;i<r;i++) (Rep.12 to 14)
Step 13: for(j=0;j<c;j++) (Rep.14)
Step 14: B[i][j]=sc.nextInt();
Step 15: for (i=0;i<r;i++) (Rep.15 to 17)
Step 16: for (j=0;j<c;j++)(Rep.17)
Step 17: if (A[i][j] == B[i][j]) then flag++;
Step 18: if(flag>0) then print("The two matrixes are equal");
Step 19: else print("The two matrixes are not equal");
Stop
Code:
import java.util.Scanner;
class eqMat
{
public static void main()
{
int i=0,j=0,flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of rows");
int r=sc.nextInt();
System.out.println("Enter no of columns");
int c=sc.nextInt();
int A[][]=new int[r][c];
int B[][]=new int[r][c];
int p=r*c;
System.out.println("Enter "+p+" values for array A row-wise");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Enter " + p + " values for array B row-wise");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
B[i][j]=sc.nextInt();
}
}
for ( i = 0; i < r; i++)
{
for ( j = 0; j < c; j++)
{
if (A[i][j] == B[i][j])
{
flag++;
}
}
}
if(flag>0)
{
System.out.println("The two matrixes are equal");
}
else
{
System.out.println("The two matrixes are not equal");
} } }
Output:
Enter no of rows
3
Enter no of columns
3
Enter 9 values for array A row-wise
1
2
3
4
5
6
7
8
9
Enter 9 values for array B row-wise
1
2
3
4
5
6
7
8
9
The two matrices are equal
Variable List:

Variable list Data type Description

i int Loop variable

j int Loop variable

flag int Flag variable

r int Stores number of rows

c int Stores number of columns

a int Matrix variable


b int Matrix variable

35.Write a program to declare a square matrix M[][] of order ‘N’ where ‘N’
must be greater than 3 and less than 10.Allow the user to accept three
different characters from the keyboard and fill the array accordingly:
(i) Fill four corners of the square matrix by 1 character.
(ii) Fill the boundary elements of the matrix(Except Corners) with
character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.
Algorithm:
Start
Step 1: input N
Step 2: if(N>3&&N<10)
Step 3: input ch1,ch2,ch3
Step 4: char ch[][]= new char[N][N]
Step 5: ch[0][0] = ch1;
Step 6: ch[0][N-1] = ch1;
Step 7: ch[N-1][0] = ch1;
Step 8: ch[N-1][N-1] = ch1;
Step 9: for(i=1;i<N-1;i++) (Rep.8 to 10)
Step 10: for(j=1;j<N-1;j++) (Rep.10)
Step 11: ch[i][j] = ch3;
Step 12: for(i=0;i<=N-1;i+=N-1)(Rep.8 to 10)
Step 13: for(j=1;j<N-1;j++)(Rep.10)
Step 14: ch[i][j] = ch2;
Step 15: for(i=0;i<=N-1;i+=N-1)(Rep.8 to 10)
Step 16: for(j=1;j<N-1;j++)(Rep.10)
Step 17: ch[j][i] = ch2;
Step 18: for(i=0;i<=N-1;i++)(Rep.8 to 10)
Step 19: for(j=0;j<=N-1;j++)(Rep.10)
Step 20: System.out.print(ch[i][j]);
Step 21: System.out.println();
Step 22: else print("The dimensions are inappropiate");
Stop
Code:
import java.util.Scanner;
public class SquareMatrix
{
public static void main()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the order 'N' of the square matrix (greater than
3 and less than 10): ");
int N=scanner.nextInt();
if (N > 3 && N < 10)
{ System.out.println("Invalid input for N. It must be greater than 3
and less than 10.");
System.out.print("Enter the first character: ");
char char1 = scanner.next().charAt(0);
System.out.println();
System.out.print("Enter the second character: ");
char char2 = scanner.next().charAt(0);
System.out.println();
System.out.print("Enter the third character: ");
char char3 = scanner.next().charAt(0);
System.out.println();
char matrix[ ][ ] = new char[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if ((i == 0 && j == 0) || (i == 0 && j == N - 1) ||(i == N - 1 && j == 0) || (i ==
N - 1 && j == N - 1))
{
matrix[i][j] = char1;
}
else if (i == 0 || i == N - 1 || j == 0 || j == N - 1)
{
matrix[i][j] = char2;
}
else
{
matrix[i][j] = char3;
}
}
}
System.out.println("Resultant Matrix:");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
else
{
System.out.println("Invalid input");
} } }
Output:
Enter the order 'N' of the square matrix (greater than 3 and less than 10): 4
Invalid input for N. It must be greater than 3 and less than 10.
Enter the first character: @
Enter the second character: ?
Enter the third character: #
Resultant Matrix:
@??@
?##?
?##?
@??@
Variable List:
Variable Name Data Type Description

matrix int 2-D Array

j int Loop variable

i int Loop variable

sum int Used for summation

N int Accepts

36.Input 16 values in a 4x4 matrix.Display only the boundary elements.


Calculate their sum.
Algorithm:
Start
Step 1: int A[][]= new int[4][4];
Step 2: input A
Step 3: int A[][]= new int[4][4];
Step 4: sum=0,N=A.length;
Step 5: sum+=A[0][0]+A[0][N-1]+A[N-1][0]+A[N-1][N-1];
Step 6: for(i=0;i<=N-1;i+=N-1)(Rep.13 to 14)
Step 7: for(j=1;j<N-1;j++)(Rep.14)
Step 8: sum+=A[i][j];
Step 9: for(i=0;i<=N-1;i+=N-1)(Rep.16 to 17)
Step 10: for(j=1;j<N-1;j++)(Rep.17)
Step 11: sum+=A[j][i];
Step 12: print(sum);
Step 13: System.out.println();
Stop
Code:
import java.util.*;
class Boundary
{
void display()
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter values row-wise");
int A[][]= new int[4][4];
int i,j,sum=0,N=A.length;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
sum+=A[0][0]+A[0][N-1]+A[N-1][0]+A[N-1][N-1];
for(i=0;i<=N-1;i+=N-1)
{
for(j=1;j<N-1;j++)
{
sum+=A[i][j];
}
}
for(i=0;i<=N-1;i+=N-1)
{
for(j=1;j<N-1;j++)
{
sum+=A[j][i];
}
}
System.out.println(sum);
} }
Output:
Enter values row-wise
Enter the 3 characters serially
2
2
2
2
2
3
3
2
2
3
3
2
2
2
2
2
24
Variable List:

Variable Name Data Type Description

A int 2-D Array

i int Loop variable

j int Loop variable

sum int Used for summation

N int Stores length of the array A


1. Combination using recursion
Algorithm:
Step 1: void main()
Step 2: Create Combination object
Step 5: [line 1-31]
Step 6: private int n, k
Step 7: public Combination()
Step 8: n = 0, k = 0
Step 9: void read() [line 13-22]
Step 10: Scanner scanner = new Scanner(System.in)
Step 11: Input n and k
Step 12: public int fact(int num) [line 24-32]
Step 13: if (num == 0 || num == 1) [step 13-14]
Step 14: return 1
Step 15: else [step 15-16]
Step 16: return num * fact(num - 1)
Step 17: public int compute() [line 34-47]
Step 18: if (n < k || n < 0 || k < 0) [step 18-19]
Step 19: Print error message
Step 20: return -1
Step 21: else [step 21-22]
Step 22: return fact(n) / (fact(k) * fact(n - k))
Step 23: public void display() [line 49-55]
Step 24: int result = compute()
Step 25: if (result != -1) [step 25-26]
Step 26: Print the result
Step 27: public static void main(String[] args) [line 57-62]
Step 28: Combination combination = new Combination()
Step 29: combination.read()
Step 30: combination.display()
Stop

Code:
import java.util.Scanner;
class Combination
{
private int n;
private int k;
public Combination()
{
n = 0;
k = 0;
}
public void read()
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the value of n: ");


n = scanner.nextInt();

System.out.print("Enter the value of k: ");


k = scanner.nextInt();
}
public int fact(int num)
{
if (num == 0 || num == 1)
{
return 1;
} else
{
return num * fact(num - 1);
}
}
public int compute()
{
if (n < k || n < 0 || k < 0)
{
System.out.println("Invalid input! n should be greater than or equal to
k, and both should be non-negative.");
return -1;
}
else
{
return fact(n) / (fact(k) * fact(n - k));
}
}
public void display()
{
int result = compute();
if (result != -1)
{
System.out.println("C(" + n + ", " + k + ") = " + result);
}
}
}
public class Main
{
public static void main(String[] args)
{
Combination combination = new Combination();
combination.read();
combination.display();
}
}
Output:
Input value of n:
6
Input value of k:
2
Number of combinations is 15
Variable List:

Variable Name Data Type Description

n int Accepts value from user

k int Accepts value from user

num int Calculates and returns factorial

i int Loop variable

2. Hamming Number
Algorithm:
Start
Step 1: void main() [line 2-3]
Step 2: input n
Step 3: int nn=n
Step 4: while(n%2==0) [step 4-5]
Step 5: n=n/2
Step 6: while(n%3==0) [step 6-7]
Step 7: n=n/3
Step 8: while(n%5==0) [step 8-9]
Step 9: n=n/5
Step 10:if(n==0)
Step 11:print("It is a Hamming number")
Step 12:else print("It is not a Hamming number")
Stop
Code:
import java.util.*;
class Hamming_number
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int nn=n;
while(n%2==0)
{
n=n/2;
}
while(n%3==0)
{
n=n/3;
}
while(n%5==0)
{
n=n/5;
}
if(n==1)
{
System.out.println("It is a Hamming number");
}
else
{
System.out.println("It is not a Hamming number");
}
}
}
Output:
Input a number:
10
It is a Hamming number
Input a number:
14
It is not a Hamming number

Variable List:

Variable Name Data Type Description

n int Stores number to be checked

s int Stores sum of factors

i int Loop variable

3. Count
Algorithm:
Start
Step 1: Initialize n = 0 and ctr = 0 [Constructor]
Step 2: input() [Function to input value of n]
Step 3: Print "Enter a number: "
Step 4: Take input into n
Step 5: countdg() [Function to count the number of odd digits]
Step 6: Initialize num = n
Step 7: while (num > 0) [Loop until num becomes 0]
Step 8: digit = num % 10 [Extract the last digit]
Step 9: if (digit % 2 != 0) [Check if the digit is odd]
Step 10: Increment ctr
Step 11: num = num / 10 [Remove the last digit]
Step 12: display() [Function to display the number of odd digits]
Step 13: Print "Number of odd digits in " << n << " is: " << ctr << endl;
Stop

Code:
class count {
int n;
int ctr;

count() {
n = 0;
ctr = 0;
}

void input() {
count << "Enter a number: ";
cin >> n;
}
void countdg() {
int num = n;
while (num > 0) {
int digit = num % 10;
if (digit % 2 != 0) {
ctr++;
}
num /= 10;
}
}
void display() {
cout << "Number of odd digits in " << n << " is: " << ctr << endl;
}
}

int main() {
count obj;
obj.input();
obj.countdg();
obj.display();
return 0;
}
Output:
Enter a number: 12345
Number of odd digits in 12345 is: 3
Variable List:

Variable Name Data Type Description

n int Accepts value from user

ctr int counter

4. Money
Algorithm:
Start
Step 1: class Money [Define class Money]
Step 2: Declare variables rs, ps
Step 3: Define parameterized constructor Money(int rs1, int ps1)
Step 4: Set rs = rs1 and ps = ps1
Step 5: Define default constructor Money()
Step 6: Set rs = 0 and ps = 0
Step 7: Define method fnShow()
Step 8: Print "Rs" + rs + "." + ps
Step 9: Define method fnAdd(Money m1, Money m2)
Step 10: Set rs = m1.rs + m2.rsStep 1.5.2: Set ps = m1.ps + m2.ps
Step 11: If ps >= 10
Step 12: Increment rs y 1
Step 13: Subtract 100 from ps
Step 14: Return the current object reference (this)
Step 15: main(String args[]) [Main method]
Step 16: Create objects of class Money - Am, bm, sm
Step 17: Initialize Am with parameters (200, 50)
Step 18: Initialize bm with parameters (200, 50)
Step 19: Initialize sm with default constructor
Step 20: Call method fnAdd(Am, bm) on object sm
Step 21: Print "Money 1: "
Step 22: Call method fnShow() on object Am
Step 23: Print "Money 2: "
Step 24: Call method fnShow() on object bm
Step 25: Print "Money 1 + Money 2: "
Step 26: Call method fnShow() on object sm
Stop
Code:
import java.util.*;
class Money
{
int rs, ps;
public Money(int rs1, int ps1)
{
rs=rs1;
ps=ps1; }
public Money()
{
rs=0;
ps=0;
}
void fnShow()
{
System.out.println("Rs"+rs+"."+ps);
}
Money fnAdd(Money m1, Money m2)
{
rs=m1.rs+m2.rs;
ps=m1.ps+m2.ps;
if(ps>=100)
{
rs++;
ps-=100;
}
return this;
}

public static void main()


{
Money Am =new Money(200, 50);
Money bm=new Money(200,50);
Money sm=new Money();
sm.fnAdd(Am,bm);
System.out.print("\n Money 1: ");
Am.fnShow();
System.out.print("\n Money 2: ");
bm.fnShow();
System.out.print("\n Money 1 + Money 2: ");
sm.fnShow();
}

}
Output:
Money 1: Rs200.50
Money 2: Rs200.50
Money 1 + Money 2: Rs401.0
Variable List:

Variable Name Data Type Description

rs int Stores Paisa

ps int Stores Rupees


5. Combine
Algorithm:
Start
Step 1: class Combine [Define class Combine]
Step 2: Declare variables com[] (integer array), size (integer)
Step 3: Create a Scanner object sc
Step 4: Define parameterized constructor Combine(int nn)
Step 5: Set size = nn
Step 6: Initialize com[] with size
Step 7: Define method inputarray()
Step 8: Print "Enter" + size + "elements"
Step 9: Iterate i from 0 to size-1
Step 10: Take input into com[i]
Step 11: Define method sort()
Step 12: Declare variable t (temporary)
Step 13: Iterate i from 0 to size-1
Step 14: Iterate j from i to size-1
Step 15: If com[j] < com[i]
Step 16: Swap com[j] and com[i]
Step 17: Define method mix(Combine A, Combine B)
Step 18: Declare variables x = 0, y = 0, z = 0
Step 19: While x < A.size
Step 20: Assign com[z] = A.com[x]
Step 21: Increment x and z
Step 22: While y < B.size
Step 23: Assign com[z] = B.com[y]
Step 24: Increment y and z
Step 25: Define method display()
Step 26: Iterate i from 0 to size-1
Step 27: Print com[i] followed by space
Step 28: main() [Main method]
Step 29: Create a Scanner object sc1
Step 30: Print "Enter size of first array"
Step 31: Take input into a
Step 32: Create object P of class Combine with parameter a
Step 33: Call method inputarray() on object
Step 34: Print "Enter size of Second array"
Step 35: Take input into b
Step 36: Create object Q of class Combine with parameter b
Step 37: Call method inputarray() on object Q
Step 38: Create object R of class Combine with parameter (a + b)
Step 39: Call method mix(P, Q) on object R
Step 40: Call method sort() on object R
Step 41: Print "Array 1:"
Step 42: Call method display() on object P
Step 43: Print "Array 2:"
Step 44: Call method display() on object
Step 45: Print "Combined Array:"
Step 46: Call method display() on object R Stop
Code:
import java.util.Scanner;public class Combine
{
int com[]=new int[100];
int size;
Scanner sc=new Scanner(System.in);
Combine(int nn)
{
size=nn;
com=new int[size];
}
void inputarray()
{
System.out.println("\nEnter "+size+" elements");
for(int i=0;i< size;i++)
{
com[i]=sc.nextInt();
}
}
void sort()
{
int t;
for(int i=0;i< size;i++)
{
for(int j=i;j< size;j++)
{
if(com[j]< com[i])
{
t=com[j];
com[j]=com[i];
com[i]=t;
}
}
}
}
void mix(Combine A,Combine B)
{
int x=0,y=0,z=0;
while(x< A.size)
{
com[z++]=A.com[x++];
}
while(y< B.size)
{
com[z++]=B.com[y++];
}
}
void display()
{
for(int i=0;i< size;i++)
{
System.out.print(com[i]+" ");
}
}
public static void main()
{
Scanner sc1=new Scanner(System.in);
System.out.println("\n enter size of first array");
int a=sc1.nextInt();
Combine P=new Combine(a);
P.inputarray();
System.out.println("\n Enter size of Second array");
int b=sc1.nextInt();
Combine Q=new Combine(b);
Q.inputarray();
Combine R=new Combine(a+b);
R.mix(P,Q);
R.sort();
System.out.println("Array 1:");
P.display();
System.out.println();
System.out.println("Array 2:");
Q.display();
System.out.println();
System.out.println("Combined Array:");
R.display();
}

Output
enter size of first array
6
Enter 6 elements
12
14
36
22
34
11
Enter size of Second array
6
Enter 6 elements
43
1
2
3
89
70
Array 1:
12 14 36 22 34 11
Array 2:
43 1 2 3 89 70
Combined Array:
1 2 3 11 12 14 22 34 36 43 70 89
Variable List:

Variable Name Data Type Description

size int Int to store size of the


array

com[] int Array to store integers

j int Loop variable

i int Loop variable

6. Adder
Algorithm:
Start
Step 1: class Money [Define class Money]
Step 2: Declare variables rs, ps
Step 3: Define parameterized constructor Money(int rs1, int ps1)
Step 4: Set rs = rs1 and ps = ps1
Step 5: Define default constructor Money()
Step 6: Set rs = 0 and ps = 0
Step 7: Define method fnShow()
Step 8: Print "Rs" + rs + "." + ps
Step 9: Define method fnAdd(Money m1, Money m2)
Step 10: Set rs = m1.rs + m2.rs
Step 11: Set ps = m1.ps + m2.ps
Step 12: If ps >= 100
Step 13: Increment rs by 1
Step 14: Subtract 100 from ps
Step 15: Return the current object reference (this)
Step 16: main(String args[]) [Main method]
Step 17: Create objects of class Money - Am, bm, sm
Step 18: Initialize Am with parameters (200, 50)
Step 19: Initialize bm with parameters (200, 50)
Step 20: Initialize sm with default constructor
Step 21: Call method fnAdd(Am, bm) on object sm
Step 22: Print "Money 1: "
Step 23: Call method fnShow() on object Am
Step 24: Print "Money 2: "
Step 25: Call method fnShow() on object bm
Step 26: Print "Money 1 + Money 2: "
Step 27: Call method fnShow() on object sm
Stop
Code:
import java. util.Scanner;
public class Adder
{
int a[]=new int[2];
Scanner sc=new Scanner( System.in);
Adder()
{
a[0]=0;
a[1]=0;
}

void readtime()
{
System.out.println("Enter hours and minutes");
a[0]=sc. nextInt();
a[1]=sc. nextInt();
}

void disptime()
{
System. out.println("Hours=" + a[0]);
System.out.println("Minutes=" + a[ 1]);
}

void addtime(Adder X,Adder Y)


{
/* adds minutes */
a[1]=X.a[1] + Y.a[1];
if(a[1]>=60)
{
a[0]=a[1]/60;
a[1]=a[1]%60;
}
a[0] += X.a[0] + Y.a[0];
}
public static void main()
{
Adder ob1=new Adder();
Adder ob2=new Adder();
Adder ob3=new Adder();
ob1.readtime();
ob2.readtime() ;
ob3.addtime(ob1,ob2);
ob3.disptime();
}

Output:
Enter hours and minutes
2
31
Enter hours and minutes
3
32
Hours=6
Minutes=3

Variable List:

Variable Name Data Type Description

a[] int Array variable


7. Rec 1
Algorithm:
Start
Step 1: class rec1 [line 1-13]
Step 2: public void main() [line 3-12]
Step 3: int number, result
Step 4: System.out.println("Enter a positive integer:")
Step 5: number = sc.nextInt()
Step 6: result = sum(number)
Step 7: System.out.println("sum = %d"+ result)
Step 8: int sum(int n) [line 14-18]
Step 9: if (n != 0) [step 9-11]
Step 10: return n + sum(n-1)
Step 11: return n
Step 12: else [step 12-13]
Stop
Code:
class rec1
{
public void main()
{
int number, result;
System.out.println("Enter a positive integer:");
number=sc.nextInt() result = sum(number);
System.out.println("sum = %d"+ result);
}
int sum(int n)
{
if (n != 0)
return n + sum(n-1); return n; 120
else
}
}
Output:
Enter a positive integer:
5
sum = 15
Variable List:

Variable Name Data Type Description

number int Stores the number

result int Stores the result

8. Write a program to print the sum of factors of a number.


Algorithm:
Start
Step 1: num1() [line 2-3]
Step 2: s=0,n=0
Step 3: void input() [line 4]
Step 4: input n
Step 5: void sumfact() [line 6-8]
Step 61: for(i=1; i<=n;i++) [Rep: 7-8]
Step 7: if(n%i==0) [rep 8]
Step 8: s=s+i
Step 9: void display() [line 10-11]
Step 10:sumfact()
Step 11:print("Sum of factors is:"+s)
Stop
Code:
import java.util.Scanner;
class num1
{
int n,s;
void num()
{
s=0;
n=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void sumfact()
{
int i;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
s=s+i;
}
}
}
void display()
{
s=0;
sumfact();
System.out.println("The sum of the factors of "+n+" is "+s);
} }
Output:
The sum of the factors of 5 is 6
Variable list:

Variable Name Data Type Description

n int Stores number to be checked


s int Stores sum of factors

i int Loop variable

10. Write a program to print the reverse of a number.


Algorithm:
Start
Step1:num2() line-2
Step2:n=1,r=1
Step3:void input() line4
Step4:input n
Step5:void rev() line 6-9
Step6:while(n!=0) rep line 7-9
Step7:d=n%10
Step8:r=r*10+d
Step9:n=n/10
Step10:void display() line11-12
Step11:print("Reversed number"+r)
Stop
Code:
import java.util.Scanner;
class num2
{
int n,r;
num2()
{
n=1;
r=1;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void rev()
{
while(n!= 0)
{
int d = n % 10;
r = r * 10 + d;
n=n/10;
}
}
void display()
{
rev();
System.out.println(" Reversed number:"+r);
}
}
Output:
Enter a number:
451
Reversed number:154
Variable List:

Variable Name Data Type Description

n int Stores number

r int Stores the last digits

11. Write a program to accept a number and print the number of factors.
Algorithm:
Start
Step 1: num3()
Step 2: c=0;
Step 3: n=0;
Step 4: i=0;
Step 5: void input()
Step 6: input n
Step 7: void count()
Step 8: c=0;
Step 9: i=1;
Step 10: while(i<=n) (Rep.11to12)
Step 11: if(n%i==0) then c++;
Step 12: i++;
Step 13: void display()
Step 14: count();
Step 15: print("The no. of factors is:"+c);
Stop
Code:
import java.util.Scanner;
class num3
{
int n,c,i;
void num()
{
c=0;
n=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
void count()
{
c=0;
i=1;
while(i<=n)
{
if(n%i==0)
{
c++;
}
i++;
}
}
void display()
{
c=0;
count();
System.out.println("The no. of factors is:"+c);
}
}
Output:
Enter the number:
5
The no. of factors is:2
Variable List:

Variable Name Data Type Description

n int Stores number to be checked

c int counter
i int Loop variable

12. Write a program to accept a number and check whether it is a


sumproduct number or not.
Algorithm:
Start
Step 1:int n
Step 2:sumproduct() (rep. 2 to 3)
Step 3:n=0
Step 4:void readnum() (rep. 4 to 5)
Step 5:input n
Step 6:int sum(int v) (rep. 6 to 12)
Step 7:d=0,s=0
Step 8:while(v>0) (rep. 9 to 11)
Step 9:d=v%10
Step 10:s=s+d
Step 11:v=v/10
Step 12:return s
Step 13:int product(int v) (rep. 13 to 19)
Step 14:d=0,p=1
Step 15:while(v>0) (rep. 15 to 18)
Step 16:d=v%10
Step 17:p=p*d
Step 18:v=v/10
Step 19:return p
Step 20:void check()
Step 21:int a=sum(n)
Step 22:int b=product(n)
Step 23:if(a*b==n) then print ("It is a sumproduct number")
Step 20:else print ("It is not a sumproduct number")
Stop
Code:
import java.util.Scanner;
class sumproduct
{
int n;
sumproduct()
{
n=0;
}
void readnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt();
}
int sum(int v)
{
int d=0,s=0;
while(v>0)
{
d=v%10;
s=s+d;
v=v/10;
}
return s;
}
int product (int v)
{
int d=0,p=1;
while(v>0)
{
d=v%10;
p=p*d;
v=v/10;
}
return p;
}
void check()
{
int a=sum(n);
int b=product(n);
if(a*b==n)
{
System.out.println("It is a sumproduct number");
}
else
{
System.out.println("It is not a sumproduct number");
}
}
}

Output:
Enter a number
144
It is a sumproduct number
Variable List:

Variable name Data Type Description

n int Stores input from user

v int Formal parameter

d int Stores remainder


s int Stores sum of remainder

v int Stores quotient of the number

p int Stores product of the remainder

a int Stores the sum

b int Stores the product

13. Write a program to check whether a number is a Smith number or


not:
Algorithm:
Start
Step 1: define a function boolean isSmithnumber(n)
Step 2: int digitSum = 0
Step 3: temp=n
Step 4: while(temp>0) [repeat 5 to 6]
Step 5: digitSum=digitSum+temp % 10;
Step 6: temp=temp/10;
Step 7: primefsum=0
Step 8: f=2
Step 9: while (n > 1)
Step 10: if n % f == 0 then primefSum += getDigitSum(f);
Step 10: else f++
Step 11: return (digitSum == primefSum)
Step 12: define a function getdigitsum(n)
Step 13: sum=0
Step 14: while(n>0) [repeat 15 & 16]
Step 15: sum=sum+n%10;
Step 16: n=n/10;
Step 17: return sum
Step 18: input num
Step 19: if (isSmithnumber(num)) then print it is a smith number
Step 20: else print it is not a smith number
Stop
Code:
import java.util.*;
class Smith
{
boolean isSmithnumber(int n)
{
int digitSum = 0;
int temp = n;
while (temp > 0)
{
digitSum=digitSum+temp % 10;
temp=temp/10;
}
int primefSum = 0;
int f = 2;
while (n > 1)
{
if (n % f == 0)
{
primefSum += getDigitSum(f);
n=n/f;
}
else
{
f++;
}
}
return (digitSum == primefSum);
}
int getDigitSum(int n)
{
int sum = 0;
while (n > 0)
{
sum=sum+n%10;
n=n/10;
}
return sum;
}
void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (isSmithnumber(num))
{
System.out.println(num + " is a Smith number.");
}
else
{
System.out.println(num + " is not a Smith number.");
}
}
}
Output:
Enter a number: 22
22 is a Smith number.
Variable List:

Variable Name Data Type Description

n int number to be used for checking


Digitsum int sum of the digits of the number

Temp int stores temporary value for n

Primefsum int stores sum of the prime factors of the number

f int Flag variable

Sum int stores sum of

Num int number to be checked by the user

isSmithnumber boolean function to check smith number

getDigitSum Int stores sum of the digits of the number

14.FiboString
Algorithm:
Start
Step 1: class FiboString [line 5-28]
Step 2: String x, y, z
Step 3: int n
Step 4: FiboString() [line 8-13]
Step 5: x = "a", y = "b", z = "ba"
Step 6: void accept() [line 15-21]
Step 7: Scanner Sc = new Scanner(System.in)
Step 8: Input n
Step 9: void generate() [line 23-32]
Step 10: Print x + "," + y
Step 11: for (int i = 0; i <= n - 2; i++) [step 11-18]
Step 12: Print "," + z
Step 13: x = y
Step 14: y = z
Step 15: z = y + x
Step 16: static void main() [line 34-39]
Step 17: FiboString obj = new FiboString()
Step 18: obj.accept()
Step 19: obj.generate()
Stop
Code:
import java.util.*;
class FiboString
{
String x,y,z;
int n;
FiboString()
{
x="a";
y="b";
z="ba";
}
void accept()
{
Scanner Sc = new Scanner (System.in);
System. out.println ("Enter number of terms");
n = Sc.nextInt();
}
void generate()
{
System. out.print(x+","+y);
for(int i=0; i<=n-2; i++)
{
System.out.print(","+z);
x=y;
y=z;
z=y+x;

}
}
static void main()
{
FiboString obj=new FiboString();
obj.accept();
obj.generate();
}
}
Output:
Enter number of terms
6
a
b
ba
bab
babba
babbabab

Variable List:

Variable Name Data Type Description

x int Stores first term

y int Stores second term

z int Stores third term

n int Stores
terms the number of

15.Tokenizer
Write a program to accept a sentence and display each word in the
sentence along with the number of vowels in that word.
Algorithm:
Start
Step 1:String Line
Step 2:Sentence() (rep. 2 to 3)
Step 3:input line
Step 4:void c() (rep. 4 to 19)
Step 5:int startIndex=0
Step 6:int lastInd=0
Step 7:while(lastInd<line.length()) (rep. 7 to 19)
Step 8:if (line.charAt(lastInd)==' '||lastInd==line.length()-1)
Step 9:if (lastInd== line.length()-1)
Step 10:lastInd++;
Step 11:String word = line.substring(startIndex,lastInd);
Step 12:int vowelCount = 0;
Step 13:for (char ch : word.toLowerCase().toCharArray())
Step 14:if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
Step 15:vowelCount++
Step 16:System.out.println("No of vowels in " + word + "-" + vowelCount)
Step 18:startIndex=lastInd+1
Step 19:lastInd++
Step 20: void main() (rep. 21 to 23)
Step 21:Sentence obj = new Sentence()
Step 22:obj.input()
Step 23:obj.count()
Stop
Code:
import java.util.Scanner;
class Sentence
{
String line;
Sentence()
{
line="";
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
line = sc.nextLine();
}
void count()
{
int startIndex=0;
int lastInd=0;
while (lastInd<line.length())
{
if (line.charAt(lastInd)==' '||lastInd==line.length()-1)
{
if (lastInd== line.length()-1)
{
lastInd++;
}
String word = line.substring(startIndex,lastInd);
int vowelCount = 0;
for (char ch : word.toLowerCase().toCharArray())
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowelCount++;
}
}
System.out.println("No of vowels in " + word + "-" + vowelCount);
startIndex=lastInd+1;
}
lastInd++;
}
}
public static void main()
{
Sentence obj = new Sentence();
obj.input();
obj.count();
} }
Output:
Enter a sentence:
Kolkata is my city
No of vowels in Kolkata-3
No of vowels in is-1
No of vowels in my-0
No of vowels in city-1
Variable List:

Variable Name Data Type Description

m int Stores number of rows

n int Stores number of columns

arr[][] int Stores array elements

newArr int Stores array elements

16.Mirror image
Algorithm:
Start
Step 1: public class MirrorImage [line 3-46]
Step 2: public static void main(String args[]) [line 5-45]
Step 3: Scanner in = new Scanner(System.in)
Step 4: System.out.print("Enter number of rows (m): ")
Step 5: int m = in.nextInt()
Step 6: System.out.print("Enter number of columns (n): ")
Step 7: int n = in.nextInt()
Step 8: int arr[][] = new int[m][n]
Step 9: int newArr[][] = new int[m][n]
Step 10: System.out.println("Enter array elements")
Step 11: for (int i = 0; i < m; i++) [step 11-19]
Step 12: System.out.println("Enter Row "+ (i+1) + " :")
Step 13: for (int j = 0; j < n; j++) [step 13-18]
Step 14: arr[i][j] = in.nextInt()
Step 15: System.out.println("Input Array:")
Step 16: for (int i = 0; i < m; i++) [step 16-23]
Step 17: for (int j = 0; j < n; j++) [step 17-22]
Step 18: System.out.print(arr[i][j] + "\t")
Step 19: System.out.println()
Step 20: for (int j = 0; j < n; j++) [step 20-35]
Step 21: for (int i = 0; i < m; i++) [step 21-34]
Step 22: newArr[i][n - 1 - j] = arr[i][j]
Step 23: System.out.println("Mirror Image Array:")
Step 24: for (int i = 0; i < m; i++) [step 24-31]
Step 25: for (int j = 0; j < n; j++) [step 25-30]
Step 26: System.out.print(newArr[i][j] + "\t")
Step 27: System.out.println()
Stop
Code:
import java.util.Scanner;

public class MirrorImage


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of rows (m): ");
int m = in.nextInt();
System.out.print("Enter number of columns (n): ");
int n = in.nextInt();
int arr[][] = new int[m][n];
int newArr[][] = new int[m][n];

System.out.println("Enter array elements");


for (int i = 0; i < m; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("Input Array:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

for (int j = 0; j < n; j++) {


for (int i = 0; i < m; i++) {
newArr[i][n - 1 - j] = arr[i][j];
}
}
System.out.println("Mirror Image Array:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(newArr[i][j] + "\t");
}
System.out.println();
}
}
}
Output:
Enter number of rows (m): 4
Enter number of columns (n): 4
Enter array elements
Enter Row 1 :
1
2
3
4
Enter Row 2 :
5
6
7
8
Enter Row 3 :
9
10
11
12
Enter Row 4 :
13
14
15
16
Input Array:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Mirror Image Array:
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13

Variable List:

Variable Name Data Type Description

arr[] String Array

i int Loop Variable

m int Accepts number of rows

n int Accepts number of columns

17.Word
Algorithm
:
Start
Step 1: class Word [Define class Word]
Step 2: Declare private variable line (String)
Step 3: Define default constructor Word()
Step 4: Initialize line to an empty string
Step 5: Define method input()
Step 6: Create a Scanner object scanner
Step 7: Print "Enter a word: "
Step 8: Take input into line using scanner.nextLine()
Step 9: Define method compare(Word w1)
Step 10: If this.line equals w1.line
Step 11: Print "Both words are the same."
Step 12: Else
Step 13: Print "The words are not the same."
Step 14: public class Main [Define class Main]
Step 15: Define main() method
Step 16: Create objects word1 and word2 of class Word
Step 17: Print "Enter details for Word 1:"
Step 18: Call method input() on object word1
Step 19: Print "Enter details for Word 2:"
Step 20: Call method input() on object word2
Step 21: Call method compare(word2) on object word1
Stop

Code:
import java.util.Scanner;
class Word {
private String line;

public Word() {
line = "";
}
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
line = scanner.nextLine();
}

public void compare(Word w1) {


if (this.line.equals(w1.line)) {
System.out.println("Both words are the same.");
} else {
System.out.println("The words are not the same.");
}
}
}
public class Main
{
public static void main(String[] args)
{
Word word1 = new Word();
Word word2 = new Word();

System.out.println("Enter details for Word 1:");


word1.input();

System.out.println("Enter details for Word 2:");


word2.input();

word1.compare(word2);
}
}
Output:
Enter details for Word 1:
Enter a word: Hello

Enter details for Word 2:


Enter a word: World

The words are not the same.

Variable List:

Variable Name Data Type Description

line String string Accepts a

Word1 String word Checks first


Word2 String word Checks second

18
.Number
Algorithm:
Start
Step 1: class number [line 3-49]
Step 2: private int n
Step 3: public number() [line 5-7]
Step 4: n = 0
Step 5: public void input() [line 9-16]
Step 6: Scanner scanner = new Scanner(System.in)
Step 7: System.out.print("Enter a number: ")
Step 8: n = scanner.nextInt()
Step 9: public void concat_prime(number n1) [line 18-44]
Step 10: int num1 = this.n
Step 11: int num2 = n1.n
Step 12: int combinedNumber = 0
Step 13: while (num1 > 0 || num2 > 0) [step 13-41]
Step 14: int digit1 = num1 % 10
Step 15: int digit2 = num2 % 10
Step 16: if (isPrimeDigit(digit1)) [step 16-17]
Step 17: combinedNumber = combinedNumber * 10 + digit1
Step 18: if (isPrimeDigit(digit2)) [step 18-19]
Step 19: combinedNumber = combinedNumber * 10 + digit2
Step 20: num1 /= 10
Step 21: num2 /= 10
Step 22: System.out.println("New Number: " + combinedNumber)
Step 23: private boolean isPrimeDigit(int digit) [line 23-35]
Step 24: if (digit < 2) [step 24-25]
Step 25: return false
Step 26: for (int i = 2; i <= Math.sqrt(digit); i++) [step 26-33]
Step 27: if (digit % i == 0) [step 27-28]
Step 28: return false
Step 29: return true
Step 30: public class Main [line 37-48]
Step 31: public static void main(String[] args) [line 39-47]
Step 32: number num1 = new number()
Step 33: number num2 = new number()
Step 34: System.out.println("Enter details for Number 1:")
Step 35: num1.input()
Step 36: System.out.println("Enter details for Number 2:")
Step 37: num2.input()
Step 38: num1.concat_prime(num2)
Stop
Code:
import java.util.Scanner;
class number
{
private int n;
public number()
{
n = 0;
}
public void input()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
n = scanner.nextInt();
}
public void concat_prime(number n1)
{
int num1 = this.n;
int num2 = n1.n;
int combinedNumber = 0;
while (num1 > 0 || num2 > 0)
{
int digit1 = num1 % 10;
int digit2 = num2 % 10;
if (isPrimeDigit(digit1))
{
combinedNumber = combinedNumber * 10 + digit1;
}
if (isPrimeDigit(digit2))
{
combinedNumber = combinedNumber * 10 + digit2;
}
num1 /= 10;
num2 /= 10;
}
System.out.println("New Number: " + combinedNumber);
}
private boolean isPrimeDigit(int digit)
{
if (digit < 2)
{
return false;
}
for (int i = 2; i <= Math.sqrt(digit); i++)
{
if (digit % i == 0)
{
return false;
}
}
return true;
}
}
public class Main
{
public static void main(String[] args)
{
number num1 = new number();
number num2 = new number();
System.out.println("Enter details for Number 1:");
num1.input();
System.out.println("Enter details for Number 2:");
num2.input();
num1.concat_prime(num2);
}
}
Output:
Enter details for Number 1:
Enter a number: 12345

Enter details for Number 2:


Enter a number: 67890

New Number: 3579


Variable List:

Variable Name Data Type Description

n int Inputs number from user

num1 int Stores part of number to be


combined

num2 int Stores part of number to be


combined

digit1 int Stores the number divided by 10


digit2 int Stores the number divided by 10

combinedNumber int Stores the final combined


number

19.Rec2
Algorithm:
Start
Step 1: class rec2 [line 1-14]
Step 2: public void main() [line 3-12]
Step 3: int number, result
Step 4: number = 5698
Step 5: result = sum(number)
Step 6: System.out.println("sum = %d" + result)
Step 7: int sum(int n) [line 14-18]
Step 8: if (n != 0) [step 8-10]
Step 9: return n + sum(n/10)
Step 10: else [step 10-11]
Step 11: return n
Step 12: Stop
Code:
class rec2
{
public void main()
{
int number, result;
number=5698;
result=sum(number);
System.out.println("sum="+ result);
}
int sum(int n)
{
if (n != 0)
return n + sum(n/10);
else
return n;
}
}
Output:
Sum=6382
Variable List:

Variable Name Data Type Description

number int Accept the number


from user

result int Stores the result

sum int Stores the sum

20. A class SeriesSum is designed to calculate the sum of the following


series
Sum = x2/1! + x4/3! + x6/5! + ...+ x2n/(2n-1)!
Some of the members of the class are given below.
Class Name:SeriesSum
Data members/instance variables:
x: to store an integer number
n: to store number of terms
Member Functions:
SeriesSum(int xx,int nn) : constructor to assign x = xx and n = nn
double findFact(int m) : to return the factorial of m
double findPower(int x, int y): to return x raised to the power y
void calculate() : to calculate the sum of the series
void display(): to display the sum of the series

Algorithm:
Function Input
Start
Step 1 : Input n
Step 2 : Input x
Stop

Code:
import java.util.Scanner;
public class SeriesSum
{
private int x;
private int n;
private double s;
public SeriesSum(int xx,int nn)
{
x = xx;
n = nn;
s = 0;
}
double findFact(int m)
{
double f = 1;
for(int i = 1; i <= m; i++)
{
f = f * i;
}
return f;
}
double findPower(int x, int y)
{
double p = 1;
for(int i = 0; i < y; i++)
{
p = p * x;
}
return p;
}
void calculate()
{
s = 0;
for(int i = 0; i < n; i++)
{
s = s + findPower(x, 2*i)/findFact(2*i - 1);
}
}
void display()
{
System.out.println("Sum of the series is " + s);
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of x");
int xx = sc.nextInt();
System.out.println("Enter value of n");
int nn = sc.nextInt();

SeriesSum ss = new SeriesSum(xx,nn);


ss.calculate();
ss.display();
}
}
Output:
Enter value of x
2
Enter value of n
3
Sum of the series is 7.666666666666666

Variable List:

Variable Name Data Type Description

s double Stores sum

x int Stores value of x

n int Stores number of terms


1.Accept a name from the user and check whether it is
there in a particular list or not.
How many names start with a vowel?
How many of them have a middle name?

Algorithm
Start
Step 1: FileWriter fw = new
FileWriter("C:\\picnic.txt")
Step 2: BufferedWriter bw = new BufferedWriter(fw)
Step 3: PrintWriter pw = new PrintWriter(bw)
Step 4: Scanner sc = new Scanner(System.in)
Step 5: Print "Enter no. of students"
Step 6: int n = sc.nextInt()
Step 7: Print "Enter names of students"
Step 8: For i = 0 to n (Rep: 9-10)
Step 9: String name = sc.nextLine()
Step 10: pw.println(name)
Step 11: pw.close(), bw.close(), fw.close(),
sc.close()
Step 12: StringTokenizer st = new StringTokenizer("")
Step 13: FileReader fr = new
FileReader("C:\\picnic.txt")
Step 14: BufferedReader br = new BufferedReader(fr)
Step 15: Scanner s1 = new Scanner(new
File("C:\\picnic.txt"))
Step 16: int m = 0, int v = 0
Step 17: While s1.hasNext() (Rep: 18-23)
Step 18: String x = s1.nextLine().trim()
Step 19: If x.length() > 0 (Rep: 20-23)
Step 20: char y = x.charAt(0)
Step 21: If y is a vowel, v++
Step 22: Count spaces in x
Step 23: If spaceCount > 1, m++
Step 24: Print m (middle names)
Step 25: Print v (vowel names)
Stop

Code:
import java.util.*;
import java.io.*;
class Picnic
{
public static void main()throws IOException
{

FileWriter fw = new
FileWriter("C:\\picnic.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of students");
int n = sc.nextInt();
System.out.println("Enter names of Students");
for (int i = 0; i <=n; i++)
{
String name = sc.nextLine();
pw.println(name);
}
pw.close();
sr.enter();
bw.close();
fw.close();
sc.close();
StringTokenizer st= new StringTokenizer("");
FileReader fr=new
FileReader("C:\\picnic.txt");
BufferedReader br=new BufferedReader(fr);
Scanner s1=new Scanner(new
File("C:\\picnic.txt"));
int m=0;
int v=0;
int s=0;
while(s1.hasNext())
{
String x = s1.nextLine().trim();
if (x.length() > 0)
{
char y = x.charAt(0);
if (y == 'a' || y == 'A' || y == 'e' ||
y == 'E' || y == 'I' || y == 'i' || y == 'O' || y ==
'o' || y == 'U' || y == 'u')
{
v++;
} int spaceCount = 0;

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


{
if (x.charAt(i) == ' ')
{
spaceCount++;
}
}
if (spaceCount > 1)
{
m++;
}
}
}
System.out.println("No. of names having a
middle name:"+m);
System.out.println("No. of names starting with
a vowel:"+v);
}
}
Name Type Description

n int Accepts no. of


students

name String Accepts names of


students

x String
Shortened string

y char Checks for vowels

v int Vowel counter

SpaceCount int Space counter

m int Middle name counter

2. Class Stud

Start
Step 1: InputStreamReader st = new
InputStreamReader(System.in)
Step 2: BufferedReader br = new BufferedReader(st)
Step 3: Declare variables: int roll, String name, int
cls, char sec
Step 4: void input_data() throws IOException (Rep:
5-8)
Step 5: roll = Integer.parseInt(br.readLine())
Step 6: name = br.readLine()
Step 7: cls = Integer.parseInt(br.readLine())
Step 8: sec = (char) br.read()
Step 9: void display() (Rep: 10-13)
Step 10: Print roll as "Roll no: roll"
Step 11: Print name as "Name: name"
Step 12: Print cls as "Class: cls"
Step 13: Print sec as "Section: sec"
Stop

Code:
import java.io.*;
class Stud
{
InputStreamReader st=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(st);
int roll;
String name;
int cls;
char sec;
void input_data() throws IOException
{
roll=Integer.parseInt(br.readLine());
name=br.readLine();
cls=Integer.parseInt(br.readLine());
sec=(char)br.read();
}
void display()
{
System.out.println("Roll no:"+roll);
System.out.println("Name:"+name);
System.out.println("Class:"+cls);
System.out.println("Section:"+sec);
}
}

Name Type Description


sec char Section

roll int Roll no

name String Name of student(s)

cls int Class

3.
Algorithm

Code:
import java.io.*;
class StudWr
{
InputStreamReader st= new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(st);
int roll;
String name;
int cls;
String sec;
void input_data() throws IOException
{
roll=Integer.parseInt(br.readLine());
name=br.readLine();
cls=Integer.parseInt(br. readLine());
sec=br.readLine();
}
void display()
{
System.out.println("Roll no:"+roll);
System.out.println("Name:"+name);
System.out.println("Class:"+cls);
System.out.println("Section:"+sec);
}
void writedata() throws IOException
{
FileOutputStream fw= new
FileOutputStream("e:\\stud.dat");
DataOutputStream dw= new DataOutputStream(fw);
input_data();
dw.writeInt(roll);
dw.writeUTF(name);
dw.writeInt(cls);
dw.writeUTF(sec);
fw.close();
}
public static void main() throws IOException
{
StudWr ob=new StudWr();
ob.writedata();
}
}

Name Type Description

sec char Section

roll int Roll no

name String Name of student(s)

cls int Class

4.
Algorithm
Step 1: class Plane
Step 2: Declare variables: double x1, double y1
Step 3: void input() (Rep: 4-7)
Step 4: Create Scanner sc = new Scanner(System.in)
Step 5: Print "Enter x1 and y2 coordinate"
Step 6: x1 = sc.nextDouble()
Step 7: y1 = sc.nextDouble()
Step 8: void show() (Rep: 9-12)
Step 9: Print "x1 coordinate is: x1"
Step 10: Print "y1 coordinate is: y1"
Step 11: class Circle extends Plane
Step 12: Declare variables: double x2, double y2,
double radius, double area
Step 13: void input() (Rep: 14-17)
Step 14: Create Scanner sc = new Scanner(System.in)
Step 15: Call super.input()
Step 16: Print "Enter x2 and y2 coordinate"
Step 17: x2 = sc.nextDouble()
Step 18: y2 = sc.nextDouble()
Step 19: void findRadius() (Rep: 20-21)
Step 20: radius = (Math.sqrt((Math.pow((x2-x1),2) +
Math.pow((y2-y1),2)))) / 2.0
Step 21: void findArea() (Rep: 22-23)
Step 22: area = 3.14 * radius * radius
Step 23: void show() (Rep: 24-27)
Step 24: Print "Radius of the circle is: radius"
Step 25: Print "Area of the circle is: area"
Stop

Code:
import java.util.*;
class Plane
{
double x1,y1;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter x1 and y2
coordinate");
x1=sc.nextDouble();
y1=sc.nextDouble();
}
void show()
{
System.out.println("x1 coordinate is: "+x1);
System.out.println("y1 coordinate is: "+y1);
}
}

class Circle extends Plane


{
double x2,y2,radius,area;
void input()
{
Scanner sc = new Scanner(System.in);
super.input();
System.out.println("Enter x2 and y2
coordinate");
x2=sc.nextDouble();
y2=sc.nextDouble();
}
void findRadius()
{
radius =
(Math.sqrt((Math.pow((x2-x1),2)+Math.pow((y2-y1),2))))
/2.0;
}
void findArea()
{
area = 3.14*radius*radius;
}
void show()
{
System.out.println("Radius of the circle is:
"+radius);
System.out.println("Area of the circle is:
"+area);
}
}

Name Type Description

x1 double 1st x coordinate

y1 double 1st y coordinate

x2 double 2nd x coordinate

y2 double 2nd y coordinate

radius double Radius of circle

area double Area of circle

5.
Algorithm
Step 1: class Product
Step 2: Declare variables: String name, int code,
double amount
Step 3: Product(String n, int c, double p) (Rep: 4-6)
Step 4: Initialize name = n
Step 5: Initialize code = c
Step 6: Initialize amount = p
Step 7: void show() (Rep: 8-11)
Step 8: Print "Product name: name"
Step 9: Print "Product Code: code"
Step 10: Print "Amount: amount"
Step 11: public class Sales extends Product
Step 12: Declare variables: int day, double tax,
double totamt
Step 13: Sales(String name, int code, double amt, int
d, double tx, double tamt) (Rep: 14-17)
Step 14: Call super(name, code, amt)
Step 15: Initialize day = d
Step 16: Initialize tax = tx
Step 17: Initialize totamt = tamt
Step 18: void compute() (Rep: 19-22)
Step 19: tax = 0.124 * amount
Step 20: Declare double fine = 0.0
Step 21: If day > 30, fine = (day - 30) * 0.025 *
amount
Step 22: totamt = amount + fine + tax
Step 23: void show() (Rep: 24-27)
Step 24: Call super.show()
Step 25: Print "Total amount: totamt"
Step 26: class Executor
Step 27: void main() (Rep: 28-35)
Step 28: Create Scanner sc = new Scanner(System.in)
Step 29: Print "Enter Product name: "
Step 30: String name = sc.nextLine()
Step 31: Print "Enter Product Code: "
Step 32: int code = sc.nextInt()
Step 33: Print "Enter Amount: "
Step 34: double amt = sc.nextDouble()
Step 35: Print "Enter no. of days: "
Step 36: int days = sc.nextInt()
Step 37: Create Sales obj = new Sales(name, code, amt,
days, 0, 0)
Step 38: Call obj.compute()
Step 39: Call obj.show()
Stop
Code:
class Product
{
String name;
int code;
double amount;
Product(String n, int c,double p)
{
name = n;
code = c;
amount = p;

}
void show()
{
System.out.println("Product name: "+name);
System.out.println("Product Code: "+code);
System.out.println("Amount: "+amount);
}
} //parent class

public class Sales extends Product


{
int day;
double tax, totamt;
Sales(String name, int code, double amt, int d,
double tx, double tamt)
{
super(name,code,amt);
day = d;
tax = tx;
totamt = tamt;
}
void compute()
{
tax = 0.124*amount;
double fine = 0.0;
if(day>30)
fine = (day-30)*0.025*amount;
totamt = amount+fine+tax;
}
void show()
{
super.show();
System.out.println("Total amount: "+totamt);
}
}//child class

Executor class

import java.util.*;import java.util.*;


class Executor
{
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Product name: ");
String name = sc.nextLine();
System.out.println("Enter Product Code: ");
int code = sc.nextInt();
System.out.println("Enter Amount: ");
double amt = sc.nextDouble();
System.out.println("Enter no.of days: ");
int days = sc.nextInt();
Sales obj = new Sales(name,code,amt,days,0,0);
obj.compute();
obj.show();
}
}//executor class
Name Type Description

name String product name

code int Product code

amount double Product amount

n String Variable to
initialise name

c int Variable to
initialise code

p double Variable to
initialise amount

day int Accepts no. of days

tax double Tax on the product

totamt double amount including


tax and fine

d int Variable to
initialise days

tx double Variable to
initialise tax

tamt double Variable to


initialise total
amount

fine double Fine of delay


6. Index of program: Design a superclass employee and
a subclass salary to accept all the necessary details
and calculate the hra,ta and gross of the employee
Algorithm:

CODE:
class Employee
{
int eno;
int basic;
Employee(int n,int m)
{
eno=n;
basic=m;
}
public void display()
{
System.out.println("Employee number: "+eno);
System.out.println("Basic pay: "+basic);

}
}

import java.util.*;
class salary extends Employee
{
int ta,hra,gross;
salary(int n,int m)
{
super(n,m);
this.ta=0;
this.hra=0;
this.gross=0;
}
public void cal_salary()
{
ta=(20/100)* (super.basic);
hra=(30/100)* (super.basic);
gross=super.basic+ta+hra;
}
public void display()
{
super.display();
System.out.println("ta=" + ta);
System.out.println("hra=" + hra);
System.out.println("gross=" + gross);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter employee number");
int nn=sc.nextInt();
System.out.println("Enter basic pay");
int bb=sc.nextInt();
salary obj = new salary(nn,bb);
obj.cal_salary();
obj.display();
}
}

Variable List
7.Index of program:A superclass Record contains names
and marks of the students in two different single
dimensional arrays.
Define a subclass Highest to display the names of the
students obtaining the highest mark
Algorithm:

Code:
import java.util.Scanner;
class Record{
String n[];
int m[];
int size;
public Record(int cap){
size = cap;
n = new String[size];
m = new int[size];
}
public void readarray(){
Scanner in = new Scanner(System.in);
for(int i = 0; i < size; i++){
System.out.print("Student name: ");
n[i] = in.nextLine();
System.out.print("Marks scored: ");
m[i] = Integer.parseInt(in.nextLine());
}
}
public void display(){
for(int i = 0; i < size; i++)
System.out.println(n[i] + " score = " +
m[i]);
}
}
class Highest extends Record{
int ind;
public Highest(int cap){
super(cap);
ind = 0;
}
public void find(){
for(int i = 1; i < size; i++){
if(m[i] > m[ind])
ind = i;
}
}
public void display(){
super.display();
System.out.println("Students with highest
mark:");
for(int i = 0; i < size; i++){
if(m[i] == m[ind])
System.out.println(n[i] + " = " +
m[i]);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("How many students? ");
int s = Integer.parseInt(in.nextLine());
Highest obj = new Highest(s);
obj.readarray();
obj.find();
obj.display();
}
}
Variable
list

8.
Algorithm
Start
Step 1: class sort_bubble
Step 2: Declare Scanner sc = new Scanner(System.in)
Step 3: Declare variable int n
Step 4: void input() (Rep: 5-8)
Step 5: Print "Enter number"
Step 6: n = sc.nextInt()
Step 7: int rt() (Rep: 9-10)
Step 8: Return n
Step 9: void pass(int v) (Rep: 11-12)
Step 10: n = v
Step 11: public static void main() (Rep: 13-22)
Step 12: Create array sort_bubble ob[] = new
sort_bubble[5]
Step 13: For i = 0 to 4 (Rep: 14-15)
Step 14: ob[i] = new sort_bubble()
Step 15: For i = 0 to 4 (Rep: 16-17)
Step 16: Call ob[i].input()
Step 17: Declare int t
Step 18: For i = 0 to 4 (Rep: 19-21)
Step 19: For j = 0 to 4 - 1 - i (Rep: 20)
Step 20: If ob[j].rt() > ob[j + 1].rt() (Rep: 21)
Step 21: t = ob[j].rt()
Step 22: ob[j].pass(ob[j + 1].rt())
Step 23: ob[j + 1].pass(t)
Step 24: Print "Sorted array"
Step 25: For i = 0 to 4 (Rep: 26)
Step 26: Print ob[i].rt()
Stop

Code:
import java.util.*;
class sort_bubble
{ Scanner sc=new Scanner(System.in);
int n;
void input()
{
System.out.println("Enter number");
n=sc.nextInt();
}

int rt()
{
return n;
}

void pass(int v)
{
n=v;
}

public static void main()


{

sort_bubble ob[]=new sort_bubble[5];


for(int i=0;i<5;i++)
{
ob[i]=new sort_bubble();
}

for(int i=0;i<5;i++)
{
ob[i].input();
}
int t;
for(int i=0;i<5;i++)
{
for(int j=0;j<5-1-i;j++)
{
if(ob[j].rt()>ob[j+1].rt())
{
t=ob[j].rt();
ob[j].pass(ob[j+1].rt());
ob[j+1].pass(t);
}
}
}
System.out.println("Sorted array");
for(int i=0;i<5;i++)
{
System.out.println(ob[i].rt());
}
}
}

Name Type Description

n int Accepts a number

v int Initialises value n


to v

t int Stores index from


object array

ob[] object Object array

9.
Algorithm
Start
Step 1: class sort_select
Step 2: Declare Scanner sc = new Scanner(System.in)
Step 3: Declare variable int n
Step 4: void input() (Rep: 5-8)
Step 5: Print "Enter number"
Step 6: n = sc.nextInt()
Step 7: int rt() (Rep: 9-10)
Step 8: Return n
Step 9: void pass(int v) (Rep: 11-12)
Step 10: n = v
Step 11: public static void main() (Rep: 13-22)
Step 12: Create array sort_select ob[] = new
sort_select[5]
Step 13: For i = 0 to 4 (Rep: 14-15)
Step 14: ob[i] = new sort_select()
Step 15: For i = 0 to 4 (Rep: 16-17)
Step 16: Call ob[i].input()
Step 17: For i = 0 to 4 (Rep: 18-21)
Step 18: Initialize int s = ob[i].rt()
Step 19: Initialize int p = i
Step 20: For j = i to 4 (Rep: 21)
Step 21: If ob[j].rt() < s (Rep: 22)
Step 22: s = ob[j].rt()
Step 23: p = j
Step 24: Initialize int t = ob[i].rt()
Step 25: ob[i].pass(s)
Step 26: ob[p].pass(t)
Step 27: Print "Sorted array"
Step 28: For i = 0 to 4 (Rep: 29)
Step 29: Print ob[i].rt()
Stop

Code:
import java.util.*;
class sort_select
{ Scanner sc=new Scanner(System.in);
int n;
void input()
{
System.out.println("Enter number");
n=sc.nextInt();
}

int rt()
{
return n;
}

void pass(int v)
{
n=v;
}

public static void main()


{
sort_select ob[]=new sort_select[5];
for(int i=0;i<5;i++)
{
ob[i]=new sort_select();
}

for(int i=0;i<5;i++)
{
ob[i].input();
}

for(int i=0;i<5;i++)
{
int s=ob[i].rt();
int p=i;
for(int j=i;j<5;j++)
{
if(ob[j].rt()<s)
{
s=ob[j].rt();
p=j;
}
}
int t=ob[i].rt();
ob[i].pass(s);
ob[p].pass(t);
}
System.out.println("Sorted array");
for(int i=0;i<5;i++)
{
System.out.println(ob[i].rt());
}
}
}

Name Type Description

n int Accepts a number

v int Initialises value n


to v

t int Stores index from


object array

s int

p int

j int

ob[] object Object array

10.
Algorithm
Start
Step 1: class sort_insert
Step 2: Declare Scanner sc = new Scanner(System.in)
Step 3: Declare variable int n
Step 4: void input() (Rep: 5-8)
Step 5: Print "Enter number"
Step 6: n = sc.nextInt()
Step 7: int rt() (Rep: 9-10)
Step 8: Return n
Step 9: void pass(int v) (Rep: 11-12)
Step 10: n = v
Step 11: public static void main() (Rep: 13-23)
Step 12: Create array sort_insert ob[] = new
sort_insert[5]
Step 13: For i = 0 to 4 (Rep: 14-15)
Step 14: ob[i] = new sort_insert()
Step 15: For i = 0 to 4 (Rep: 16-17)
Step 16: Call ob[i].input()
Step 17: Initialize int p = 0
Step 18: For i = 0 to 4 (Rep: 18-22)
Step 19: Initialize int s = ob[i].rt()
Step 20: Initialize p = i - 1
Step 21: While p >= 0 and s <= ob[p].rt() (Rep: 21)
Step 22: ob[p + 1].pass(ob[p].rt())
Step 23: p--
Step 24: ob[p + 1].pass(s)
Step 25: Print "Sorted array"
Step 26: For i = 0 to 4 (Rep: 27)
Step 27: Print ob[i].rt()
Stop

Code:
import java.util.*;
class sort_insert
{ Scanner sc=new Scanner(System.in);
int n;
void input()
{
System.out.println("Enter number");
n=sc.nextInt();
}

int rt()
{
return n;
}

void pass(int v)
{
n=v;
}

public static void main()


{

sort_insert ob[]=new sort_insert[5];


for(int i=0;i<5;i++)
{
ob[i]=new sort_insert();
}

for(int i=0;i<5;i++)
{
ob[i].input();
}
int p=0;
for(int i=0;i<5;i++)
{
int s=ob[i].rt();
p=i-1;
while(p>=0&&s<=ob[p].rt())
{
ob[p+1].pass(ob[p].rt());
p--;
}
ob[p+1].pass(s);
}
System.out.println("Sorted array");
for(int i=0;i<5;i++)
{
System.out.println(ob[i].rt());
}
}
}

Name Type Description

n int Accepts a number

v int Initialises value n


to v

t int Stores index from


object array

s int

p int

ob[] object Object array

11.
Assignment on Array Question 1. A class duplicate
contains an array of Integers which will not store
duplicate elements. Some of the members of the class
are given below:110) Class name: duplicate Data
Members/instance variables: arty Integer array Size:
size of the array Member functions/methods: Combine(nt
nn): parameterized constructor to assign size = nn
void Inputarrayo: accepts the array elements. void
remove() : Remove duplicate elements void display():
displays the array elements
Algorithm
Step 1: class duplicate
Step 2: Declare Scanner sc = new Scanner(System.in)
Step 3: Declare variables: int ar[], int size
Step 4: duplicate(int nn) (Rep: 5-7)
Step 5: Initialize size = nn
Step 6: Initialize ar = new int[size]
Step 7: void inputarray() (Rep: 8-11)
Step 8: Print "enter array elements"
Step 9: For i = 0 to size - 1 (Rep: 10)
Step 10: ar[i] = sc.nextInt()
Step 11: void remove() (Rep: 12-19)
Step 12: Declare int k = 0, int f = 0, int v = 0
Step 13: Declare int br[] = new int[size]
Step 14: For i = 0 to size - 1 (Rep: 15-18)
Step 15: Initialize v = ar[i]
Step 16: Initialize f = 0
Step 17: For j = 0 to k - 1 (Rep: 17)
Step 18: If br[j] == v (Rep: 18)
Step 19: Set f = 1 and break
Step 20: If f == 0 (Rep: 19)
Step 21: Set br[k++] = v
Step 22: Set ar = br
Step 23: void display() (Rep: 24-26)
Step 24: For i = 0 to size - 1 (Rep: 25)
Step 25: Print ar[i]
Stop

Code:
import java.util.*;
class duplicate
{
Scanner sc=new Scanner(System.in);
int ar[];
int size;

duplicate(int nn)
{
size=nn;
ar=new int[size];
}

void inputarray()
{
System.out.println("enter array elements");
for(int i=0;i<size;i++)
{
ar[i]=sc.nextInt();
}
}

void remove()
{
int k=0,f=0;
int v=0;
int br[]=new int[size];
for(int i=0;i<5;i++)
{
v=ar[i];
f=0;
for(int j=0;j<k;j++)
{
if(br[j]==v)
{
f=1;
break;
}
}
if(f==0)
{
br[k++]=v;
}
}
ar=br;
}

void display()
{
for(int i=0;i<size;i++)
{
System.out.println(ar[i]);
}
}
}

Name Type Description

i int Loop variable

ar int Array variable

br int Array variable

12.
Algorithm
Start
Step 1: class duplicate2
Step 2: Declare Scanner sc = new Scanner(System.in)
Step 3: Declare variable int size
Step 4: Declare int ar[] = new int[size]
Step 5: duplicate2(int nn) (Rep: 6-8)
Step 6: Initialize size = nn
Step 7: void inputarray() (Rep: 9-12)
Step 8: For i = 0 to size - 1 (Rep: 10)
Step 9: Print "Enter a number"
Step 10: ar[i] = sc.nextInt()
Step 11: void remove() (Rep: 13-22)
Step 12: Call inputarray()
Step 13: Declare int b[] = new int[size]
Step 14: For i = 0 to size - 1 (Rep: 14-19)
Step 15: Initialize int m = ar[i]
Step 16: Initialize int f = 0
Step 17: For j = 0 to size - 1 (Rep: 17)
Step 18: If b[j] == m (Rep: 18)
Step 19: Set f = 1 and break
Step 20: If f == 0 (Rep: 19)
Step 21: Set b[size++] = m
Step 22: void display() (Rep: 23-27)
Step 23: Call remove()
Step 24: For i = 0 to size - 1 (Rep: 24)
Step 25: ar[i] = b[i]
Step 26: Print ar[i]
Stop

Code:
.import java.util.*;
class duplicate2
{
Scanner sc=new Scanner(System.in);
int size;
int ar[]=new int[size];
duplicate2(int nn)
{
size=nn;
}
void inputarray()
{
for(int i=0;i<size;i++)
{
System.out.println("Enter a number");
ar[i]=sc.nextInt();
}
}
void remove()
{
inputarray();
int b[]=new int[size];
for(int i=0;i<size;i++)
{
int m=ar[i];
int f=0;
for(int j=0;j<size;j++)
{
if(b[j]==m)
{
f=1;
break;
}
}
if(f==0)
{
b[size++]=m;
}
}
}
void display()
{
remove();
for(int i=0;i<=size;i++)
{
ar[i]=b[i];
System.out.println(ar[i]);
}
}
}

Name Type Description

i int Loop variable

ar int array

b int array

13..You are given a sequence of N integers, which are


called pseudo arithmetic sequences [10) (sequences
that are in arithmetic progression). Sequence of N
integers : 2, 5, 6, 8, 9, 12 We observe that 2 + 12 =
5 i- 9 = 6 + = 14 The sum of.the above sequence can be
calculated as 14 x 3 = 42. For sequences containing an
odd number of elements the rule is to double the
middle element, for example 2, S, 7, 9, 12 = 2 + 12 =
5 + 9 = 7 + 7 .= 14. 14 x 3 = 42 [middle element =7] A
class Pseudoarithmetic determines whether a given
sequence is a pseudo-arithmetic sequence. The details
of the class are given below: Class name: Pseudo
arithmetic Data members/Instance variables: n: to
store the she of the sequence an: integer array to
store the sequence of numbers ans, flag: store the
status sum: store the sum of the sequence of numbers
r: store the sum of the two numbers Member functions:
Pseutloadthmeticn: default constructor void accept(lnt
nn): to assign nn to n and to create an Integer array.
Fill in the elements of the array boolean check():
return true If the sequence Is a pseudo arithmetic
sequence otherwise returns false Specify the class
Pseudo-arithmetic, giving the details of the
constructor(), void accept(Int) and boolean check°.
Also, define a main(} function to create an object and
call the member functions accordingly to enable the
task.

Algorithm
Start
Step 1: class pseudoarithmetic
Step 2: Declare variables: int n, a[], ans, sum,
boolean flag
Step 3: pseudoarithmetic() (Rep: 4-7)
Step 4: Initialize n = 0, ans = 0, sum = 0, flag =
false
Step 5: void accept(int nn) (Rep: 8-14)
Step 6: Initialize Scanner sc = new Scanner(System.in)
Step 7: Set n = nn
Step 8: Initialize a = new int[n]
Step 9: Print "Enter the values for the sequence"
Step 10: For i = 0 to nn - 1 (Rep: 11)
Step 11: a[i] = sc.nextInt()
Step 12: boolean check() (Rep: 15-26)
Step 13: If (n % 2) == 0 (Rep: 16)
Step 14: Set ans = (n / 2) * (a[0] + a[n - 1])
Step 15: Initialize int i, j
Step 16: For i = 0, j = n - 1 to i < j (Rep: 17-18)
Step 17: sum += a[i] + a[j]
Step 18: Increment i and decrement j
Step 19: Else (Rep: 19)
Step 20: Set ans = ((n / 2) + 1) * (a[0] + a[n - 1])
Step 21: For i = 0, j = n - 1 to i <= j (Rep: 22-23)
Step 22: sum += a[i] + a[j]
Step 23: Increment i and decrement j
Step 24: If ans == sum (Rep: 24)
Step 25: Return true
Step 26: Else
Step 27: Return false
Step 28: void main() (Rep: 27-35)
Step 29: Initialize Scanner sc = new
Scanner(System.in)
Step 30: Print "Enter size of the sequence"
Step 31: Set int nn = sc.nextInt()
Step 32: Create pseudoarithmetic obj = new
pseudoarithmetic()
Step 33: Call obj.accept(nn)
Step 34: Set flag = obj.check()
Step 35: If flag == true (Rep: 35)
Step 36: Print "It is a pseudoarithmetic sequence and
summation is " + obj.sum
Step 37: Else
Step 38: Print "It is not a pseudoarithmetic sequence"
Stop

Code:
.import java.util.*;
class pseudoarithmetic
{
int n,a[],ans,sum;
boolean flag;
pseudoarithmetic()
{
n=ans=sum=0;
flag=false;
}
void accept(int nn)
{
Scanner sc = new Scanner(System.in);
int i;
n=nn;
a = new int[n];
System.out.println("Enter the values for the
sequence");
for(i=0;i<nn;i++)
{
a[i] = sc.nextInt();
}
}
boolean check()
{
if((n%2)==0)
{
ans=(n/2)*(a[0]+a[n-1]);
int i,j;
for(i=0,j=n-1;i<j;i++,j--)
{
sum+=a[i]+a[j];
}
}
else
{
ans=((n/2)+1)*(a[0]+a[n-1]);
int i,j;
for(i=0,j=n-1;i<=j;i++,j--)
{
sum+=a[i]+a[j];
}
}

if(ans==sum)
return true;
else
return false;
}

void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of the
sequence");
int nn = sc.nextInt();
pseudoarithmetic obj = new pseudoarithmetic();
obj.accept(nn);
flag = obj.check();
if(flag==true)
System.out.println("It is a pseudoarithmetic
sequence and summation is "+obj.sum);
else
System.out.println("It is not a
pseudoarithmetic sequence");
}
}

Name Type Description

n int Takes number input

flag int Counter

sum int Does sum

14.
Algorithm
Start
Step 1: class array
Step 2: Declare int ar[] = new int[10], int top
Step 3: array() (Rep: 4-6)
Step 4: Initialize top = -1
Step 5: void push(int v) (Rep: 7-11)
Step 6: If top == 9 (Rep: 8)
Step 7: Print "Array full"
Step 8: Else (Rep: 9)
Step 9: Set ar[++top] = v
Step 10: void pop() (Rep: 12-16)
Step 11: If top == -1 (Rep: 13)
Step 12: Print "Array empty"
Step 13: Else (Rep: 14)
Step 14: Set ar[top--] = 0
Step 15: void display() (Rep: 17-20)
Step 16: For i = 0 to top - 1 (Rep: 17)
Step 17: Print ar[i]
Step 18: public static void main() (Rep: 21-34)
Step 19: Create array ob = new array()
Step 20: Initialize Scanner sc = new
Scanner(System.in)
Step 21: Declare int ch = 0
Step 22: While ch < 4 (Rep: 22)
Step 23: Print menu options (Rep: 23)
Step 24: Set ch = sc.nextInt()
Step 25: Switch ch (Rep: 25)
Step 26: Case 1 (Rep: 26)
Step 27: Print "Enter data"
Step 28: Set int n = sc.nextInt()
Step 29: Call ob.push(n)
Step 30: Case 2 (Rep: 29)
Step 31: Call ob.pop()
Step 32: Case 3 (Rep: 30)
Step 33: Call ob.display()
Stop

Code:
import java.util.*;
class array
{
int ar[]=new int[10];
int top;
array()
{
top=-1;
}
void push(int v)
{
if(top==9)
System.out.println("Array full");
else
ar[++top]=v;
}
void pop()
{
if(top==-1)
{
System.out.println("Array empty");
}
else
{
ar[top--]=0;
}
}
void display()
{
for(int i=0;i<top;i++)
{
System.out.println(ar[i]);
}
}
public static void main()
{
array ob=new array();
Scanner sc=new Scanner(System.in);
int ch=0;
while(ch<4)
{
System.out.println("1>For insert data");
System.out.println("2>For insert data");
System.out.println("3>For insert data");
System.out.println("Enter choice");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter data");
int n=sc.nextInt();
ob.push(n);
break;
case 2:
ob.pop();
break;
case 3:
ob.display();
break;
}
}
}
}

Name Type Description

i int Loop variable

top int Stores top value

n int Takes input

15.
Algorithm
Start
Step 1: class array1
Step 2: Declare int ar[] = new int[10], int top
Step 3: array1() (Rep: 4-6)
Step 4: Initialize top = -1
Step 5: void push(int v) (Rep: 7-11)
Step 6: If top == 9 (Rep: 8)
Step 7: Print "Array full"
Step 8: Else (Rep: 9)
Step 9: Set ar[++top] = v
Step 10: void pop() (Rep: 12-16)
Step 11: If top == -1 (Rep: 13)
Step 12: Print "Array empty"
Step 13: Else (Rep: 14)
Step 14: Set ar[top--] = 0
Step 15: void display() (Rep: 17-20)
Step 16: For i = 0 to top - 1 (Rep: 17)
Step 17: Print ar[i]
Step 18: public static void main() (Rep: 21-35)
Step 19: Create array1 ob = new array1()
Step 20: Initialize Scanner sc = new
Scanner(System.in)
Step 21: Declare int ch = 0
Step 22: While ch < 4 (Rep: 22)
Step 23: Print menu options (Rep: 23)
Step 24: Set ch = sc.nextInt()
Step 25: Switch ch (Rep: 25)
Step 26: Case 1 (Rep: 26)
Step 27: Print "Enter data"
Step 28: Set int n = sc.nextInt()
Step 29: Call ob.push(n)
Step 30: Case 2 (Rep: 30)
Step 31: Call ob.pop()
Step 32: Case 3 (Rep: 31)
Step 33: Call ob.display()
Step 34: Default (Rep: 33)
Step 35: Print "Wrong choice"
Stop

Code:
import java.util.*;
class array1
{
int ar[]=new int[10];
int top;
array1()
{
top=-1;
}
void push(int v)
{
if(top==9)
System.out.println("Array full");
else
ar[++top]=v;
}
void pop()
{
if(top==-1)
{
System.out.println("Array empty");
}
else
{
ar[top--]=0;
}
}
void display()
{
for(int i=0;i<top;i++)
{
System.out.println(ar[i]);
}
}
public static void main()
{
array ob=new array();
Scanner sc=new Scanner(System.in);
int ch=0;
while(ch<4)
{
System.out.println("1>For insert data");
System.out.println("2>For insert data");
System.out.println("3>For insert data");
System.out.println("Enter choice");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter data");
int n=sc.nextInt();
ob.push(n);
break;
case 2:
ob.pop();
break;
case 3:
ob.display();
Break;
default:
System.out.println("Wrong choice");

}
}
}
}
Name Type Description

i int Loop variable

ob Class Object variable

16.
class num
data member int n
function member: void input():input value of n
int getnum():return value of n
write a main function to create n number of object
array too store no. of integer values
display highest and lowest value in the array.

Algorithm
Start
Step 1: class num
Step 2: Declare int n
Step 3: void input() (Rep: 4-7)
Step 4: Create Scanner sc = new Scanner(System.in)
Step 5: Print "Enter the value of n"
Step 6: Set n = sc.nextInt()
Step 7: int getnum() (Rep: 8)
Step 8: Return n
Step 9: void main() (Rep: 9-21)
Step 10: Create Scanner sc = new Scanner(System.in)
Step 11: Print "Enter no. of values you want to enter"
Step 12: Set int n = sc.nextInt()
Step 13: Create num ob[] = new num[n]
Step 14: For i = 0 to n - 1 (Rep: 15-16)
Step 15: Set ob[i] = new num()
Step 16: Call ob[i].input()
Step 17: Set h = l = ob[0].n
Step 18: For i = 1 to n - 1 (Rep: 19-21)
Step 19: If l > ob[i].n (Rep: 20)
Step 20: Set l = ob[i].n
Step 21: If h < ob[i].n (Rep: 21)
Step 22: Set h = ob[i].n
Step 23: Print "Highest: " + h
Step 24: Print "Lowest: " + l
Stop

Code:
import java.util.*;
class num
{
int n;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n=sc.nextInt();
}
int getnum()
{
return n;
}
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of values you
want to enter");
int i,h,l,n = sc.nextInt();
num ob[] = new num[n];
for(i=0;i<n;i++)
{
ob[i] = new num();
ob[i].input();
}
h=l=ob[0].n;
for(i=1;i<n;i++)
{
if(l>ob[i].n)
l=ob[i].n;
if(h<ob[i].n)
h=ob[i].n;
}
System.out.println("Highest: "+h);
System.out.println("Lowest: "+l);
}
}

Name Type Description

i int Loop variable

h int Highest number

17.
class word
data member:string x
function member:void accept():accept
string rt_word():return the word
write main function function to store n number of
words
and count how many words more than 5 letters.

Algorithm
Start
Step 1: class sentence
Step 2: Declare String x
Step 3: void accept() (Rep: 4-7)
Step 4: Create Scanner sc = new Scanner(System.in)
Step 5: Print "Enter a word"
Step 6: Set x = sc.next()
Step 7: String rt_word() (Rep: 8)
Step 8: Return x
Step 9: void main() (Rep: 10-18)
Step 10: Create Scanner sc = new Scanner(System.in)
Step 11: Print "Enter no. of words"
Step 12: Set int n = sc.nextInt()
Step 13: Create sentence obj[] = new sentence[n]
Step 14: Set int c = 0
Step 15: For int i = 0 to n - 1 (Rep: 16-17)
Step 16: Set obj[i] = new sentence()
Step 17: Call obj[i].accept()
Step 18: If obj[i].rt_word().length() > 5 (Rep: 18)
Step 19: Increment c
Step 20: Print "No. of words with more than 5 letters
is: " + c
Stop

Code:
import java.util.*;
class sentence
{
String x;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
x=sc.next();
}
String rt_word()
{
return x;
}
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter no.of words");
int n=sc.nextInt();
int c=0;
sentence obj[] = new sentence[n];
for(int i=0;i<n;i++)
{
obj[i] = new sentence();
obj[i].accept();
if(obj[i].rt_word().length() > 5)
c++;
}
System.out.println("No. of words with more
than 5 letters is: "+c);
}
}

Name Type Description

c int No. of words

i int Loop variable

Index of program:Write a program to accept a sentence


which may be terminated by either '.', '?' or 'l'
only. The words are to be separated by a single blank
space and are in uppercase.
Perform the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-palindrome words of the sentence
into palindrome words by concatenating the word by its
reverse (excluding the last character).
Example:
The reverse of the word HELP would be LEH (omitting
the last alphabet) and by concatenating both, the new
palindrome word is HELPLEH. Thus, the word HELP
becomes HELPLEH.
Note: The words which end with repeated alphabets, for
example ABB would become ABBA and not ABBBA and XAZZZ
becomes XAZZZAX.
[Palindrome word: Spells same from either side.
Example: DAD, MADAM etc.] (c) Display the original
sentence along with the
converted sentence.
Algorithm:

Code:
import java.util.*;

public class Palindrome


{

public static boolean isPalindrome(String word) {


boolean palin = true;

int len = word.length();


for (int i = 0; i <= len / 2; i++) {
if (word.charAt(i) != word.charAt(len - 1
- i)) {
palin = false;
break;
}
}

return palin;
}

public static String makePalindrome(String word) {


int len = word.length();
char lastChar = word.charAt(len - 1);
int i = len - 1;
while (word.charAt(i) == lastChar) {
i--;
}

StringBuffer sb = new StringBuffer(word);


for (int j = i; j >= 0; j--) {
sb.append(word.charAt(j));
}

return sb.toString();
}

public static void main(String args[])


{
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr =
in.nextLine().trim().toUpperCase();
int len = ipStr.length();

char lastChar = ipStr.charAt(len - 1);


if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
}
String str = ipStr.substring(0, len - 1);

StringTokenizer st = new StringTokenizer(str);


StringBuffer sb = new StringBuffer();

while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb.append(word);
}
else {
String palinWord =
makePalindrome(word);
sb.append(palinWord);
}
sb.append(" ");
}

String convertedStr = sb.toString().trim();

System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
}
}
Variable
list

18.
Algorithm
Start
Step 1: class queue
Step 2: Declare int max = 5
Step 3: Declare int queue[] = new int[5]
Step 4: Declare int front = -1
Step 5: Declare int rear = -1
Step 6: void enqueue(int element) (Rep: 7-16)
Step 7: If front == -1 && rear == -1 (Rep: 8-10)
Step 8: Set front = 0
Step 9: Set rear = 0
Step 10: Set queue[rear] = element
Step 11: Else if (rear + 1) % max == front (Rep: 12)
Step 12: Print "OVERFLOW"
Step 13: Else (Rep: 14-16)
Step 14: Set rear = (rear + 1) % max
Step 15: Set queue[rear] = element
Step 16: void dequeue() (Rep: 17-27)
Step 17: If (front == -1) && (rear == -1) (Rep: 18)
Step 18: Print "Queue is underflow"
Step 19: Else if front == rear (Rep: 20-23)
Step 20: Print "The dequeued element is %d" +
queue[front]
Step 21: Set front = -1
Step 22: Set rear = -1
Step 23: Else (Rep: 24-27)
Step 24: Print "The dequeued element is %d" +
queue[front]
Stop

Code:
import java.util.*;
class queue
{
int max = 5;
int queue[] = new int[5];
int front = -1;
int rear = -1;
void enqueue(int element)
{
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
queue[rear] = element;
}
else if((rear+1)%max == front)
System.out.println("OVERFLOW");
else
{
rear = (rear+1)%max;
queue[rear] = element;
}
}

void dequeue()
{
if((front == -1)&&(rear == -1))
System.out.println("Queue is underflow");
else if(front == rear)
{
System.out.println("The dequeued element
is %d" + queue[front]);
front = -1;
rear = -1;
}
else
{
System.out.println("The dequeued element
is %d" + queue[front]);
}
}
}
Name Type Description

front int Store front value

rear int Store rear value

19.
Algorithm
Start
Step 1: class Link
Step 2: Declare int lnk[], max, begin, end
Step 3: Link(int mm) (Rep: 4-8)
Step 4: Set max = mm
Step 5: Initialize lnk to new int[max]
Step 6: Set begin = 0
Step 7: Set end = 0
Step 8: void addlink(int v) (Rep: 9-12)
Step 9: If end != max (Rep: 10)
Step 10: Set lnk[end++] = v
Step 11: Else (Rep: 11)
Step 12: Print "Out of size"
Step 13: int delink() (Rep: 13-18)
Step 14: If begin != end (Rep: 14)
Step 15: Return lnk[begin++]
Step 16: Else (Rep: 15)
Step 17: Print "Empty"
Step 18: Return -99
Step 19: void display() (Rep: 19-23)
Step 20: Set int i = begin
Step 21: Print "All the elements are:"
Step 22: While i < end (Rep: 22)
Step 23: Print lnk[i++]
Stop

Code:
import java.util.*;
class Link
{
int lnk[],max,begin,end;
Link(int mm)
{
max=mm;
lnk=new int[max];
begin=0;
end=0;
}
void addlink(int v)
{
if(end!=max)
{
lnk[end++]=v;
}
else
{
System.out.println("Out of size");
}
}
int delink()
{
if(begin!=end)
{
return lnk[begin++];
}
else
{
System.out.println("Empty");
return -99;
}
}
void display()
{
int i=begin;
System.out.println("All the elements are:");
while(i<end)
System.out.println(lnk[i++]);
}
}

Name Type Description

link int Array variable

20.:Register is an entity which can hold a maximum of


100 names. The register enables the user to add and
remove names from the topmost end only.

Algorithm:

Code:
import java.util.Scanner;
class Register{
String stud[];
int cap;
int top;
public Register(int max){
cap = max;
if(cap > 100)
cap = 100;
stud = new String[cap];
top = -1;
}
public void push(String n){
if(top + 1 == cap)
System.out.println("OVERFLOW");
else{
stud[++top] = n;
System.out.println(n + " PUSHED");
}
}
public String pop(){
if(top == -1)
return "$$";
return stud[top--];
}
public void display(){
if(top == -1)
System.out.println("STACK EMPTY");
else{
for(int i = 0; i <= top; i++)
System.out.println(stud[i]);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Capacity: ");
int cap = Integer.parseInt(in.nextLine());
Register obj = new Register(cap);
while(true){
System.out.println("1. PUSH NAME");
System.out.println("2. POP NAME");
System.out.println("3. DISPLAY NAME");
System.out.print("Enter your choice: ");
int choice =
Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("Name to be pushed:
");
String n =
in.nextLine().toUpperCase();
obj.push(n);
break;
case 2:
n = obj.pop();
if(n.equals("$$"))
System.out.println("UNDERFLOW");
else
System.out.println(n + " POPPED");
break;
case 3:
obj.display();
break;
default:
System.out.println("Bye!");
return;
}
}
}
}

21.Design a Circular Queue to add elements from the


front index if possible and to remove elements from
the rear,if any.

Algorithm:
Code:
import java.util.*;
Class Cqueue
{
int ele[];
int cap;
int front;
int rear;
Cqueue(int max)
{
cap=max;
front=0;
rear=0;
ele=new int[cap];
}
Void insert(int v)
{
if(front==0 && rear==0)
q[front]=v;
else if((rear+1)%cap)==rear)
System.out.println(“Queue is full”);
else
{
front=(front+1) % cap;
ele[front]=v;
}
}
int delete()
{
if(rear==0 && front==0)
{
System.out.println(“Queue is empty”);
return -999;
}
else if(rear==cap-1)
{
return ele[rear];
rear=0;
}
else
{
return(ele[rear++]);
}
}
}

Name Type Description

Front Int store front value

Rear int store rear value

ele[] int array

22.Create a stack which can accept elements and also


remove them but only from the top.The size of the
array can be input by the user.Main function need not
be written.

Algorithm
Code:

import java.util.Scanner;
Class Stack{
String st[];
int size;
int top;

Stack(int cap)
{
size = cap;
if(size > 100)
size = 100;
st = new String[size];
top = -1;
}
void pushname(String n){
if(top + 1 == size)
System.out.println("OVERFLOW");
else{
st[++top] = n;
}
}
String popname(){
if(top == -1)
return "UNDERFLOW";
return st[top--];
}
public void display(){
if(top == -1)
System.out.println("STACK EMPTY");
else{
for(int i = 0; i <= top; i++)
System.out.println(st[i]);
}
}

Name Type Description

top int Stores top value

i int Loop variable

st[] int Array variable


THANK
YOU

You might also like