Programs 12345
Programs 12345
Programs 12345
ISC 2021
Program1
Write a program to assign values to variables length, and breadth and print if it is a square or
a rectangle using a default constructor
Algorithm:
Step1: Start
Step3: End
number()
Step1: Start
Step3: End
void check()
Step1: Start
Step2: Check if length is equal to breadth and if it is equal, then print "It is a square"
Step4: End
void main()
Step1: Start
Step4: End
Page Break
2
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source Code:
public class number
{
int l,b;
public number()
{
l=0; b=0;
}
void check()
{
if(l==b)
{
System.out.println("It is a square");
}
else
{
System.out.println("It is a rectangle");
}
}
{
cal.check();
}
}
3
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 2
WAP to input values to variables, length and breadth in the main function and print if it is
.square or a rectangle
Algorithm:
Step1: Start
Step3: End
Dimension()
Step1: Start
Step3: End
void print()
Step1: Start
Step2: If the length is equal to the breadth, then print, "It is a square"
Step4: End
void main()
Step1: Start
Step6:End
4
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source Code:
import java.util.*;
public class dimension
{
Int l,b;
public dimension();
{
l=0;b=0;
}
void print(l,b)
{
if(l==b) //checking
{
System.out.println("It is a square"); //printing
}
else
{
System.out.println("It is a rectangle");
}
}
{
Scanner ob=new Scanner(System.in);//input values
System.out.println("Enter breadth");breadth=ob.nextInt();
5
Name: VIKHYAT AGRAWAL UID:
ISC 2021
ob.print(l,b);
}
}
6
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 3
Write a program to assign a value to marks and print,
excellent>90; good>80, fair>70, average>=60, poor<50.
Algorithm:
Step1: Start
Step3: End
Calculate()
Step1: Start
Step3: End
print()
Step1: Start
Step2: Check if the marks are >=90, then print "Excellent", if it is.
Step3: Check if the marks are >=80 and marks<90, then print "Good", if it is.
Step4: Check if the marks are >=70 and marks<80, then print "Fair", if it is.
Step5: Check if the marks are >=60 and marks<50, then prtin "Average", if it is.
Step6: Check if the marks are <50, then print "Poor", if it is.
Step7: End
void main()
Step1: Start
Step4: End
7
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source Code:
public class calculate
{
int marks;
public calculate(); {
marks=97;
}
public void print()
{
if(marks>=90)
{
System.out.println("Excellent");
}
{
System.out.println("Good");
}
{
System.out.println("Fair");
}
{
System.out.println("Average");
}
else if(marks<50)
8
Name: VIKHYAT AGRAWAL UID:
ISC 2021
{
System.out.println("Poor");
}
}
{
ob.print();
}
}
9
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 4
A Special number is a number in which the sum of the factorials of each digit is equal to
number itself .for example 145=1!+4!+5!=1+24+120
Design a special class to check if a given number is a special no or not
ALGORITHM
Step1: Start
Step2: Initialize instance variables n,num,r,sum of factorial,fact
Step3: End
check()
Step1: Start
Step2: Initialize the Scanner class
Step3: Take the users input for n
Step4: Copy the value of n to num
Step5: Start a while loop where num>0
Step6: Use r to calculate the remainder of num
Step7: Assign value to fact
Step8: Start a for loop from i=1 to calculate factorial of each digit of number
Step9: To calculate sum of factorial
Step10: Use num to calculate quotient of num
Step11: Check whether n=sum of factorial ,if it is then print “special number”
Step12: Else print “ Not special no”
Step13: End
Public static void main()
Step1: Start
Step2: Create an object ob
Step3: Call function check()
Step4: End
10
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code:
import java.util.*;
public class Special Number
{
Int n,num,r,sum of factorial=0,fact;
Public void check()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
int fact=1;
for(int i=1;i<=r;i++)
{
fact=fact*i;
}
sum Of Factorial = sum Of Factorial+fact;
num = num / 10;
}
if(n==sum Of Factorial)
{
System.out.println("Special Number" );
}
else
{
System.out.println("Not Special Number" );
}
}
Public static void main()
{
11
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Special ob=new special();
Ob.check();
}
}
12
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 5
A class numbers contains the following data members and member functions to check for
triangular numbers [A triangular number is formed by the addition of a consecutive sequence
of integers starting from 11
For example
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15
Therefore 3, 6,10,15 are triangular numbers
Class name: numbers
Data Members/instance variables:
n: integers to be checked for whether it is triangular or not
Member functions/methods
void getnum()to accept integer n
int check (i) to check if n is triangular
void dispnum() to display message whether 'n' is triangular or not
Specify the class Numbers giving the details of the functions getnum, int check() and void
dispnum().
13
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Algorithm
Step1: Start
Step2: Initialize the instance variable n
Step3: End
Void getnum()
Step1: Start
Step2: Initialize the scanner class
Step3: Take the users input for n
Step4: End
Void check()
Step1: Start
Step2: Initialize and assign values to sum,x,I;
Step3: Check whether n >3 if then
Start the while loop where (n>sum)
To find value of sum
Step4: To check whether sum=n,if then x=1 else x=0
Step5: End
Void display()
Step1:Start
Step2: to check whether x=1;if then print (“no is triangular”)
Step3: Else (“no is not triangular”)
Step4: End
Public static void main()
Step1: Start
Step2: Create an object ob
Step3: Call the function Void check(),Void getnum(),Void display()
Step4: End
14
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code:
Import java.util.*:
class Numbers{
int n,
public void getnum() {
scanner ob =new scanner(system.in)
system.out.println(“enter the no”);n=ob.next int()
}
public int check(){
int sum=0,x=0,i=1;
if(n>3)
{
While(n>sum)
{
Sum=sum+i;
i++
}
}
If(sum=n)
x=1;
Else
x=0;
}
public void display(){
if(x=1)
System.out.println(n + " is triangular.");
else
System.out.println(n + " is not triangular.");
}
Public static void main()
{
Numbers ob=new numbers();
15
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Ob.getnum();
Ob.check();
Ob.display();
}
}
16
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 6
Write a program to input a three digit number and check and print whether a no is Armstrong
no or not
Algorithm
Step1: Start
Step2: Initialization of instance variables n,result=0
Step3: End
Void input()
Step1: Start
Step2: Initliaze the scanner class
Step3: Take the user input for n
Step4: End
Void calculate()
Step1: Start
Step2: Initialize the function variables m, r
Step3: Copy the value of n to m
Step4: Start the while loop where (m! =0)
To find the value of result
Step5: End
Void display()
Step1: Start
Step2: To check whether (result=n),if then print “n is an Armstrong no”
Step3: Else print “n is not an Armstrong no”
Step4: End
Public static void main()
Step1: Start
Step2: Create an object ob
Step3: Call the function Void input(),Void calculate(),Void display()
Step4: End
17
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code
import java.util.*;
public class Armstrong
{
int n, result=0;
void input()
{
Scanner ob=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
n=ob.nextInt();
}
void calculate() {
int m, r;
m =n;
while (m != 0)
{
r= m % 10;
result += Math.pow(r, 3);
m /= 10;
}}
void display()
{
if(result== n)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
public static void main()
{
18
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Armstrong ob=new Armstrong();
ob.input();
ob.calculate();
ob.display();
}
}
19
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 7
A class telcall calculates the monthly phone bill of a consumer some of the the class are given
below.
Class name
Data members/instance variable:
Phno: phone number
name: name.consumer
no : number of calls made
amt: bill amount
Member function/methods:
Telecall():: parameterized Constructor to assign values to data members
void compute(): to calculate the phone bill amount based on slabs given below
void dispdata(): to display the details in the specified
Number of calls
1-100
101-200
201-300
Above 300
rate
Rs. 500/-rental charge only
Rs. 1.00/-per call + rental charge
Rs.1.20/-per call + rental charge
Rs. 1.50/-per call + rental charge
The calculations need to be done as per the slabs.
Specify the class telcall giving the details of all the functions.
In the main function create an object of type telcall and display the phone bill in the
following format:
Phone number
name
XXX
total calls
amount
20
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Algorithm
Step1: Start
Step2: Initialization of instance variables n, phno,amt,name
Step3: End
Telecall(int a,int b,string s)
Step1: Start
Step2: Assign of values to instance variables
Step3: End
Void compute()
Step1: Start
Step2: Initialization of function variable m
Step3: To calculare value of m
Step4: Copy the value of m to amt
Step5: End
Void dipdata()
Step1: Start
Step2: Print “ phone no is phno”
Step3: Print “ Name is name”
Step4: Print “Total no of call is n”
Step5: Print “Amount is amt”
Step6: End
Public static void main()
Step1: Start
Step2: Create an object obj
Step3: Call the function Void compute(),Void dipdata()
Step4: End
21
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code
import java.util.*;
class telecall
{
int phno,n;
double amt;
String name;
phno=a;
n=b;
name=s;
double m;
m=1000+100+(n-200)*1.2;
22
Name: VIKHYAT AGRAWAL UID:
ISC 2021
else
m=1120+(n-300)*1.5;
amt=m;
void dipdata()
System.out.println("Total Calls"+n);
System.out.println(" Amount"+amt);
}public static void main()
{
int a,b;
String s1;
Scanner ob=new Scanner(System.in);
System.out.println("enter name");s1=ob.nextLine();
System.out.println("enter your phone no");a=ob.nextInt();
System.out.println("enter total calls");b=ob.nextInt();
telecall obj=new telecall( a, b, s1);
obj.compute();
obj.dipdata();
}}
23
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 8
An Emirp number is a number which is prime backwards and forwards. Example: 13 and 31
are both prime numbers. Thus 13 is an Emirp number.
Design a class Emirp to check if a given number is Emirp number or not. Some of the
members of the class are given below:
Member functions:
intisprime (int x): to check the number is primeand return 1 if prime otherwise return 0.
void is Emirp (): reverse the given number and check if both the original number and the
reverse number are prime,
by invoking the function isprime(int) and display result with an appropriate message. Specify
the class Emirp giving details of the constructor(int), intisprime(int) and void Emirp(). Define
main() function to create an object and call the methods to check for Emirp number
24
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Algorithm
Step1: Start
Step2: Initialize the instance variables n,rev,f
Step3: End
EMIRP(INT NN)
Step1: Start
Step2: Assign values to instance variables
Step3: End
Is prime(int x)
Step1: Start
Step2: To check whether(n=x),if then return 1
Step3: Else if(n%x=0//n==1),if then return 0
Step4: Else isprime(x+1)
Step5 :End
Void isemirp()
Step1: Start
Step2: Copy the value of x to n
Step3: Start the while loop where (x!=0)
To find value of rev
Step4: Copy the value of ans1 to isprime(f)
Step5: Copy the value of n to rev and f=2
Step6: Copy the value of ans2 to isprime(f)
Step7: To check whether (ans1==1 && ans2==1) ,if then print “N is an emirp no”
Step8: Else print “N is not an emirp no”
Step9: End
Public static void main()
Step1: Start
Step2: Initialize the scanner class
Step3: Take the user input for x
Step4: Create an object obj
Step5: Call the function Void isemirp()
Step6: End
25
Name: VIKHYAT AGRAWAL UID:
ISC 2021
26
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code
import java.util. Scanner;
public class Emirp
{
int n,rev,f;
Emirp(int nn)
{
n=nn;
rev=0;
f=2; }
int isprime(int x)
{
if(n==x)
{
return 1;
}
else if (n%x==0 ||n == 1)
{
return 0;
}
else
return isprime(x+1);
}
void isEmirp()
{
int x=n;
while(x!=0)
{ rev=(rev* 10) + x%10;
x=x/10; }
int ans1=isprime(f);
n=rev;
27
Name: VIKHYAT AGRAWAL UID:
ISC 2021
f=2;
int ans2=isprime(f);
if(ans1==1 && ans2==1)
System. out.println(n+" is anEmirp number");
else
System.out.println(n+" is not an Emirp number"); }
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("\n Enter a number");
int x=sc.nextInt();
Emirp obj = new Emirp(x); obj.isEmirp(); }}
28
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program 9
Sum=1+x/1!+x3/2!+x5/3!+………………..x2n-1/n!
A class series sum has been defined to calculate sum of the above series. Design the class as
given below.
Class Name
Seriessum
n, x : integer
sum: double
Member functions/methods:
Sumseries! : Constructor
int factorial (int n): Calculates and returns factorial of n(n!) where
n! = 1*2*3*......n
double term (int p. int n): calculates and returns the value of p/q! by making use of factorial
(int)
double çalsum: calculates the sum of the given series using the appropriate data and other
member functions.
29
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Algorithm
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
30
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code
import java.util.*;
classeriessum
{
int x,n;
double sum;
seriessum()
{
sum=1.0;
n=0;
x=0;
}
int factorial(int n)
{
int f=1;
for(int i=1;i<=n;i++) {
f=f*i;
}
return(f);
}
double term(double p,int q)
{
p=Math.pow (x,p);
q=factorial(q);
double c=(double) p/q;
return(c);
}
void accept()
{
Scanner ob=new Scanner(System.in);
31
Name: VIKHYAT AGRAWAL UID:
ISC 2021
System.out.println("enter the value of x");
x=ob.nextInt();
System.out.println("enter the value of n");
n=ob.nextInt();
}
void displaysum()
{
System.out.println("sum is="+sum);
}
double calsum()
{
int i;
for(i=1;i<=n;i++)
{
sum=sum+term(2*i-1,i);
}
return(sum);
}
public void main()
{
Scanner ob=new Scanner(System.in);
seriessum obj=new seriessum();
obj.factorial(n);
obj.term(0,0);
obj.accept();
obj.calsum();
obj.displaysum();
}}
32
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Program10
A magic number is a number in which the eventual sum of the digits of the number is equal
to 1.
10 = 1+0=1
Design a class Magic to check if a given number is a magic number. Some of the members of
the class are given below.
Member functions/methods:
void ismagic: checks if the given number is a magic number by calling the function Sum of
Digits(int) and displays appropriate
33
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Algorithm
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
34
Name: VIKHYAT AGRAWAL UID:
ISC 2021
Source code
import java.util.Scanner;
public class Magic
{
int n,ntemp; //instace variable to store the number
public Magic() //default constructor, assigns 0 to n
{
n=0;
}
public void getnum(int nn) //method, assigns the parameter value to the number
{
n=nn;
}
public int Sum_of_digits() //method, returns the sum of digits of number
{
int n1,r1,sum=0;
ntemp=n;
do //outer loop
{
sum=0;
do //inner loop
{
n1=n/10;
r1=n%10;
sum+=r1;
n=n1;
}
while(n1!=0);
n=sum;
}
while(sum>=10);
return sum; //returning sum
}
public void ismagic() //method, checks if the number is Magic Number
{
int sum=Sum_of_digits();
if(sum==1)
System.out.println(ntemp+" is a Magic Number");
else
35
Name: VIKHYAT AGRAWAL UID:
ISC 2021
System.out.println(ntemp+" is not a Magic Number");
}
public static void main()
{
Magic ob=new Magic(); //creating object of Magic class
Scanner in=new Scanner(System.in); //creating object of Scanner class
System.out.println("Enter a number");
int num=in.nextInt(); //inputting number
ob.getnum(num); //calling method
ob.ismagic(); //calling method
}
36
Name: VIKHYAT AGRAWAL UID:
ISC 2021
PROGRAM 11
Pal prime numbers are palindromic prime numbers i.e. the prime numbers that are
palindrome also.
First pal prime number is 2 digit (2-d) number and it is 11. There is only one pal prime which
is 2 digits long. To generate a 3 digit (3-d) pal prime number there are many methods. One
method is where one just needs to insert a 1-d palprime number in between 11 and then test
for its primeness.
This way only two pal primes are generated (by inserting 3 and 5 and between 11) 131 and
151.
(insertion of 7 in 11 does not yield a prime number)
You need to write the method GenPalPrime for the following class that generates
pal primes using the method mentioned here
Class PalPrime
{public static void GenpalPrime (int n)
{/* the passed number n tells the number of digits in the palprime to be generated.
Acceptable values for n are 2-5*/
}
}
The 5-d primes may be generated by inserting 3-d palprimes in 11 eg, insertion of 131,
383, 797, yields these palprime numbers :11311,13831,17971
You may define additional methods if needed.
Sample Input Output:
Enter the width of palprime numbers(i.e. no. of digits in a palprime no): 2
11
Enter the width of palprime numbers(i.e. no of digits in a palprime no): 3
131
151
37
Name: VIKHYAT AGRAWAL UID:
ISC 2021
ALGORITHM
38
Name: VIKHYAT AGRAWAL UID:
ISC 2021
SOURCE CODE
import java.util.Scanner;
public class PalPrime
{
public static boolean isprime(int n) //method, checks whether a number is prime or not
{
int flag=1;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
return true;
else
return false;
}
public static boolean ispal(int n) //method, checks whether a number is palidrome or not
{
int n1,r1,ntemp,rev=0;
ntemp=n;
do
{
n1=n/10;
r1=n%10;
rev=rev*10+r1;
n=n1; }
39
Name: VIKHYAT AGRAWAL UID:
ISC 2021
while(n1>0);
if(rev==ntemp)
return true;
else
return false;
}
public static void GenPalPrime(int n) //method to generate numbers which are prime and
palindrome both
{
int count=0;
if(n==2)
{
System.out.println("11");
count++;
}
else if(n==3)
{
int begnum=101;
int lnum=0;
boolean boolpr=false;
boolean boolpl=false;
for(int i=3;i<10;i++)
{
boolpr=isprime(i);
boolpl=ispal(i);
if(boolpr==true && boolpl==true)
{
lnum=begnum+i*10;
if(isprime(lnum)==true && ispal(lnum)==true)
{
System.out.println(lnum);
count++;
40
Name: VIKHYAT AGRAWAL UID:
ISC 2021
}}}}
else if(n==4)
{
int begnum=1001;
int lnum=0;
boolean boolpr=false;
boolean boolpl=false;
for(int i=10;i<100;i++)
{
boolpr=isprime(i);
boolpl=ispal(i);
if(boolpr==true && boolpl==true)
{
lnum=begnum+i*10;
if(isprime(lnum)==true && ispal(lnum)==true)
{
System.out.println(lnum);
count++;
}}}}
else if(n==5)
{
int begnum=10001;
int lnum=0;
boolean boolpr=false;
boolean boolpl=false;
for(int i=100;i<1000;i++)
{
boolpr=isprime(i);
boolpl=ispal(i);
if(boolpr==true && boolpl==true)
{
41
Name: VIKHYAT AGRAWAL UID:
ISC 2021
lnum=begnum+i*10;
if(isprime(lnum)==true && ispal(lnum)==true)
{
System.out.println(lnum);
count++;
}}}}
else
System.out.println("Value of n should be within range 2-5");
if(n>=2 && n<=5 && count==0)
System.out.println("No palprime number of this width");
}
42
Name: VIKHYAT AGRAWAL UID:
ISC 2021
43
Name: VIKHYAT AGRAWAL UID:
ISC 2021
PROGRAM 12
Brun Constant. Twin primes are the prime numbers with a difference of 2 eg. (3.5) (5,7), (11,
13), (17,39) (29,31)
The sum of reciprocals of the twin primes converges to a sum known as a Brun'sConstant
The class Primes below contains methods which help you to work with twin primes and
Bruns's constant .Study the class and then answer the questions that follow:
public class Primes{
public Boolean ls Prime(int n)
{
/*returns true if n is a prime number, false otherwise*/
//Assume n>1
}
public void twin Primes(int lim) {*prints out twin primes below lim. Each pair is printed on a
separate line within brackets e.g. (3,5)
public double Brun Constant (int lim)
{*calculates and returns Brun's constant below lim*/
Define all the methods of the class Primes given above
Define method main that obtains the limit value and prints the Brun's constant
accordingly, Sample Input Output
Enter limit 13
(1/3+1/5) (1/5+1/7)+(1/11+1/13)
Brun's constant below limit 13 is :1.0440226793289185
44
Name: VIKHYAT AGRAWAL UID:
ISC 2021
ALGORITHM
45
Name: VIKHYAT AGRAWAL UID:
ISC 2021
SOURCE CODE
import java.util.Scanner;
public class primes
{
double sum=0.0;
int i, f;
// Check the number prime or not
public boolean isPrime(int n)
{
f=1;
for (i = 2 ; i < n ; i++)
{
if(n % i == 0)
{
f=0;
break;
}
}
if(f == 1)
return true;
else
return false;
}
// Here checked the two number twin prime or not
// and also calculate brun's constant.
public void twinPrimes(int number )
{
System.out.println("Twin prime");
System.out.println("==========");
for (int i = 2; i < number ; i++)
46
Name: VIKHYAT AGRAWAL UID:
ISC 2021
{
int num1 = i;
int num2 = i + 2;
if (isPrime(num1) && isPrime(num2))
{
System.out.println(num1 + " " + num2);
}}}
public double BrunConstant(int number)
{ for (int i = 2; i < number ; i++)
{
int num1 = i;
int num2 = i + 2;
if (isPrime(num1) && isPrime(num2))
{
sum = sum + (double)1/num1 + (double)1/num2;
}} return sum;}
47