0% found this document useful (0 votes)
2 views53 pages

Program 1

This document is a Computer Science project file submitted by Prakhar Pratap Singh from St. Antony's Inter College for the session 2025-26. It includes various programming assignments, acknowledgments, and a detailed index of programs that cover topics such as pangrams, word analysis, unique-digit integers, Hamming numbers, and twisted primes. Each program is accompanied by its solution, variable descriptions, and example outputs.

Uploaded by

divyanshyadav033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

Program 1

This document is a Computer Science project file submitted by Prakhar Pratap Singh from St. Antony's Inter College for the session 2025-26. It includes various programming assignments, acknowledgments, and a detailed index of programs that cover topics such as pangrams, word analysis, unique-digit integers, Hamming numbers, and twisted primes. Each program is accompanied by its solution, variable descriptions, and example outputs.

Uploaded by

divyanshyadav033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ST.

ANTONY’S INTER COLLEGE


Sector-A, Jankipuram, Near Ram Ram Bank Chauraha,
Lucknow- 226021(UP)

Computer Science Project File


SESSION:- 2025-26
SUBMITTED BY- SUBMITTED TO-
Name:-Prakhar Pratap Singh Mr. Ashish Gupta
Class & Sec:-XI A
ASSESSMENT
Name-: Prakhar Pratap Singh
Class-: XI A
Subject-: Computer Science
Sub. Teacher-: Mr. Ashish
Gupta
School-: St. Antony’s Inter
College
Session-: 2025-26
Internal Examiner-:
External Examiner-:
AckNowlEdgEMENT
I would like to express my sincere gratitude to all those
who helped me complete this project successfully.
First and foremost, I would like to thank my Computer
Science teacher Mr. Ashish Gupta, for his constant
support, valuable guidance and encouragement
throughout the process.
I would also like to extend my thanks to my school
principal Mr. B. Antony, for providing all the necessary
facilities required for this project.
Finally, I would like to thank my family who motivated
me and boosted my morale when I was stressed.

Prakhar Pratap Singh


XI A
INdEX
S. No. Title T. Sign.
1. Program 1
2. Program 2
3. Program 3
4. Program 4
5. Program 5
6. Program 6
7. Program 7
8. Program 8
9. Program 9
10. Program 10
11. Program 11
12. Program 12
13. Program 13
14. Program 14
15. Program 15
16. Program 16
17. Program 17
18. Program 18
19. Program 19
20. Program 20
PROGRAM 1
Define a class to accept a string and check for Pangram string.
[A Pangram is a sentence that contains every letter of the alphabet
at least once.]
Example: "The quick brown fox jumps over the lazy dog"

