Ilovepdf Merged (1) Merged
Ilovepdf Merged (1) Merged
CLASS- XII
SECTION-A
U.I.D.- 7753831
Algorithm:
Start
Step 1: input n
Step 2: i=1
Step 5: i= i+1
Stop
Code:
import java.util.*;
class factors
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:
15
Factors are:
15
Variable List:
Algorithm:
Start
Step 1:input n
Step 2:i=1,counter=0
Step 3:while i<n [repeat 4&5]
Step 5:i=i+1
Stop
Code:
import java.util.*;
class prime
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
Output
It is a prime number
Variable list
c int Counter
Algorithm:
Start
Step 1:input n
Step 2:i=1,s=0
Step 5:i=i+1
Stop
Code:
import java.util.Scanner;
class perfect
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
Output
Perfect number
Variable list
Algorithm:
Start
Step 5:i=i+1
Step 6:j=1,d=0
Step 9:j=j+1
Stop
Code:
import java.util.Scanner;
class twin_prime
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(m-n==2||n-m==2)
else
}
}
Output:
11
13
Variable list
Algorithm:
Start
Step 1:input n
Step 2:t=n
Step 3:c=0
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
Code:
import java.util.Scanner;
class Kaprekar
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
Output:
Enter a number
45
Variable list:
c int counter
Step 1:input n
Step 2:t=n
Step 3:c=0
Step 5:n=n/10
Step 6:c=c+1
Step 7:v=t*t
Code:
import java.util.Scanner;
class automorphic
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
} }
Output:
25
Automorphic number
Variable list:
c int Counter
Algorithm:
Step 1:input n
Step 2:t=n
Step 3:c=0
Step 5:t=t/10
Step 6:c=c+1
Step 7:t=n
Step 8:s=0
Step 10:d=t%10
Step 11:s=s+d^ c
Step 12:t=t/10
Step 13:c=c-1
Code:
import java.util.Scanner;
class disarium
void main()
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)
else
}
Output:
135
it is a Disarium number
Variable list:
c int counter
Algorithm:
Start
Step 1: input n
Step 2: n2=n* 2
Step 3: n3=n*3
Step 7:c=0
Step 11:if(ch==a)
Step 12:c++
Step 14:f=false
Step 15:break
Stop
Code:
import java.util.Scanner;
class Fascinating
void main()
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)
else
} }
Output:
192
it is a Fascinating number.
Variable list:
f boolean flag
c int count
Algorithm:
Step 3: temp=n
Step 6: temp=temp/10;
Step 7: primefsum=0
Step 8: f=2
Code:
import java.util.*;
class Smith
boolean isSmithnumber(int n)
int digitSum = 0;
int temp = n;
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++;
int getDigitSum(int n)
int sum = 0;
while (n > 0)
sum=sum+n%10;
n=n/10;
return sum;
void main()
{
if (isSmithnumber(num))
else
Output:
Enter a number: 22
22 is a Smith number.
Variable List:
Algorithm:
Start
Step 2: input n
Step 5:s=0
Step 7:s = s + j
Stop
Code:
import java.util.Scanner;
class Triangular
{
void display()
int n = sc.nextInt();
int i, j, s;
s = 0;
s = s + j;
if (s <= n)
Output:
Variable list:
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
Code:
import java.util.Scanner;
class Dudeney
void display()
{
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)))
else
} }
Output:
Enter a number
512
It is a Dudeney number
Variable List:
Variable Data Description
Name Type
Algorithm:
Start
Step 3: n=0,f=1
Step 5: input n
Step 7: i=1,f=1
Step 9: f=f*i
Step 10:i++
Step 12:fact()
Stop
Code:
import java.util.Scanner;
class nfactor
int n=sc.nextInt();
int i=1;
while(i<n)
if(n%i==0)
System.out.println(i);
i=i+1;
Output:
Variable List:
Algorithm:
Start
Step 2: s=0,n=0
Step 4: input n
Step 8: s=s+i
Step 10:sumfact()
Stop
Code:
import java.util.Scanner;
class num1
int n,s;
void num()
s=0;
n=0;
}
void input()
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();
} }
Output:
Variable list:
Algorithm:
Start
Step1:num2() line-2
Step2:n=1,r=1
Step4:input n
Step7:d=n%10
Step8:r=r*10+d
Step9:n=n/10
Step11:print("Reversed number"+r)
Stop
Code:
import java.util.Scanner;
class num2
int n,r;
num2()
{
n=1;
r=1;
void input()
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();
} }
Output:
Enter a number:
451
Reversed number:154
Variable List:
Algorithm:
Start
Step 1: num3()
Step 2: c=0;
Step 3: n=0;
Step 4: i=0;
Step 6: input n
Step 8: c=0;
Step 9: i=1;
Stop
Code:
import java.util.Scanner;
class num3
int n,c,i;
void num()
c=0;
n=0;
void input()
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();
} }
Output:
Variable List:
Algorithm:
Start
Step 1:int n
Step 3:n=0
Step 4:void readnum() (rep. 4 to 5)
Step 5:input n
Step 7:d=0,s=0
Step 9:d=v%10
Step 10:s=s+d
Step 11:v=v/10
Step 12:return s
Step 14:d=0,p=1
Step 16:d=v%10
Step 17:p=p*d
Step 18:v=v/10
Step 19:return p
Stop
Code:
import java.util.Scanner;
class sumproduct
int n;
sumproduct()
n=0;
void readnum()
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 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)
else
Output:
Enter a number
144
It is a sumproduct number
Variable List:
Algorithm:
Start
Step 3:input n
Step 5:int i
Step 10:rev=0
Step 12:rem=n%10
Step 13:rev=rev*10+rem
Step 14:n=n/10
Stop
Code:
import java.util.Scanner;
class Emirp
int n,rev,f;
void input()
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;
obj.n = rev;
obj.isPrime();
if (f == 0 && obj.f == 0)
{
System.out.println("The original number is prime
but the reversed number is not prime");
else
Output:
35
11
Variable List:
Algorithm:
Start
Step 3: oct=0,n=0;
Step 6: input n
Step 8: t=n,s=0;
Step 18:deci_oct();
Code:
import java.util.Scanner;
class DeciOct
int n,oct;
DeciOct()
n=0;
oct=0;
void getnum()
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();
} }
Output:
Enter a number
55
Decimal no. : 55
Variable List:
Algorithm:
Start
Step 4: x=xx;
Step 5: n=nn;
Step 7: i, f=1;
Step 8: for(i=1;i<=m;i++)
Step 9: f=f*i;
Stop
Code:
import java . util .*;
class SeriesSum1 {
x = xx;
n = nn;
double findFact(int m) {
if (m == 0) {
return 1.0;
} else {
sum += term;
void display() {
int n = 5;
series.calculate();
series.display();
Output:
Sum of the series: 1.2404667449597834E14
Variable List:
Algorithm:
Start
Step 3:input n
Step 5:l=n%10
Step 6:sum=sum+l
Step 7:pro=pro*l
Step 8:n=n/10
Stop
Code:
int sum=0,pro=1, l;
void main()
int n= sc.nextInt();
while(n>0)
l = n%10;
sum =sum+l;
pro=pro*l;
n = n/10;
else
} } }
Output:
1124
Algorithm:
Start
Stop
Code:
import java.util.*;
class Armstrong
{ int n, s;
Armstrong ()
{n=0;
s=0;
void input ()
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)
else
void main()
as.input();
as.sumcal();
as.check();
}
Output:
Enter a no:
153
No. is Amstrong
Variable List:
Algorithm:
Start
Step 2:input n
Step 11:p++
Step 12:n /= 2
Stop
Code:
import java.util.Scanner;
class EvilNumber
void main()
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++;
p++;
n /= 2;
if (count % 2 == 0)
else
} }
Output:
No. of 1's: 4
Variable List:
Algorithm:
Start
Step 10:lastInd++;
Step 14:if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
Step 15:vowelCount++
Step 18:startIndex=lastInd+1
Step 19:lastInd++
Step 22:obj.input()
Step 23:obj.count()
Stop
Code:
import java.util.Scanner;
class Sentence
String line;
Sentence()
line="";
void input()
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++;
startIndex=lastInd+1;
lastInd++;
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:
Algorithm:
Start
Step 4: c1=0,c2=0;
Stop
Code:
import java.util.*;
class Initials
void display()
System.out.println("Enter a name");
str= str.trim();
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
M.K. Gandhi
Variable List:
25.Write a program to input two numbers and then merge the two
numbers.
Algorithm:
Start
Step 3: n1=0;
Step 4: n2=0;
Step 5: mergNum=0;
Step 8: n1=sc.nextLong();
Step 9: n2=sc.nextLong();
Stop
Code:
import java.util.*;
class merger
long n1,n2,mergNum;
merger()
n1=0;
n2=0;
mergNum=0;
}
void readNum()
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("merged number:"+mergNum);
void main()
{
ob.readNum();
ob.joinNum();
ob.show();
} }
Output:
45
54
1st number: 45
2nd number 54
merged number:4554
Variable List:
Algorithm:
Start
Step 4:size=0
Step 5:sent=” “
Step 6:rev=””
Step 10:readsentence();
Step 22:exfirstlast();
Stop
Code:
import java.util.*;
class Exchange
int size;
String rev,sent;
Exchange()
size = 0;
sent = "";
rev = "";
void readsentence() {
System.out.println("Enter a sentence");
sent = sc.nextLine();
void exfirstlast()
readsentence();
if (word.length() > 1)
else
rev = rev.trim();
void display()
exfirstlast();
} }
Output:
Enter a sentence
It is a warm day
Original sentence: It is a warm day
Variable List:
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() {
s = scanner.nextLine();
ch = Character.toLowerCase(ch);
void count() {
int vowelCount = 0;
if (check(s.charAt(i))) {
vowelCount++;
void main()
ob.input();
ob.count();
}
Output:
Variable List:
Algorithm:
Start
Step 1: String s;
Step 3: s="";
Step 6: input s
Step 9: l = x.length();
Stop
Code:
class str2
String s;
str2()
s="";
void input()
System.out.println("Enter a String");
s = sc.next();
String reverse(String x)
int l = x.length();
while(l!=0)
{
rev = rev + x.charAt(l-1);
l--;
return rev;
void check()
if(rev.equals(s))
else
Output:
Variable List:
Algorithm:
Start
Step 5:for(i=0;i<4;i++)
Step 6:for(j=0;j<4;j++)
Step 7:a[i][j]=sc.nextInt()
Step 12:if(a[r][c]>max)
Step 13:max=a[r][c]
Step 19:if(a[c][r]<min)
Step 20:min=a[c][r]
Step 24:ob.input();
Stop
Code:
import java.util.*;
class matrix_maxmin
void input()
int i,j;
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()
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:
Algorithm:
Start
Step 7:a[i][j]=sc.nextInt()
Step 19:if (v % i == 0)
(Rep:20-23)
Step 21:break
Step22:if (isPrime)
(Rep:23)
Step23:print(v)
Step24:void main()
(Rep:25-27)
Step26:ob.input()
Step27:ob.check()
Stop
Code:
import java.util.*;
class MatrixPrime
void input() {
a[i][j] = sc.nextInt();
void check()
int v = a[r][c];
if (v <= 1) {
continue;
if (v % i == 0) {
isPrime = false;
break;
if (isPrime) {
System.out.println(v);
void main()
ob.input();
ob.check();
} }
Output:
9
10
11
12
13
14
15
16
11
13
Variable List:
Start
Step 8: a[i][j]=sc.nextInt();
Stop
Code:
import java.util.*;
class magic_square
int i,j;
int s1=0,s2=0,s3=0,s4=0;
void display()
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];
for(i=0;i<4;i++)
{ s2=0;
for(j=0;j<4;j++)
s2=s2+a[j][i];
for(i=0;i<4;i++)
s3=s3+a[i][i];
for(i=0;i<4;i++)
s4=s4+a[i][3-i];
else
} }
Output:
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
Variable List:
Algorithm:
Start
Step 8: for(j=0;j<3;j++)
Step 9: m1[i][j]=sc.nextInt();
Stop
Code:
import java.util.*;
class matrix_sum
int i,j,s=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
m1[i][j]=sc.nextInt();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m2[i][j]=sc.nextInt();
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:
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:
Algorithm:
Start
Step 5: m=sc.nextInt();
Step 6: n=sc.nextInt();
Stop
Code:
import java.util.*;
class saddle_DDA
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();
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;
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;
} }
Output:
The array:
1 2 3
4 5 6
7 8 9
Saddle point: 7
Variable List:
Algorithm:
Start
Step 1: i=0,j=0,flag=0
Step 2: input r
Step 3: input c
Step 6: p=r*c
Stop
Code:
import java.util.Scanner;
class eqMat
int i=0,j=0,flag=0;
System.out.println("Enter no of rows");
int r=sc.nextInt();
System.out.println("Enter no of columns");
int c=sc.nextInt();
int p=r*c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
A[i][j]=sc.nextInt();
for(i=0;i<r;i++)
for(j=0;j<c;j++)
B[i][j]=sc.nextInt();
}
}
if (A[i][j] == B[i][j])
flag++;
if(flag>0)
else
} } }
Output:
Enter no of rows
Enter no of columns
1
2
Variable List:
Algorithm:
Start
Step 1: input N
Step 2: if(N>3&&N<10)
Stop
Code:
import java.util.Scanner;
int N=scanner.nextInt();
System.out.println();
System.out.println();
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:");
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
Resultant Matrix:
@??@
?##?
?##?
@??@
Variable List:
Algorithm:
Start
Step 2: input A
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 7: for(j=1;j<N-1;j++)(Rep.14)
Step 8: sum+=A[i][j];
Stop
Code:
import java.util.*;
class Boundary
{
void display()
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:
24
Variable List:
Variable List:
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
c int counter
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:
c int Counter
c int counter
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:
f boolean flag
c int count
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:
Output:
Enter the limit:15
Triangular Numbers upto 15
1
3
6
10
15
Variable list:
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 List:
Variable List:
c int counter
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:
Output:
Sum of the series: 1.2404667449597834E14
Variable List:
f int factorial
Variable List:
Variable List:
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:
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:
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:
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:
a int matrix
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:
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:
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;
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:
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:
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
N int Accepts
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);
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:
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:
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;
}
}
Output:
Money 1: Rs200.50
Money 2: Rs200.50
Money 1 + Money 2: Rs401.0
Variable List:
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:
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]);
}
Output:
Enter hours and minutes
2
31
Enter hours and minutes
3
32
Hours=6
Minutes=3
Variable List:
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:
c int counter
i int Loop variable
Output:
Enter a number
144
It is a sumproduct number
Variable List:
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:
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:
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;
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();
}
Variable List:
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();
}
word1.compare(word2);
}
}
Output:
Enter details for Word 1:
Enter a word: Hello
Variable List:
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
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:
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();
Variable List:
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;
x String
Shortened string
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);
}
}
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();
}
}
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);
}
}
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
Executor class
n String Variable to
initialise name
c int Variable to
initialise code
p double Variable to
initialise amount
d int Variable to
initialise days
tx double Variable to
initialise tax
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;
}
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());
}
}
}
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;
}
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());
}
}
}
s int
p int
j int
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;
}
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());
}
}
}
s int
p int
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]);
}
}
}
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]);
}
}
}
ar int array
b int array
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");
}
}
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;
}
}
}
}
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
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);
}
}
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);
}
}
Code:
import java.util.*;
return palin;
}
return sb.toString();
}
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(" ");
}
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
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++]);
}
}
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;
}
}
}
}
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++]);
}
}
}
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]);
}
}