Comp Prac 12th
Comp Prac 12th
TH
1 Term 2 Questions
1. Armstrong
2. Special Number
3. Sorting Word
4. Fibonacci String
5. Array Sum
6. Evil Number
2 Practice Questions
3 Recursion
1. Recursion addition
2. Pseudo return
3. Recursion factorial
4. Number to word
5. Recursion palindrome 1
6. Recursion palindrome 2
7. Recursion remainder
8. Recursion power
9. Recursion GCD
10. Recursion binary search
11. Recursion magic number
12. Recursion decimal to
binary string
13. Recursion decimal to binary
14. Recursion guess number
15. Recursion power
16. Recursion palindrome String
17. Recursion armstrong
18. Recursion prime factorisation
19. Recursion octal to decimal
4 Inheritance
1. Question 2024 specimen
paper
2. Calculating area and volume
of a few shapes
Term 2 Paper Questions
Question 1) Armstrong
import java.util.*;
public class
SortWord
{
String
text; int
len;
SortWord
()
{
text =
null; len
= 0;
}
void readText()
{
Scanner sc = new
Scanner(System.in);
System.out.println("enter a word");
text = sc.next();
}
void sortText()
{
char a[] = new
char[text.length()]; for(int
i=0;i<a.length;i++)
a[i] = text.charAt[i];
for(int i=0;i<a.length-1;i++)
{
for(int j=0;j<a.length-1-i;i++)
{
if(a[j]>a[j+1])
{
char t =
a[j]; a[j] =
a[j+1];
a[j+1] =
t;
}
}
}
System.out.print("sorted
word = "); for(int
i=0;i<a.length;i++)
System.out.print(a[i]);
}
static void main()
{
SortWord ob = new SortWord();
ob.readText();
ob.sortText();
}
}
import java.util.*;
public class
FiboString
{
String
x;
String
y;
String
z; int
n;
FiboString()
{
x = "a";
y = "b";
z = "ba";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the number of
terms"); n = sc.nextInt();
}
void generate()
{
System.out.print(x + " ");
System.out.print(y +
" "); for(int
i=2;i<=n;i++)
{
z=y+
x; x =
y;
y = z;
System.out.print(z +
" ");
}
}
static void main()
{
FiboString ob = new FiboString();
ob.accept();
ob.generate();
}
}
import java.util.*;
public class
ArraySum
{
int s;
int m[][];
int n[][];
int k[][];
ArraySum(int
x)
{
s = x;
m = new int[s][s];
n = new int[s][s];
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter " + s*s + "
numbers"); for(int i=0;i<s;i++)
for(int j=0;j<s;j+
+) m[i][j] =
sc.next();
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
System.out.print(n[i]
[j]);
System.out.println();
}
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
System.out.print(k[i]
[j]);
System.out.println();
}
}
}
import java.util.*;
public class Evil
{
int nnum;
int bin;
Evil()
{
nnum = 0;
bin = 0;
}
void acceptNum()
{
Scanner sc = new
Scanner(System.in);
System.out.println("enter a
number"); nnum = sc.nextInt();
}
void convertbin(int x)
{
int c = 1;
while(x!=0)
{
int r = x
%2; x =
x/2; bin
+= r*c; c
= c*10;
}
}
void check()
{
convertbin(nnum
); int k = 0;
while(bin!=0)
{
int r = bin
%10; bin =
bin/10;
if(r==1)
k++;
}
if(k%2 == 0)
System.out.println(nnum + " is an evil
number"); else
System.out.println(nnum + " is not an evil number");
}
}
Practice Questions
Question 7)String Replace
import java.util.*;
public class StringReplace
{
String
n; int
s;
int
p;
int
A;
int
B;
int
C;
int
D;
int
E;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a 3 letter word and two integers greater than 1 or
10" less than
);
while(true)
{
n = sc.next(); n =
n.toUpperCase(); s =
sc.nextInt();
p = sc.nextInt();
if(n.length() == 3 && s>=1 && s<=10 && p>=1 && p<=10)
{
int k = 0;
for(int i=0;i<n.length();i++)
{
char c = n.charAt(i);
if(c == 'A' || c == 'B' || c == 'C' || c == 'D' ||
c == 'E') k++;
}
if(k ==
3)
break;
else
System.out.println("invalid input.enter again");
}
else
System.out.println("invalid input. enter again");
}
}
void change()
{
String str = "";
for(int
i=0;i<s;i++)
{
if(p>=n.length())
{
for(int j=0;j<n.length();j++)
{
if(n.charAt(j) ==
'A') str += 'B';
else if(n.charAt(j)
== 'B') str +=
"AB";
else if(n.charAt(j)
== 'C') str +=
"CD";
else if(n.charAt(j)
== 'D') str +=
"DC";
else
str += "EE";
}
}
else
{
for(int j=0;j<p;j++)
{
if(n.charAt(j) ==
'A') str += 'B';
else if(n.charAt(j)
== 'B') str +=
"AB";
else if(n.charAt(j)
== 'C') str +=
"CD";
else if(n.charAt(j)
== 'D') str +=
"DC";
else
str += "EE";
}
}
n=
str;
str =
"";
}
for(int i=0;i<n.length();i++)
{
char c =
n.charAt(i); if(c
== 'A')
A++;
else if(c ==
'B') B++;
else if(c ==
'C') C++;
else if(c ==
'D') D++;
else
E+
+;
}
}
void display()
{
System.out.println("new string = "
+ n); System.out.println("A\tB\tC\
tD\tE");
System.out.println(A + "\t" + B + "\t" + C + "\t" + D + "\t" + E);
}
static void main()
{
StringReplace ob = new
StringReplace(); Scanner sc = new
Scanner(System.in); while(true)
{
ob.input();
ob.change();
ob.display();
System.out.println("do you wish to continue? enter 1
for yes"); int a = sc.nextInt();
if(a !=
1)
break;
}
}
}
import java.io.*;
class Pseudoarithmetic
{
public int n;
public int
a[]; public
int ans;
public int
flag; public
int sum;
public int r;
Pseudoarithmetic()
{
n = 0;
flag = 0;
sum = 0;
}
}
boolean check()
{
if (n % 2 == 0)
{
int i = 0, p = n -
1; while (i < p)
{
r = a[i] + a[p];
if (r == (a[i + 1] + a[p - 1]) && (r * 3) ==
sum) { flag = 0;
}
els
e
{ flag = 1;
return
false;
}
p=p-
1; i = i
} + 1;
}
els
e
{
int i = 0, p = n -
1; while (i <= p)
{
r = a[i] + a[p];
if (r == (a[i + 1] + a[p - 1]) && (r * 3) ==
sum) { flag = 0;
}
els
e
{ flag = 1;
return
false;
}
p=p-
1; i = i
+ 1;
}
}
if (flag ==
0) return
true;
else
return false;
}
}
public static void main()
{
Pseudoarithmetic obj = new Pseudoarithmetic();
obj.accept(6);
boolean isPseudoArithmetic =
obj.check(); if (isPseudoArithmetic)
{
System.out.println("The sequence is a pseudo arithmetic sequence.");
}
else
{
System.out.println("The sequence is not a pseudo arithmetic sequence.");
}
}
Recursion
public class infinite// when a method calls itself it is called as recursive method
{
static void show()//Infinite recursion happens when base case is missing
{
System.out.println("Hello");
show(); // recursive case
}
class RecurAdd
{
static void main()
{
int a;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter any
number->"); a=sc.nextInt();
System.out.println("Summation of 1 to "+a+" = "+Add(a));
}
static int Add(int n)
{
if (n<2) //base
case return 1;
return n+ Add(n-1); //recursive case
}
/* Output tracing for static int Add(5)
* return 5 + Add(4)
* return 5 + return 4 + Add(3)
* return 5 + return 4 + return 3 + Add(2)
* return 5 + return 4 + return 3 + return 2 + Add(1)
* return 5 + return 4 + return 3 + return 2 + return 1
* return 5 + return 4 + return 3 + return 2 + 1
* return 5 + return 4 + return 3 + 3
* return 5 + return 4 + 6
* return 5 + 10
* 15
*/
}
import java.io.*;
class RecurFact
{
static void main()throws IOException
{
int a;
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.print("Enter any number->");
a=Integer.parseInt(in.readLine());
System.out.println("Factorial of "+a+" =
"+factorial1(a));
}
static int factorial(int n)
{
if (n<2)
return 1; //base case
return n*factorial(n-1); //recursive case
}
static int factorial1(int n)
{
return ((n<2)?1:n*factorial1(n-1));
}
}
void show(int d)
{
String
name[]={"zero","one","two","three","four","five","six","seven","eight",
"nine"}; for(int i=0;i<=9;i++)
if(d==i)
s=name[i]+"
"+s;
}
void display()
{
extd(n);
System.out.println(s);
}
public static void main()
{
NumWord o = new
NumWord(256); o.display();
}
}
boolean checkPalin(int n)
{
int l =
w.length();
if(n < l)
{
char c = w.charAt(n);
char c1 = w.charAt(l-
n-1);
if(c==c1)
{
checkPalin(n+1);
return(true);
}
else
return(false);
}
else
return(false);
}
Question 14)Palindrome
import java.util.*;
public class Palindrome
{
int no,n;
String
str,p="";
void input()
{
Scanner sc = new Scanner
(System.in);
System.out.print("Enter the
number: "); no=sc.nextInt();
System.out.print("Enter the
String: "); str=sc.next();
}
if(no==0)// base
case return n;
else
{
temp=no%10;
n=n*10+temp
;
return(ispalin(no/10));//recursive case
}
}
void check()
{
System.out.print('\u000c');
if(str.equalsIgnoreCase(ispalin(str)))
{
System.out.println("The word "+str+" is a Palindrome Word");
}
else
{
System.out.println("The word "+str+" is not a Palindrome Word");
}
if(no==ispalin(no))
{
System.out.println("The number "+no+" is Palindrome Number");
}
else
{
System.out.println("The number "+no+" is not Palindrome Number");
}
}
ob.input();
ob.check();
}
}
void input()
{
Scanner sc = new Scanner
(System.in);
System.out.print("Enter the
number"); n=sc.nextInt();
System.out.print("Enter the
divisor"); div=sc.nextInt();
}
int remain(int n)
{
if(div>n)
return n;
return remain(n-div);
}
class RecurRemainder
{
static int guessWhat (int p, int q)
{
if (p>=q)
{
p=p-q;
return guessWhat (p, q);
}
else
return p;
}
import
java.util.*;
class
RecurPOW
{
static long power(int x,int y)
{
if (y==0)
return 1; //base case
return x*power(x,y-1); //recursive case
}
static void main()
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number-
>"); a=sc.nextInt();
System.out.print("Its Exponent->");
b=sc.nextInt();
System.out.println(a+"^"+b+" = "+power(a,b));
}
import java.util.*;
class RecurFibo
{
static void fib(int x,int a,int b)
{
int c=a+b;
System.out.print(c+"
\t"); a=b;
b=c;
if(x<1)
return;//escape route- psuedo
return fib(x-1,a,b);
}
static void main()
{
int
a,b,l;
a=0;
b=1;
Scanner sc=new
Scanner(System.in);
System.out.println("How many
terms?"); l=sc.nextInt();
System.out.print(a+"\t"+b+"\t");
fib(l-3,0,1);
}
}
import java.io.*;
public class RecurBinarySearch
{
int arr[],l;
static BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); RecurBinarySearch(int l)
{
this.l=l;
arr=new
int[l];
}
void sort()
{
int i,j,min,pos,t;
for(i=0;i<arr.length-
1;i++)
{
min=arr[i];
pos=i;
for(j=i+1;j<arr.length;j++)
{
if(arr[j]<min)
{
min=arr[j]
; pos=j;
}
}
t=arr[i];
arr[i]=arr[pos];
arr[pos]=t;
}
}
import java.io.*;
public class
Magic
{
int s,n;
InputStreamReader in=new
InputStreamReader(System.in); BufferedReader
bf=new BufferedReader(in);
void input()throws IOException
{
System.out.print("Enter a no :");
n=Integer.parseInt(bf.readLine());
}
int isSumOneDigit(int no)
{s
=0;
while(no>0)
{
s+=no%10;
no/=10;
}
if(s<10
)
return
s; else
return (isSumOneDigit(s));
}
boolean isMagic(int no)
{
if(no==1)
return
true; else
return false;
}
public static void main()throws IOException
{
Magic m=new
Magic(); m.input();
if(m.isMagic(m.isSumOneDigit(m.n)))
System.out.println(m.n+ " is a magic
number"); else
System.out.println(m.n+ " is not a magic number");
}
}
Question 20)Recursion decimal to binary string
import java.io.*;
public class DectoBinStr
{
int n;
String
s="";
void putData()
{
System.out.println("the binary equivalent is " + s);
}
}
}
import java.io.*;
public class
DectoBin
{
int
s;
int
n;
DectoBin()
{
n=0;
s=0;
}
void getData() throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number");
n=Integer.parseInt(in.readLine());
void putData()
{
int t=n;
recursive(t);
System.out.println("the binary equivalent of " + n +" is " + s);
}
class Recurguess
{
static void main()
{
int
a=4;
int
b=3;
System.out.println(guessWhat(a,b));
}
static int guessWhat (int p, int q)
{
if (q==0)
return 0;
else if
(q==1)
return p;
else if
(q>0)
return p + guessWhat (p,q-
1); else
{
q=(int)Math.abs(q);
return (-1*(p + guessWhat(p,q-1)));
}
}
}
import java.io.*;
public class Power_Recursion
{
int a,b, n;
Power_Recursion()
{
n=1;
}
public void main()throws IOException
{
//int a,b;
Power_Recursion p=new Power_Recursion();
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.print("Enter the base->");
a=Integer.parseInt(in.readLine());
System.out.print("Enter the power-
>");
b=Integer.parseInt(in.readLine());
System.out.println("a^b="+p.power
(a));
}
public long power(int a)
{
if(n>b)
return 1;
else
{
n=n+1;
return (a*power(a));
}
}
}
else {
System.out.print("
"+test); printFun(test -
2); System.out.print("
"+ test);
}
}
if( n==sumOfPowers
(n)) return true;
else
return false;
}
import java.io.*;
public class
OctDeci
{
int octal;
int deci;
OctDeci()
{
octal=0;
deci=0;
}
void getNum() throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("Enter the number");
octal=Integer.parseInt(in.readLine());
Inheritance