SolUTIoN:
import [Link].*;
class pangram{
public static void pangramm(String s){
s=[Link]();
s=[Link]();// to remove trailing spaces from terminal ends
for(int i=65;i<=90;i++)//if this loop gets completed this means
that the string is pangram
{
String d=""+(char)i;
if(![Link](d))//if one of the alphabet is not present in
the string then the program terminates making string nonpangram
{[Link]("It is not a Pangram String");
[Link](0);}
}
[Link]("It is a Pangram String");

public static void main(String args[]) {


Scanner sc=new Scanner([Link]);
[Link]("Enter string to be tested :");
String s=[Link]();
pangramm(s);
}}
oUTPUT:
Example 1-
Enter string to be tested:
The quick brown fox jumps over the lazy dog
It is a Pangram String

Example 2-
Enter string to be tested:
I am the owner of this car.
It is not a Pangram String

V.d.S.:

Variable Datatype Description


s String To input a String to test
i int To run the loop to test the
presence of characters in
String.
d String To convert character into
String for testing.
PROGRAM 2
Design a class to accept a string and display the longest and smallest
word.
INPUT: Pack my box with five dozen liquor jugs.
LONGEST WORD: dozen
SHORTEST WORD: my

SolUTIoN:
import [Link].*;
class largesmall{
public static void smaarge(String s){
s=[Link]();// to remove the spaces at the terminal ends
s=s+" "; // to add a space so that the word can be extracted
from the string
int l=[Link]();
String large="";
String small=[Link](0,[Link](' ')); //to extract first
word from string
String rep="";
for(int i=0;i<l;i++)
{rep=rep+[Link](i);
if([Link](i)==' ')
{if([Link]()<[Link]()) // condition to find the
longest word
large =rep;
if([Link]()>[Link]()) // condition to find the
smallest word
small =rep;
rep="";// to emptify "rep" after each iteration
}}
[Link]("LONGEST WORD: "+large);
[Link]("SMALLEST WORD: "+small);
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("INPUT :");
String s=[Link]();
smaarge(s);
}}
oUTPUT:
INPUT : Hi ! my name is prakhar pratap singh
LONGEST WORD: prakhar
SMALLEST WORD: Hi

V.d.S.:

Variable Datatype Description


s String To input a String to test
l int To store the length of s
large String To store the largest word
small String To store the smallest word
rep String To store each word from s
that are separated by
‘ ’(space)
i int To extract each character
from s by loop
PROGRAM 3
Define a class to accept two positive integers m and n. Assume
m < 30000 and n < 30000. Given two positive integers m and n,
where m < n, write a program to determine how many unique-digit
integers are there in the range between m and n (both inclusive)
and output them.
INPUT: m = 100 n = 120
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE:
102, 103, 104, 105, 106, 107, 108, 109, 120
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9

SolUTIoN:
import [Link].*;
class unique{
public static boolean Unique(int p) // function to check if number
is unique or not
{
String s=[Link](p); // converting number to string
int f=0;
int l=[Link]();
for(int i=0;i<l;i++){
for(int j=i+1;j<l;j++)
{if([Link](i)==[Link](j))
f=1;}

}if(f==0)
return true;
else
return false; }
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
[Link]("INPUT : ");
[Link]("Value of m<n");
[Link]("m = ");
int m=[Link]();
[Link]("n = ");
int n=[Link]();
if(m<30000&&n<30000)// m & n should be less then 30000
{
[Link]("OUTPUT: THE UNIQUE DIGIT INTEGERS ARE :"); // a
unique digit no. is the one in which all numbers are different
for(int i=m;i<=n;i++)
if(Unique(i))
[Link](i+" , ");
}
else
[Link]("The value of m and n should be less than 30000");
}}

oUTPUT:
Example 1-
INPUT :
Value of m<n
m = 120
n = 130
OUTPUT: THE UNIQUE DIGIT INTEGERS ARE :
120 , 123 , 124 , 125 , 126 , 127 , 128 , 129 , 130 ,

Example 2-
INPUT :
Value of m<n
m = 30009
n = 45678
The value of m and n should be less than 30000
V.d.S.:

Variable Datatype Description


p int Formal parameter in
function to test for unique
digit number
m int To input the lower limit for
range
n int To input the upper limit for
range
i int To run the loop to test all
the numbers between m
and n for being unique
f int Flag variable to check if
argument is unique or not
PROGRAM 4
Define a class to accept a word and convert it in upper case, and
display the original word and new word in alphabetical order,
without using sorting technique.
Ex: Input : World
Output : original word : WORLD
Alphabetical word : DLORW

SolUTIoN:
import [Link].*;
class sortstring{
public static void sort(String s){
s=[Link](); // to remove the spaces from terminal points
s=[Link]();// to convert trimmed string to upper case
int c=0;
String st="";
for(int i=65;i<=90;i++)
{
for(int j=0;j<[Link]();j++)//loop to check the number of
occurences of a character
{
if([Link](j)==(char)i)
++c;// to count the no. of times a character is present
in string
}

for(int k=1;k<=c;k++)
{
st =st+(char)i;// to add the character c no. of times in
the blank string
}
c=0;
}
[Link]("Original word : "+s);
[Link]("Alphabetical word : "+st);
}
public static void main(String[] args) {
Scanner sc=new Scanner ([Link]);
[Link]("Input : ");
String s=[Link]();
sort(s);
}
}

oUTPUT:
Example 1:
Input : computer
Original word : COMPUTER
Alphabetical word : CEMOPRTU

Example 2:
Input : zenith
Original word : ZENITH
Alphabetical word : EHINTZ

