Compprojectt
Compprojectt
Of
Computer Science:
Class-XI (2023-2024)
Name: ___________________________
Roll : ___________ Stream:____________
UID: _____________
1
CONTENTS
3. Write a program to accept any angle in degrees and find the sin() value using infinite series. 7-9
6. Write a program to check whether a number is a Circular Prime Number or not. 15-17
Write a program to accept any amount to pay and display all possible Combination Of Notes to
7. 18-20
pay the amount.
Write a program to accept two positive integers and generate all Unique Digit Numbers in that
8. 21-23
range and calculate their frequency.
9. Write a program to find the Union and Intersection of two sets. 24-26
10. Write a program to accept numbers in an array and sort it using Insertion Sort. 27-29
11. Write a program to check whether a number is a Prime no. Or not - using Recursion. 30-31
12. Write a program to generate reverse Fibonacci series up to N terms - using recursion. 32-33
13. Write a program to Encode and Decode a code given by the user 34-36
** Project work: A program to convert a number from one base(2-20) to another base(2-20) 37-39
2
PROGRAM 1
A library issues a book @0.2% per day on the cost price of the book. A book can be retained for 7 days without
any fine but it imposes a fine after it on the following basis for being late
ALGORITHM:
STEP 1: START.
STEP 2: „N‟ and „CP‟ are defined as class variable if int datatype.
STEP 3: Default constructor used to initialize the class variables.
ReadData()
STEP 4: This function is used to accept value from the user using Scanner class.
Calculate()
STEP 5: This function is used to calculate the total amount.The rent calculated by multiplying 0.002*OP*N
Is stored in the variable „double rent‟. A variable „int fine‟ is declared and „N‟ is substracted with 7.
STEP 6: if-else statement is used to calculate the fine based on the number of days for which the book is
delayed to be submitted.
STEP 7: The total amount to be paid is printed by adding fine and rent.
main()
STEP 8: In this function object of the class is created.
STEP 9: The above functions are called using the object.
STEP 10: STOP.
3
Program code in JAVA:
//computing Library Fine & Rent
import java.util.*;
class Library
{
int N;
int CP;
Library()
{
N= CP=0;
}
void ReadData()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Cost Of The Book: ");
CP=sc.nextInt();
System.out.print("Enter The Total Days: ");
N=sc.nextInt();
}
void Calculate()
{
double rent=0.002*CP*N;
int fine;
N=N-7;
if(N<=5)
fine=N*2;
else if(N<=10)
fine=5*2+(N-5)*3;
else
fine=(5*2)+(5*3)+(N-10)*5;
System.out.print("Total Amount To Pay: "+(CP+fine+rent));
}
static void main()
{
Library obj=new Library();
obj.ReadData();
obj.Calculate();
}
}
OUTPUT 1:
Enter The Cost Of The Book: 526
Enter The Total Days: 12
Total Amount To Pay: 548.624
OUTPUT 2:
Enter The Cost Of The Book: 680
Enter The Total Days: 20
Total Amount To Pay: 747.2
OUTPUT 3:
Enter The Cost Of The Book: 1200
Enter The Total Days: 15
Total Amount To Pay: 1255.0
4
PROGRAM 2
Write a program to accept any positive integer and generate all its Prime Factors.
e.g Input 12, Output: 2,2,3
ALGORITHM:
STEP 1: START.
STEP 2: „N‟ and „F‟ are defined as class variables of int datatype.
STEP 3: Default constructor is used to initialize the class variables.
ReadNo()
STEP 4: This function is used to accept value from the user using Scanner class.
Calculate()
STEP 5: for loop is used, where (F=2;F<=N;F++).
STEP 6: if „N‟ is divisible by „F‟ then the factor is printed and „N‟ becomes N/F.
main()
STEP 7: In this function object of the class is created.
STEP 8: The above functions are called using the object.
STEP 9: STOP.
5
Program code in JAVA:
//Prime Factors
import java.util.*;
class PrimeFactors
{
int N;
int F;
PrimeFactors()
{
N=0;
F=2;
}
void ReadNo()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter A Number: ");
N=sc.nextInt();
}
void Calculate()
{
F=2;
System.out.print("The Prime factors of "+N+ " are...........");
while(F<=N)
{
if(N%F==0)
{
System.out.print(F+", ");
N=N/F;
}
else
++F;
}
}
static void main()
{
PrimeFactors obj=new PrimeFactors();
obj.ReadNo();
obj.Calculate();
}
}
OUTPUT 1:
Enter A Number: 12
The Prime factors of 12 are...........2, 2, 3,
OUTPUT 2:
Enter A Number: 29
The Prime factors of 29 are...........29,
6
PROGRAM 3
Write a program to accept any angle in degrees and find the radian equivalent and find its sine() value using the
infinite series(value considered up to 5 decimal places)
ALGORITHM:
STEP 1: START.
STEP 2: „aid‟ is defined as class variable of double datatype.
STEP 3: Default constructor is used to in initialize the class variable.
read_angle()
STEP 4: This function is used to accept value from the user using the Scanner class.
compute()
STEP 5: Math function „Math.toRadians‟ is used to convert „aid‟ to radian and is stored in the variable
„double r‟.
STEP 6: Variables „ans‟ and „term‟ of double datatype and „k‟ and „sg „of int datatype are initialized. 0 is
stored in „ans‟ and 1 in „k‟ and „sg‟.
STEP 7: The angle in radian is printed.
STEP 8: An infinite while loop is used and Math function „Math.pow‟ is used to multiply „r‟ by the power „k‟
And is divided by the function getfact and „k‟ is passed to it.
STEP 9: „if‟ statement is used to check whether „term‟ is less than 0.00001 else the radian equivalent is
calculated and is stored in „ans‟.
STEP 10: The radian equivalent is printed.
STEP 11: The factorial of „N‟ is calculated using the while loop which goes till N<1 and the factorial is stored
in „F‟.
STEP 12: The value of „F‟ is returned.
main()
STEP 13: In this function object of the class is created.
STEP 14: The above functions are called using the object.
STEP 15: STOP.
7
Program code in JAVA:
//computing sin(x)
import java.util.*;
class Sin_x
{
double aid;
Sin_x()
{
aid=0.0;
}
void read_angle()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Any Angle In Degree:");
aid=sc.nextDouble();
}
void compute()
{
double r=Math.toRadians(aid);
double ans,term;
int k,sg;
k=sg=1;
ans=0.0;
System.out.println("In Radian The Angle Is:"+r);
while(true) //running an infinite loop
{
term=Math.pow(r,k)/getfact(k);
if(term<0.00001)
break;
ans=ans+sg*term;
sg=sg*(-1);
k=k+2;//generating odd terms
}
System.out.print("So,We Have Found sin("+(int)aid+")as:" +String.format("%7.4f",ans));
}
int getfact(int N)
{
int F=1;
while(N>1)
F=F*N--;
return F;
}
static void main()
{
Sin_x obj=new Sin_x();
obj.read_angle();
obj.compute();
}
}
8
OUTPUT 1:
Enter Any Angle In Degree: 10
In Radian The Angle Is: 0.17453292519943295
So,We Have Found sin(10)as: 0.1736
OUTPUT 2:
Enter Any Angle In Degree: 15
In Radian The Angle Is: 0.2617993877991494
So,We Have Found sin(15)as: 0.2558
OUTPUT 3:
Enter Any Angle In Degree: 20
In Radian The Angle Is: 0.3490658503988659
So,We Have Found sin(20)as: 0.3420
9
PROGRAM 4
Write A Program To Find The Series Of A Special Triangular Number.
ALGORITHM:
STEP 1: START.
main()
STEP 2: In this function the value is accepted from the user by using the Scanner class.
STEP 3: Variables „a‟, „b‟, „c‟, „s‟ of int data type and „flag‟ of boolean data type are initialized and false is
stored in „flag‟.
STEP 4: A nested „for‟ loop is used where „a‟ runs from 1 til the half of „N‟ and „b‟ runs from 9 till N/2+1 and
b is added and stored in „s‟.
STEP 5: It is checked whether „s‟ is equal to „N‟ by using „if‟ statement and true is stored in „flag‟.
STEP 6: Another „for‟ loop is used where „c‟ runs from 9 till it is less than equal to „b‟ and „c‟ is printed.
STEP 7: If „flag‟ is equal to true it is printed as a ‟Triangular Number‟ else it is printed as „Not A Triangular
Number‟.
STEP 8: STOP.
10
Program code in JAVA:
//Special Triangular No.
import java.util.*;
class SpTriangular
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Number: ");
int N=sc.nextInt();
int a,b,c,s;
boolean flag=false;
for(a=1;a<=N/2;a++)
{
s=0;
for(b=a;b<=N/2+1;b++)
{
s=s+b;
if(s==N)
{
flag=true;
for(c=a;c<=b;c++)
System.out.print(c + "+");
System.out.println();
break;
}
}
}
if(flag==true)
System.out.println("It Is A Triangular Number");
else
System.out.println("It Is Not A Triangular Number");
}
}
OUTPUT 1:
Enter The Number: 15
1+2+3+4+5+
4+5+6+
7+8+
It Is A Triangular Number
OUTPUT 2:
Enter The Number: 25
3+4+5+6+7+
12+13+
It Is A Triangular Number
OUTPUT 3:
Enter The Number: 35
2+3+4+5+6+7+8+
5+6+7+8+9+
17+18+
It Is A Triangular Number
11
PROGRAM 5
Write A Program To Check Whether A Number Is A Smith Number or Not.(eg. 27, 666 etc)
ALGORITHM:
STEP 1: START.
STEP 2: „N‟ is defined as a class variable of int datatype.
STEP 3: Parameterized constructor is used to initialized „N‟ by x.
main()
STEP 4: In this function, it is checked whether the function sodg(N) is equal to the function sopf(N) by using
„if‟ statement.
STEP 5: If the two function in „STEP 4‟ are equal , it is printed that the number is „Smith Number‟ else it is
printed that „It is not a Smith Number‟.
sodg(int y)
STEP 6: In this function two variables „s‟ and „dg‟ are initialized and 0 is stored in „s‟.
STEP 7: A while loop is used which runs till y>0. The numbers are extracted from „y‟ by dividing it with 10
and the remainder is stored in „dg‟. The numbers stored in „dg‟ are added in „s‟ and the „y‟ becomes
y/10.
The value of „s‟ is returned.
sopf(int z)
STEP 8: In this function two variables „f‟ and „s‟ of int datatype are initialized and 2 is stored in „f‟ and 0 in
„s‟.
STEP 9: A while loop is used which runs till „f‟ is less than equal to „z‟. „If‟ statement is used to check
Whether „z‟ is divisible by „f‟ and „f‟ is send to the function sodg and it is added and stored in „s‟ and
then „z‟ becomes z/f.
STEP 10: In the above step „if‟ statement is used to check whether „z‟ is divisible by „f‟ or not, if not „f‟ ecomes
f+1 and „s‟ is returned.
main()
STEP 11: In this function the value is accepted from the user using Scanner class.
STEP 12: Object of the class is created and the above functions are called using the object.
STEP 13: STOP.
12
Program code in JAVA:
//check for Smith Number
import java.util.*;
class Smith
{
int N;
Smith(int x)
{
N=x;
}
void Show()
{
if(sodg(N)==sopf(N))
{
System.out.print("It Is A Smith Number");
}
else
{
System.out.print("It Is Not A Smith Number");
}
}
int sodg(int y)
{
int s=0,dg;
while(y>0)
{
dg=y%10;
s=s+dg;
y=y/10;
}
return s;
}
int sopf(int z)
{
int f=2;
int s=0;
while(f<=z)
{
if(z%f==0)
{
s=s+sodg(f);
z=z/f;
}
else
f++;
}
return s;
}
static void main()
{
13
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Number:");
int a=sc.nextInt();
Smith obj=new Smith(a);
obj.Show();
}
}
OUTPUT 1:
Enter The Number: 27
It Is A Smith Number
OUTPUT 2:
Enter The Number: 166
It Is A Smith Number
OUTPUT 3:
Enter The Number: 378
It Is A Smith Number
14
PROGRAM 6
Write a program to check whether a number is a Circular Prime number or not.
e.g. 197 as all combinations of 197 i.e. 197, 971, 719 are primes
ALGORITHM:
STEP 1: START.
isprime(int x)
STEP 2: In this function variable „f‟, „nof‟ are initialized and 0 is stolred in „nof‟.
STEP 3: A for loop is used where f runs from 1 till less than „x‟. It is checked whether x is divisible by „f‟
using „if‟ statement then „nof‟ is added with 1.
STEP 4: Then it is checked whether „nof‟ is equal to 2, if it is equal then it returns „true‟ else it returns „false‟.
countdg(int x)
STEP 5: In this function variable „c‟ is assigned and 0 is stored in it. A while loop is used which runs till x>0.
The
variable „c‟ is added with 1 and x becomes x/10 and the value of ‟c‟ is returned.
main()
STEP 6: In this function the value is accepted from the user using the Scanner class.
STEP 7: Variables „nodg‟, „NN‟, „dg‟, „org‟ are initialized which are of int datatype. A variable flag is also
initialized which is of Boolean datatype and „true‟ is storted in it.
STEP 8: A while loop is used which runs till „NN‟ is not equal to „org‟. „if‟ statement is used to check whether
„N‟ is Prime number. If „N‟ is a Prime number it is printed. Then „N‟ is divided by 10 and the
remainder
is stored in „dg‟.
STEP 9: If „N‟ is not a Prime number „flag‟ becomes „false‟ and the loop is stopped using break and „NN‟ is
stored in „N‟.
STEP 10: If „flag‟ is „false‟ it is printed as not a circular prime else it is printed as it is a circular prime.
STEP 11: STOP.
15
Program code in JAVA:
//circular Prime Number check
import java.util.*;
class CircularPrime
{
static boolean isprime(int x)
{
int f,nof=0;
for(f=1;f<=x;f++)
if(x%f==0)
nof++;
return nof==2?true:false;
}
static int countdg(int x)
{
int c=0;
while(x>0)
{
c++;
x/=10;
}
return c;
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter A Number To Check: ");
int N=sc.nextInt();
int nodg=countdg(N);
int NN=0,dg,org=N;
boolean flag=true;
while(NN!=org)
{
if(isprime(N))
{
System.out.println(N);
dg=N%10;
NN=(dg*(int)Math.pow(10,nodg-1))+N/10;
}
else
{
flag=false;
break;
}
N=NN;
}
if(!flag)
System.out.println("Not A Circular Prime");
else
System.out.println("Its A Circular Prime");
16
}
}
OUTPUT 1:
Enter A Number To Check: 71
71
17
It‟s a Circular Prime
OUTPUT 2:
Enter A Number To Check: 113
113
311
131
It‟s a Circular Prime
OUTPUT 3:
Enter A Number To Check: 1193
1193
3119
9311
1931
It‟s a Circular Prime
17
PROGRAM 7
Write a program to accept any amount to pay and display all possible combinations of the notes to pay that
amount.
ALGORITHM:
STEP 1: START.
main()
STEP 2: In this function the amount is accepted from the user using the Scanner class and is stored in the
variable „a‟ of int datatype.
STEP 3: If „a‟ is not divisible by 100 it is printed as „wrong input‟ else the various notes which can be drawn is
printed.
STEP 4: Variables „C1‟, „C2‟, „C3‟, „C4‟ of int datatype are initialized.
STEP 5: Four „for‟ loops are used, the first loop is C1=0;C1<=a/2000;C1++, the second loop is C2=0;
C2<=a/500;C2++, the third loop is C3=0;C3<=a/200;C3++, the fourth loop is C4=0;
C4<=a/100;C4++.
STEP 6: „if‟ statement is used to check whether C1*2000 + C2*500 + C3*200 + C4*100 is equal to „a‟ and
The value of C1, C2, C3 and C4 are printed.
STEP 7: STOP.
18
Program code in JAVA:
//combination of notes
import java.util.*;
class CurrencyCombinations
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Any Amount:");
int a=sc.nextInt();
if(a%100!=0)
{
System.out.print("Wrong Input");
}
else
{
System.out.println("_________________________________________");
System.out.println("\n"+"Sr.no"+"\t"+"2000" +"\t"+ "500" +"\t"+ "200" +"\t"+ "100" );
System.out.println("_________________________________________");
}
int C1,C2,C3,C4;
int b=0;
for(C1=0;C1<=a/2000;C1++)
{
for(C2=0;C2<=a/500;C2++)
{
for(C3=0;C3<=a/200;C3++)
{
for(C4=0;C4<=a/100;C4++)
{
if(C1*2000+C2*500+C3*200+C4*100==a)
{
System.out.println("\n"+ ++b+"."+"\t"+ C1 +"\t"+ C2 +"\t"+ C3 +"\t"+ C4 );
}
}
}
}
}
}
}
OUTPUT 1:
Enter Any Amount:500
Sr.no 2000 500 200 100
1. 0 0 0 5
2. 0 0 1 3
3. 0 0 2 1
4. 0 1 0 0
19
OUTPUT 2:
Enter Any Amount:800
Sr.no 2000 500 200 100
1. 0 0 0 8
2. 0 0 1 6
3. 0 0 2 4
4. 0 0 3 2
5. 0 0 4 0
6. 0 1 0 3
7. 0 1 1 1
20
PROGRAM 9
Write a program to accept two positive integers and generate all Unique Digit Numbers in that range and
calculate their frequency.
ALGORITHM:
STEP 1: START.
STEP 2: In main, the values of first limit and second limit are accepted from the user using the Scanner class
and are stored in variables „n‟ and „m‟ of int data type.
STEP 3: A for loop is created which runs from i=N to i<=M.
STEP 4: Inside the above for loop, a while loop is created which runs till nn>0, where nn=nn/10 in each loop.
STEP 5: Another while loop is created inside the above while loop, where the value of flag is modified,
running while dg2>0, where dg2=dg2/10 in each loop.
STEP 6: Inside the for loop of STEP 3 and outside the while loop of STEP 4, an if statement is created which
prints value of „i‟ and modifies the value of c=c+i, if flag==true.
STEP 7: Frequency c is printed.
STEP 8: STOP.
21
Program code in JAVA:
/* Program to accept two positive integer and generate all unique digit numbers in that range and calculate
their frequency.*/
import java.util.*;
class UniqueDigit
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The First Limit:");
int A=sc.nextInt();
System.out.print("Enter The Last Limit:");
int B=sc.nextInt();
boolean flag=true;
int c=0,i;
System.out.println("Unique Digit Numbers Are:");
for(i=A;i<=B;i++)
{
int tmp=i;
while(tmp>0)
{
int dg1=tmp%10;
int dg2=tmp/10;
while(dg2>0)
{
int z=dg2%10;
if(dg1==z)
{
flag=false;
break;
}
dg2=dg2/10;
}
tmp=tmp/10;
}
if(flag)
{
c++;
System.out.println(i);
}
flag=true;
}
System.out.println("The Frequency Is:"+c);
}
}
22
OUTPUT 1:
Enter The First Limit: 10
Enter The Last Limit: 20
Unique Digit Numbers Are:
10
12
13
14
15
16
17
18
19
20
The Frequency Is: 10
OUTPUT 2:
Enter The First Limit: 15
Enter The Last Limit: 30
Unique Digit Numbers Are:
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
The Frequency Is: 15
23
PROGRAM 9
Write a program to find the Union and Intersection of two sets.
ALGORITHM:
STEP 1: START.
STEP 2: The length of the first set and the second set is accepted. Now input unique elements and store
elements in both the array.
STEP 3: Three arrays A[], B[] and U[], I[] of int data type are initialized and the elements of the first set are
accepted in the array A[] and the elements of the first set are accepted in the array B[].
STEP 4: A for loop is used to generate the union set by comparing between the set A[] and set B[] and is
stored in the array U[] and is printed.
STEP 5: A nested for loop is used to generate the intersection I[] set by comparing between the set A[] and set
B[] and is duly printed.
STEP 6: STOP.
24
Program code in JAVA:
import java.util.*;
class Union_Intersection
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Length Of The First Set: ");
int x=sc.nextInt();
int A[]=new int[x];
System.out.print("Enter The Length Of The Second Set: ");
int y=sc.nextInt();
int B[]=new int[y];
int U[]=new int[x+y];
int I[]=new int[x<y?x:y];
int i,j,a=0,b=0;
boolean flag;
System.out.println("\nEnter uniuqe Elements of Set-1...");
for(i=0;i<x;i++)
{
System.out.print("Enter The "+(i+1)+" Elements: ");
A[i]=sc.nextInt();
U[a++]=A[i];
}
System.out.println("\nEnter uniuqe Elements of Set-2...");
for(i=0;i<y;i++)
{
System.out.print("Enter The "+(i+1)+" Elements: ");
B[i]=sc.nextInt();
}
//finding union and intersection
for(j=0;j<y;j++)
{
flag=false;
for(i=0;i<x;i++)
{
if(B[j]==A[i])
flag=true;
}
if(flag)
I[b++]=B[j];
else
U[a++]=B[j];
}
System.out.print("\nThe Union Is....");
for(i=0;i<a;i++)
System.out.print(U[i]+",");
25
System.out.print("\nThe Intersection Is....");
for(i=0;i<b;i++)
System.out.print(I[i]+",");
}
}
/*
OUTPUT:
Enter The Length Of The First Set: 3
Enter The Length Of The Second Set: 4
26
PROGRAM 10
Write a program to accept numbers in an array and sort it using Insertion Sort.
ALGORITHM:
STEP 1: START.
STEP 2: Variables „i‟,‟P‟, „tmp‟, „K‟ and „a‟ of int datatype is initialized and 1 is stored in „a‟.
STEP 3: How many numbers the user want to enter is stored in „N‟ of int datatype using the Scanner class.
STEP 4: An array Arr[] of int datatype is initialized.
STEP 5: A for loop is used to store the elements given by the user in the array Arr[].
STEP 6: The number entered by the user in the array Arr[] is displayed using the for loop.
STEP 7: A for loop is used to store the first elements of the array Arr[] in tmp and the position of the next
element is stored in „K‟.
STEP 8: A while loop is used to check whether the first element of the array Arr[] is greater than the second
element, if true their place is interchanged and if false it is checked with the third element and the
process continues till we get all the elements sorted in ascending order.
STEP 9: The sorted array in ascending order is displayed.
STEP 10: STOP.
27
Program code in JAVA:
/*Program to accept numbers in an array and sort it in ascending order using Insertion Sort*/
import java.util.*;
class InsertionSort
{
static void main()
{
int i,P,tmp,K,a=1;
Scanner sc=new Scanner(System.in);
System.out.print("How Many Numbers You Want To Enter?: ");
int N=sc.nextInt();
int Arr[]=new int[N];
for(i=0;i<N;i++)
{
System.out.print("Enter The "+(a++)+" Number: ");
Arr[i]=sc.nextInt();
}
//Display
System.out.print("\nGiven Numbers Are: ");
for(i=0;i<N;i++)
System.out.print(Arr[i] + " ; ");
//Sorting Starts
for(P=1;P<N;P++)
{
tmp=Arr[P];
K=P-1;
while(K>=0 && Arr[K]>tmp)
{
Arr[K+1]=Arr[K];
K--;
}
Arr[K+1]=tmp;
}
28
OUTPUT 1:
How Many Numbers You Want To Enter?: 5
Enter The 1 Number: 12
Enter The 2 Number: 15
Enter The 3 Number: 13
Enter The 4 Number: 18
Enter The 5 Number: 21
OUTPUT 2:
How Many Numbers You Want To Enter?: 7
Enter The 1 Number: 32
Enter The 2 Number: 35
Enter The 3 Number: 21
Enter The 4 Number: 28
Enter The 5 Number: 65
Enter The 6 Number: 45
Enter The 7 Number: 39
OUTPUT 3:
How Many Numbers You Want To Enter?: 10
Enter The 1 Number: 21
Enter The 2 Number: 23
Enter The 3 Number: 25
Enter The 4 Number: 12
Enter The 5 Number: 18
Enter The 6 Number: 56
Enter The 7 Number: 61
Enter The 8 Number: 45
Enter The 9 Number: 28
Enter The 10 Number: 61
29
PROGRAM 11
Write a program to check whether a number is a Prime or not - using recursion.
ALGORITHM:
STEP 1: START.
STEP 2: An integer is accepted and stored in „N‟ of int datatype using the method main.
STEP 3: If statement is used to check whether „N‟ is a prime number or not by sending N and 2 to the
function „boolean isprime‟. If „true‟, it is printed „It Is A Prime Number‟ else it is printed „It Is Not A
Prime Number‟.
Step 4: In the function isprime it is checked whether a number is a prime or not by giving a base recursion if
(a==b) return „true‟. Then it is checked whether a is equal to 1 or a%b is equal to 0. If so, „false‟ is
returned. Else the value of a is returned and b is increased by 1.
STEP 5: STOP.
30
Program code in JAVA:
class PrimeCheck
{
static void main(int N)
{
if (isprime(N,2))
System.out.print("It Is A Prime Number");
else
System.out.print("It Is Not A Prime Number");
}
static boolean isprime(int a,int b)
{
if(a==b)
return true;
else if(a==1 || a%b==0)
return false;
else
return isprime(a,b+1);
}
}
OUTPUT 1:
Enter N: 5
It Is A Prime Number
OUTPUT 2:
Enter N: 19
It Is A Prime Number
OUTPUT 3:
Enter N: 25
It Is Not A Prime Number
31
Write a program to generate reverse Fibonacci series up to N terms using recursion.
ALGORITHM:
STEP 1: START.
STEP 2: An integer is accepted and is stored in „N‟ of int data type using the method main.
STEP 3: A for loop is used starting from N to 1 and the values are send to the function Rgetfiboterm to
generate the Fibonacci series and is printed.
STEP 4: In the function Rgetfiboterm it is checked whether „a‟ is equal to 1 and „a‟ is equal to 2 and 0 and 1 is
returned respectively. else (a-1) + (a-2) is returned.
STEP 5: STOP.
32
Program code in JAVA:
class FibonacciSeries
{
static void main(int N)
{
int t;
System.out.println("\nN ="+N);
System.out.print("\nThe 1st "+N+" Fibonacci numbers in Reverse are...");
for(t=N;t>=1;t--)//loop is reversed g\here
System.out.print(Rgetfiboterm(t) + ", ");
}
static int Rgetfiboterm(int a)
{
if(a==1)
return 0;
else if(a==2)
return 1;
else
return Rgetfiboterm(a-1)+Rgetfiboterm(a-2);
}
}
/*
Output:
eg:1
N =5
The 1st 5 Fibonacci numbers in Reverse are...3, 2, 1, 1, 0,
eg:2
N =10
The 1st 10 Fibonacci numbers in Reverse are...34, 21, 13, 8, 5, 3, 2, 1, 1, 0,
*/
33
PROGRAM 16
Write a program to Encode and Decode a code given by the user.
ALGORITHM:
STEP 1: START.
STEP 2: Variables „i‟, „c‟, „d‟ and „e‟ of int data type are initialized and 2 is stored in „c‟, 3 in‟d‟ and 0 in „e‟.
STEP 3: A sentence to be encoded is accepted and stored in String A using the Scanner class.
Step 4: Function charAt is used to extract the characters of the String A and is converted in int and is printed
with the message “Here Is Your Encoded Number”.
STEP 5: The code to be decoded is accepted and stored in String B using Scanner class.
STEP 6: A while loop is used stating e<B.length() and if statement is used to find out the space and is printed.
STEP 7: Else the remaining are extracted and converted to characters and is printed.
STEP 8: STOP.
34
Program code in JAVA:
OUTPUT 1:
Enter The Sentence To Be Encoded: Have A Nice Day
Here Is Your Encoded Number:
72971181013265327810599101326897121
35
OUTPUT 2:
Enter The Sentence To Be Encoded: HaPPy NeW YeaR 2020
Here Is Your Encoded Number:
72978080121327810187328910197823250485048
OUTPUT 3:
Enter The Sentence To Be Encoded: Good Morning! Have A Nice Day!
Here Is Your Encoded Number:
71111111100327711111411010511010333327297118101326532781059910132689712133
36
Project Work in XI(2021-2022)
Write a program to positive integer numbers and its base in between 2-20 and convert it into any base in
between 2 & 20.
ALGORITHM:
37
Program code in JAVA:
//Project Program to convert any number from one base to another base
import java.util.*;
class XI_Projectfor_ISC2021
{
static boolean isvalid(String n, int b)
{
int k, nl=n.length(),idg;
boolean ans=true;
char dg;
if(b<2 || b>20)
ans=false;
else
for(k=0;k<nl;++k)
{
dg=n.charAt(k);
if(dg>=48 && dg<=57)
idg=dg-48;
else
idg=dg-55;
if(idg>=b)
ans=false;
}
if(!ans)
System.out.println("\nWrong Input");
return ans;
}
static void main()
{
System.out.println("*****WELCOME TO THE PROJECT of XI*****");
Scanner sc=new Scanner(System.in);
String N,ans="";
char dg;
int dv=0,b,nl,vdg ,db,p,idg;
do
{
System.out.print("\nEnter any number : ");
N=sc.next();
N=N.toUpperCase();
nl=N.length();
System.out.print("Enter its Base(2-20): ");
b=sc.nextInt();
}while(!isvalid(N,b));
do{
System.out.print("\nEnter the desired Base(2-20) for conversion: ");
db=sc.nextInt();
38
}while(db<2 || db>20);
//converting input number into decimal
for(p=nl-1;p>=0;--p)
{
dg=N.charAt(p);
if(dg>=48 && dg<=57)
idg=dg-48;
else
idg=dg-55;
dv+=idg*(int)Math.pow(b,nl-1-p);
}
//System.out.println("Decimal equivalent....."+dv);
//converting the decimal into desired base
while(dv>0)
{
int r=dv%db;
if(r>9)
ans= (char)(55+r)+ans;
else
ans=r+ans;
dv=dv/db;
}
System.out.println("\n\nSo, The number in base "+db+" is....."+ans);
}//end of main()
} //end of class
/*
OUTPUT:
eg 1:
*****WELCOME TO THE PROJECT of XI*****
Enter any number : 101
Enter its Base(2-20): 10
Enter the desired Base(2-20) for conversion: 17
So, The number in base 17 is.....5G
eg 2:
Enter any number : 101010
Enter its Base(2-20): 2
Enter the desired Base(2-20) for conversion: 16
So, The number in base 16 is.....2A
eg 3:
Enter any number : 10982
Enter its Base(2-20): 8
Wrong Input
eg 4:
Enter any number : 99
Enter its Base(2-20): 20
Enter the desired Base(2-20) for conversion: 10
So, The number in base 10 is.....189 */
39