V.d.S.:

Variable Datatype Description


s String To input a String to test
i int Used in loop to test all the
alphabetical characters
j int To run the loop to know
the number of times a
character is present in s
c int To count the number of
times a character is
present in s
st String To store a character c
number of times in sorted
manner
PROGRAM 5
Hamming numbers are positive integers whose prime factors
include 2, 3 and 5 only. Design a program to accept any positive
integer number and check if it is a Hamming number or not.
n = 8 is a hamming number as 8 = 2 × 2 × 2 and it has only 2 as its
prime factors.
n = 90 is a hamming number as 90 = 2 × 3 × 3 × 5 which has only 2, 3,
5 as prime factors.
n = 14 is not a hamming number as 14 = 2 × 7. It has 7 as one of its
prime factors.

SolUTIoN:
import [Link].*;
class hammin{
public static void hamming(int n){
int dup=n;
if(n>0){
while(n%2==0) //to divide the number by 2 as long as
possible
{n/=2;}
while(n%3==0) //to divide the number by 3 as long as
possible
{n/=3;}
while(n%5==0) //to divide the number by 5 as long as
possible
{n/=5;}
if(n==1)
[Link](dup+" is a Hamming number");
else
[Link](dup+" is not a hamming number");
}
else
[Link]("Enter a number that is positive not
negative");
}
public static void main(String[] args) {
Scanner sc=new Scanner ([Link]);
[Link]("Enter a positive number to be tested :");
int n=[Link]();
hamming(n);

}
}

oUTPUT:
Example 1-
Enter a positive number to be tested :
90
90 is a Hamming number

Example 2-
Enter a positive number to be tested :
102
102 is not a hamming number

Example 3-
Enter a positive number to be tested :
-100
Enter a number that is positive not negative

V.d.S.:

Variable Datatype Description


n int To input a number to test for
hamming number
dup int To create a copy of n
PROGRAM 6
A prime number is said to be 'Twisted Prime', if the new number
obtained after reversing the digits is also a prime number. Write a
program to accept a number and check whether the number is
'Twisted Prime' or not.
Sample Input: 167
Sample Output: 761 167 is a 'Twisted Prime'.

SolUTIoN:
import [Link].*;
public class twisted {
public static boolean prime(int n)// function to check if a number
is prime
{
int c=0;
for(int i=1;i<=n;i++) {
if(n%i==0)
c++;}
if (c==2)
return true;
else return false;

}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
[Link]("Enter a number to be checked :");
int n=[Link]();
if(prime(n))
{
int rev=0;
for(int i=n;i>0;i/=10)//to reverse the entered number
{
rev=rev*10+i%10;
}
[Link]("Reversed Number = "+rev);
if(prime(rev)) // to check if the reversed number is prime
[Link](n+" is 'Twisted Prime'");
else
[Link](n+" is not a twisted prime");
}
else{
[Link]("It is not prime number \nEnter a prime
number");//in case the entered number is not prime
}
}

oUTPUT:
Example 1-
Enter a number to be checked :
191
Reversed Number = 191
191 is 'Twisted Prime'

Example 2-
Enter a number to be checked :
214
It is not prime number
Enter a prime number

Example 3-
Enter a number to be checked :
97
Reversed Number = 79
97 is 'Twisted Prime'

V.d.S.:

Variable Datatype Description


i int To run the loop to count
the number of factors of n
c int To store the number of
factors n has
n int To input a number to test
for twisted prime
rev int To store the reversed
number of ‘n’
PROGRAM 7
Write a program to display the following pattern.
*******
*****
***
*
***
*****
*******

SolUTIoN:
class pattern2{
public static void main(String args[]){
for(int i=7;i>=1;i-=2)
{
[Link](" ");//to print the spaces in
left side
for(int j=1;j<=i;j++)
{
[Link]("* ");//to print the pattern in upper right
side
}
[Link]();
}
for(int i=3;i<=7;i+=2)
{
for(int j=1;j<=(13-(i*2-1));j++)
{[Link](" ");} //to print the spaces in left side in
downward side
for(int k=1;k<=i;k++)
[Link](" *");// to print pattern in downward side
[Link]();
}
}}
oUTPUT:
*******
*****
***
*
***
*****
*******

V.d.S.:

Variable Datatype Description


i int To print the rows in
pattern
j int To print the ‘*’ in the upper
half of pattern and blank
spaces in lower of pattern
k int To print the ‘*’ in lower half
of pattern
PROGRAM 8
Write a program to display the following pattern.
1
121
12321
1234321
123454321

SolUTIoN:
class pattern3
{
public static void main(String args[]){
for(int i=1;i<=5;i++)
{
for(int j=i;j<5;j++)//loop to print the left side spaces
{[Link](" ");}
for(int k=1;k<=i;k++)//loop to print the rows of numbers in
patterns after space
{[Link](k+" ");}
for(int d=i-1;d>0;d--)//loop to print the numbers after the
first set of numbers
{[Link](d+" ");}
[Link]();//to change line

}
}
}
oUTPUT:
1
121
12321
1234321
123454321

V.d.S.:

Variable Datatype Description


i int To run the loop to print 5
rows in pattern
j int To print the spaces in the
left side of pattern
k int To print the rows of one
set of numbers after the
left side spaces
d int To print the rows of
another set of numbers
after one set
PROGRAM 9
Write a program, to display the following pattern.
******
*****
****
***
**
*
**
***
****
*****
******

SolUTIoN:
class pattern{
public static void main(String args[])
{
for(int i=6;i>0;i--)
{
for(int j=1;j<=i;j++)//to print the upper half of pattern
{ [Link]("* ");}
[Link]();
}
for(int i=2;i<=6;i++)
{
for(int j=1;j<=i;j++)// to print of lower half of pattern
{ [Link]("* ");}
[Link]();// to change to next line
}
}
}
oUTPUT :
******
*****
****
***
**
*
**
***
****
*****
******

V.d.S.:

Variable Datatype Description


i int To print the rows of
pattern
j int To print the ‘*’ in rows in
pattern
PROGRAM 10
A triangular number is formed by the addition of consecutive
integers starting with 1. For example,
1+2=3
1+2+3=6
1+2+3+4 = 10
1+2+3+4+5=15
Thus, 3, 6, 10, 15, are triangular numbers. Write a program in Java
to display all the triangular numbers from 3 to n, taking the value
of n as an input.

SolUTIoN:
import [Link].*;
class triangularnumber{
static void triangular(int n){
int sum=0;
for(int i=1; ; i++)//used this loop to find the sum of natural
numbers to compare with n
{
sum=sum+i;
if(sum==n)//to compare the sum with n for traingular no.
{ [Link](n+" is a TRIANGULAR number");
break;}
if(sum>n)//in case the value of sum exceeds n
{[Link](n+" is not a TRIANGULAR number");
break;}
}}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter a number to be tested :");
int n=[Link]();
triangular(n);
}
}
oUTPUT:
Example 1-
Enter a number to be tested :
28
28 is a TRIANGULAR number

Example 2-
Enter a number to be tested :
35
35 is not a TRIANGULAR number

V.d.S.:

Variable Datatype Description


n int To input a number to
check
sum int To find the sum of natural
numbers in ‘i’ iterations
i int To run the loop to
increment the value of
natural numbers for sum
PROGRAM 11
A Twin prime are those numbers which are prime and having a
difference of two ( 2 ) between the two prime numbers. In other
words, a twin prime is a prime that has a prime gap of two. Write a
program to input a number and check for Twin Prime number in
Java.
Ex: (3, 5), (5, 7), (11, 13), (17, 19)

SolUTIoN:
import [Link].*;
class twinprime{
public static boolean prime(int n)//method to check if a number is
prime
{
int c=0;
for(int i=1;i<=n;i++) {
if(n%i==0)
c++;}
if (c==2)
return true;
else return false;
}
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a prime number ");
int n=[Link]();
if(prime(n)){
if(prime(n+2))//to check if the number +2 n is prime or not
[Link]("Twin primes : "+n+" "+(n+2));
if(prime(n-2))//to check if the number -2 n is prime or not
[Link]("Twin primes : "+n+" "+(n-2));
if(!prime(n-2)&&!prime(n+2))//if both numbers are not prime
[Link](n +" has no Twin primes");
} else
[Link]("It is not prime number \nEnter a prime
number");
}
}
oUTPUT:
Example 1-
Enter a prime number
5
Twin primes : 5 7
Twin primes : 5 3

Example 2-
Enter a prime number
27
It is not prime number
Enter a prime number

Example 3-
Enter a prime number
31
Twin primes : 31 29

V.d.S.:

Variable Datatype Description


n int To input a number to
check if it has twin primes
i int To run the loop to find the
number of factors n has
c int To count the number of
factors ‘n’ has
PROGRAM 12
Write a program to input two number and print (LCM)Least
Common Multiple Program in Java.

SolUTIoN:
import [Link].*;
class lcm{
public static void main(String[] args) {
Scanner sc =new Scanner([Link]);
[Link]("Enter first number :");
int n1=[Link]();
[Link]("Enter second number :");
int n2=[Link]();
int LCM =0;
for(int i=1; ;i++)
{
if((n1*i)%n2==0)// to find the first number that is
divisible by both numbers
{ LCM =n1*i;
break ; //to terminate the loop as soon as the number is found
}}
[Link]("LCM of "+n1+" and "+n2 +" is "+LCM) ;} }

oUTPUT:
Enter first number :
345
Enter second number :
24
LCM of 345 and 24 is 2760
V.d.S.:

Variable Datatype Description


n1 int To input the first number
n2 int To input the second
number
LCM int To store the lcm of the 2
numbers
i int To run the loop to find the
lcm
PROGRAM 13
A private Cab service company provides service within the city at
the following rates:
AC CAR NON AC CAR
Upto 5 km 150/- 120/-
Beyond 5 km 10/-per km 8/-per km

Design a class CabService with the following description:


Member
variables /data
members :
String To store the type of car (AC or NON_AC)
car_type
double km To store the kilometer travelled
double bill To calculate and store the bill amount
Member
methods:
CabService() Default constructor to initialize data members.
String data members to “” and double data
members to 0.0.
void accept () To accept car type and km (using Scanner class
only).
void calculate To calculate the bill as per the rules given above.
()
void display() To display the bill as per the following format-
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:

Create an object of the class in the main method and invoke the
member methods.

SolUTIoN:
import [Link].*;
class CabService{
String car_type ;
double km ;
double bill;
CabService(){ //it is the constructor to initialise members
car_type = "";
km=0.0;
bill = 0.0;
}
void accept(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the type of car \"AC\" or \"NON_AC\"
:");//to specify the type of cars
car_type= [Link]();
[Link]("Enter the number of kilometers travelled
:");
km =[Link](); //to store the distance travelled
}
void calculate()//to calculate the bill
{
if(car_type.compareToIgnoreCase("AC")==0)//used string to check
if car is ac or not
{
if(km<=5.00)
bill = 150;
else
bill =150+(km-5.0)*10;
}
else if(car_type.compareToIgnoreCase("NON_AC")==0)
{
if(km<=5.0)
bill=120;
else
bill = 120+(km-5.0)*8;
}
else //in case the input is neither ac nor nonac
[Link]("Invalid car type ") ; }
void display(){
[Link]("CAR TYPE: "+car_type);
[Link]("KILOMETER TRAVELLED: "+km);
[Link]("TOTAL BILL: Rs."+bill);
}
public static void main(String[] args) {
CabService ob =new CabService();
[Link]();
[Link]();
[Link]();
}
}

oUTPUT:
Example 1-
Enter the type of car "AC" or "NON_AC" :
ac
Enter the number of kilometers travelled :
45
CAR TYPE: ac
KILOMETER TRAVELLED: 45.0
TOTAL BILL: Rs.550.0

Example 2-
Enter the type of car "AC" or "NON_AC" :
non_ac
Enter the number of kilometers travelled :
45
CAR TYPE: non_ac
KILOMETER TRAVELLED: 45.0
TOTAL BILL: Rs.440.0
V.d.S.:

Variable Datatype Description


car_type String To input the type of car as
AC or NON AC
km double To input the number of
kilometers travelled
bill double To store the amount due
for payment
PROGRAM 14
A happy number is a number in which the eventual sum of the square
ofthe digits of the number is equal to 1.
Example: 28=(2) 2+(8) 2 =4+64=68
68=(6)2+ (8) 2 =36+64=100
100= (1) 2 +(0) 2 +(0) 2 =1+0+0=1
Hence, 28 is a happy number.

Design a class Happy to check if a given number, is a happy number.


Some of the members of the class are given below:
Class name : happy
Data members/instant
variables :
n: Stores the number.
Member functions:
happy() : Constructor to assign 0 to n.
void getnum(int nn) : To assign a parameter value to the
number n = nn.
int sum_sq_digits(int x) : Returns the sum of the square of the
digits of the number x.
void is_happy() : Checks if the given number is a happy
number by calling the function
sum_sq_digits(int) and displays an
appropriate message

Specify the class Happy giving details of the constructor(), void


getnum(int), int sum_sq_digits(int) and void is happy().
Also define a main() function to create an object and call the
methods to check the happy number.
SolUTIoN:
import [Link].*;
class happy //a number is happy if the eventual sum of it's square of
digits is 1
{
int n;
happy()// constructor to initialize n
{
n=0;
}
void getnum(int nn)// to take input in n
{
n=nn;
}
int sum_sq_digits(int x)// to find square of sum of digits of number
{
int sum=0;
for(int i=x;i>0;i/=10)
sum =sum+(int)[Link](i%10,2);
return sum;
}
void ishappy()// method to check if input number is happy or not
{
while(n>=9){
n = sum_sq_digits(n);
}
if(n==1)
[Link]("It is a HAPPY Number");
else
[Link]("It is not a HAPPY Number");
}
public static void main(String args[]) {
Scanner sc=new Scanner([Link]);
[Link]("Enter a number :");
int n =[Link]();
happy ob =new happy();
[Link](n);
[Link](); }
}
oUTPUT:
Example 1 -
Enter a number :
49
It is a HAPPY Number
Example 2 -
Enter a number :
61
It is not a HAPPY Number

V.d.S.:

Variable Datatype Description


n int To input a number to test
nn int To store the copy of n
sum int To store the sum of square of
digits of parameter
i int To run the loop to extract
digits from parameter
PROGRAM 15
Create a class PrimePalindrome -(A prime palindrome integer is a
positive integer which is prime as well as a palindrome) with the
following specifications:
Class Name PrimePalindrome
Member functions :
boolean isPrime (int n) returns 'true' if number is
Prime and 'false' if it is not
boolean isPalin (int n) returns 'true' if number is
Palindrome and 'false' if not.
void show (int n1, int n2 ) accepts the lower and upper
limit and prints all the Pal Prime
numbers in between that range
by sending each numbers in the
range to both the functions is
Prime( ) and is Palin( ).

Write a main method to create object of a class and call the above
member method.

SolUTIoN:
import [Link].*;
class PrimePalindrome{
boolean isPrime(int n)//function to check if the number is prime or
not
{
int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
++c;
if(c==2)
return true;
else
return false;}
boolean isPalin(int n)// function to check if the number is palindrome
or not
{
int rev=0;
for(int i=n;i>0;i/=10)//loop to reverse n
rev=rev*10+i%10;
if(rev==n)// to check if n is palindrome or not
return true;
else
return false;
}
void show(int n1,int n2)
{
[Link]("PrimePalindrome numbers in the given range are :
");
int c=0;
for(int i=n1;i<=n2;i++)
{
if(isPrime(i)&&isPalin(i))
{ [Link](i+" , ");
c++;}
}
if(c==0)
[Link]("There are no PrimePalindrome numbers in range");
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the lower limit : ");
int l=[Link]();
[Link]("Enter the upper limit : ");
int u=[Link]();
PrimePalindrome ob= new PrimePalindrome();
[Link](l,u);
}
}
oUTPUT:
Enter the lower limit : 100
Enter the upper limit : 200
PrimePalindrome numbers in the given range are :
101 , 131 , 151 , 181 , 191 ,

V.d.S.:

Variable Datatype Description


n int Argument to test for prime
and palindrome
c int To count the number of
factors of n
i int To run the loop to find the
factors of n and extract digits
from n
rev int To store the reversed number
of n
l int To store the lower limit for
range
u int To store the upper limit for
range
PROGRAM 16
Define a class to accept a number and check whether it is a SUPERSPY
number or not. A number is called SUPERSPY if the sum of the digits
equals the number of the digits.
Example 1:
Input: 1021
output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1]
NUMBER OF DIGITS = 4
Example2: Input: 125
output: Not an SUPERSPY number [1+2+5 is not equal to 3]

SolUTIoN:
import [Link].*;
class superspy{
public static void check(int n){
int l=0,sum=0;
for(int i=n;i>0;i/=10)// to find the sum of digits
{
sum=sum+i%10;
++l;
}
if(l==sum)
{
[Link]("Output : SUPERSPY number[SUM OF THE DIGITS = ");

for(int i=0;i<l-1;i++)// to print in the format if condition gets


true
[Link]([Link](n).charAt(i)+"+");
[Link]([Link](n).charAt(l-1)+"="+sum+"]");
[Link]("NUMBER OF DIGITS = "+l);
}
else{
[Link]("Output : Not an SUPERSPY number [");
for(int i=0;i<l-1;i++)// to print in the format if condition gets
false
[Link]([Link](n).charAt(i)+"+");
[Link]([Link](n).charAt(l-1)+"="+sum+" is not
equal to "+l+" ]");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Input : ");
int n=[Link]();
check(n);// calling the method to check for SUPERSPY number
}
}

oUTPUT:
Example 1-
Input : 201
Output : SUPERSPY number[SUM OF THE DIGITS = 2+0+1=3]
NUMBER OF DIGITS = 3
Example 2-
Input : 345
Output : Not an SUPERSPY number [3+4+5=12 is not equal to 3 ]

V.d.S.:

Variable Datatype Description


n int To input a number to test
l int To store the length of n by
converting n to string
sum int To store the sum of digits of n
i int To run the loop to extract
digits from n
PROGRAM 17
A student appearing for the ICSE/ISC examination will be given an
index number, which is of the following format:
Number of 7 digits/number of 3 digits.
The first digit represents ICSE(1) or ISC(2), the next two digits
represent the year, the next four digits represent the centre number,
the last 3 digits represent the index number.
Example:
1244311/204
Class: 10
Year: 24
Centre number: 4311
Index number: 204
Example:
2259856/107
Class: 12
Year: 25
Centre number: 9856
Index number:107
Define a class to accept the student index number as a String and
print his/her details as above.
SolUTIoN:
import [Link].*;
class indexnumber{
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the index number in following format:
\"xxxxxxx/xxx\" "); // to specify the format for entry of index no.
String s=[Link]();
if([Link]()==11&&[Link](7)=='/')// to check if the entered
string is valid as index number
{if([Link](0)=='1')
[Link]("Class: 10");
else if([Link](0)=='2')
[Link]("Class: 12");
else
{[Link]("Invalid index number");
[Link](0);}
// to print the different components of index no.
[Link]("Year: "+[Link](1,3));
[Link]("Center Number: "+[Link](3,7));
[Link]("Index Number: "+[Link](8));
}}
}

oUTPUT:
Example 1-
Enter the index number in following format: "xxxxxxx/xxx"
1257084/896
Class: 10
Year: 25
Center Number: 7084
Index Number: 896

Example 2-
Enter the index number in following format: "xxxxxxx/xxx"
2256789/908
Class: 12
Year: 25
Center Number: 6789
Index Number: 908

Example 3-
Enter the index number in following format: "xxxxxxx/xxx"
5467784/890
Invalid index number

V.d.S.:

Variable Datatype Description


S String To input a String to test for
the valid index number
PROGRAM 18
Define a class to accept 20 number and display the sum of even and
odd number differently, without using array.

SolUTIoN:
import [Link].*;
class oddeven{
public static void main(String args[]){
Scanner sc= new Scanner([Link]);
[Link]("Enter 20 numbers :");
int evsum=0,odsum=0;
for(int i=1;i<=20;i++)//loop to input 20 numbers by using same
variable n
{
int n=[Link]();//using same variable 20 times
if(n%2==0)//condition to check even numbers
evsum=evsum+n;
else
odsum=odsum+n;//sum if number is odd
}
[Link]("Sum of odd numbers = "+odsum );
[Link]("Sum of even numbers = "+evsum);}
}

oUTPUT:
Enter 20 numbers :
34
57
69
64
24
90
45
60
69
67
31
34
25
94
60
71
75
30
47
20
Sum of odd numbers = 556
Sum of even numbers = 510

V.d.S.:

Variable Datatype Description


n int To input the 20 numbers
by using the same variable
by iteration construct
i int To run the loop 20 times to
input 20 numbers using ‘n’
evsum int To store the sum of even
numbers
odsum int To store the sum of odd
numbers
PROGRAM 19
W.A.P. to accept a number and check number is Tech or not. A
number is said to be a tech number, when an even digit number is
divided into exactly two parts and the square value of the sum of
those two numbers is equal to the original number.
Ex: 3025 30+25=55
(55)2 = 3025, is equal to original number.

SolUTIoN:
import [Link].*;
class tech{
public static void techno(int n)
{
int l=[Link](n).length();// to convert no. to string to
find its length
int sum=0;
if(l%2==0)// condition to check if the number contains even number
of digits
{
int h=n%(int)[Link](10,l/2);//to divide number in 2 halves
int r=n/(int)[Link](10,l/2);
sum=(int)[Link](h+r,2);// to find square of sum of 2 halves
}
else{
[Link](n+" is not an Even digit number");
[Link](0);
}
if(sum==n)
[Link](n+" is a Tech number");
else
[Link](n+" is not a Tech number");
}
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
[Link]("Enter a number containing even digits : ");
int n=[Link]();
techno(n);
}
}

oUTPUT:
Example 1-
Enter a number containing even digits :
2025
2025 is a Tech number

Example 2-
Enter a number containing even digits :
5879
5879 is not a Tech number

Example 3-
Enter a number containing even digits :
45679
45679 is not an Even digit number

V.d.S.:

Variable Datatype Description


n int To input a number to test
l int To store the length of n by
converting it into String
sum int To store the sum of the 2
halves of n
h int To store the first half of n
r int To store the second half of
n
PROGRAM 20
Define a class ElectricBill with the following specifications:
Class Name : ElectricBill
Data member :
String n : to store the name of the customer
double bill : to store the amount to be paid
int units to store the number of units consumed
Member methods:
void accept( ) : to accept the name of the customer and
number of units consumed
void calculate( ) : to calculate the bill as per the following tariff:
Number of units Rate per unit
First 100 units Rs. 2.00
Next 200 units Rs. 3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of
units consumed is above 300 units.
void print ( ): To print the details as follows –
Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………

Write a main method to create an object of the class and call the
above member methods.
SolUTIoN:
import [Link].*;
class ElectricBill{
String n;
int units;
double bill;
void accept(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of customer :");
n=[Link]();
[Link]("Enter the number of units consumed :");
units = [Link]();
}
void calculate()// to calculate the bill
{
if(units<=100) //conditions to calculate the
bill as per consumption
bill = units *2;
else if(units>100&&units<=300)
bill =(units-100)*3+200;
else
{bill=(units-300)*5+800;
bill=bill+(2.5/100*bill);}
}
void print()// to print the final electricity bill of consumer
{
[Link]("Name of the customer: "+n);
[Link]("Number of units consumed: "+units);
[Link]("Bill amount: Rs."+bill);
}
public static void main(String[] args) {
ElectricBill ob = new ElectricBill();
[Link]();
[Link]();
[Link]();
}
}
oUTPUT:
Enter the number of customer :
Mayank Narayan Saini
Enter the number of units consumed :
567
Name of the customer: Mayank Narayan Saini
Number of units consumed: 567
Bill amount: Rs.2188.375

V.d.S.:

Variable Datatype Description


n String To store the name of the
person
units int To store the number of units
consumed
bill double To store the amount due

You might also like