Isc Proj Computer Science
Isc Proj Computer Science
Isc Proj Computer Science
Solution-
import java.util.*;
class primeFactors
{
int a=0;int factors[]= new int[1000];
while (p.factors[x]!=0)
{
if (p.factors[x+1]!=0)
System.out.print (p.factors[x++]+" x ");
else
System.out.print (p.factors[x++]+" x 1"); } }
public void printPrime(int n)// prints all prime numbers less than n
{
for(int x=0;x<n+1;x++)
System.out.println(nextPrime() + ",");
}
if (n==1)factors[0]=1;
int i=0;
while (n!=1)
{ int p=pr.nextPrime();
while (n%p==0)
{ n/=p;
factors[i++]=p; } } } }
Outputs-
Solution-
import java.util.*;
class SmithNumbers extends primeFactors
{
static SmithNumbers obj=new SmithNumbers();
public static void mainSmith(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print ("Input the number to be checked\t:");
int input=s.nextInt();
if(obj.isSmith(input))
System.out.println ("Smith");
else System.out.println ("Not Smith");
}
Boolean isSmith(int n)
{ int i=0;
int SODf=0;
primeFactorise(n);
int SODn=sumOfDigits(n);
while(i<factors.length)
SODf+=sumOfDigits(factors[i++]);
if (SODf==SODn)
return true;
else return false;
}
int sumOfDigits(int in)
{ int sum=0;
while (in>0)
{
sum+=in%10;
in/=10;
} return sum; }}
Outputs-
Smith
Not Smith
PROGRAM-3
A magic number is a number in which the eventual sum of digits of the number is
equal to 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:
Specify the class Magic giving details of the constructor, void getnum(int), int
Sum_of_digits(int) and void isMagic().
Solution-
import java.io.*;
public class Magic
{long n;
Magic()
{ n=0; }
long sumOfDigits(long a)
{ long s=0;
while (a!=0)
{
s+=a%10;
a/=10;
}return (s);
}
void isMagic()
{ long sum=0;
sum=sumOfDigits (n);
while (sum>9)
{
sum=sumOfDigits (sum);
}
System.out.println ("In a magic number, the eventual sum of digits is 1.");
System.out.println ("Since, the eventual sum of digits of the number "+n+" is
"+sum+",");
if (sum==1)
System.out.println (" therefore, it is a magic number :-)");
else
System.out.println (" therefore it is not a magic number :-(");
}
Output
This program checks whether a given number is magic number or not.
bookper day. As per the rules of the library, a book can be retained for 7 days
without any fine. If the book is returned after 7 days, a fine will also be charged for
the excess days as per the chart given below:
Design a class Library and another class Compute to perform the task. The details
of the two classes are given below:
Class name : Library
Member functions:
Member functions:
Compute(…) : parameterized constructor to assign
values to data members of both the
classes.
void fine() : calculates the fine for the excess days
void display() : display the book details along with
the
number of days, fine and the total amount
to be paid. Total amount is calculated as:
(2% of price of book*total no. of days)+fine.
Specify the class Library giving details of the constructors and void show(). Using
the concept of Inheritance, specify the class Compute giving details of constructor,
void fine() and void display() function.
Solution
import java.io.*;
import java.util.*;
class Library
{
String name,author;
float price;
Library(String name,String author,float price)
{
this.name=name;
this.author=author;
this.price=price;
}
void show()
{
System.out.println("Book Name\t: "+name);
System.out.println("Book Author\t: "+author);
System.out.println("Book Price\t: Rs."+price+"/-");
}
}
void fine()
{
if(d>=1 && d<=5)
fine=2f*d;
else if(d>=6 && d<=10)
fine=5*2f+3f*(d-5);
else
fine=5*2f+5*3f+5f*(d-10);
}
void display()
{
show();
System.out.println("No. of days\t:"+d+" days");
System.out.println("Fine\t: Rs."+fine+"/-");
System.out.println("Total amount\t: Rs."+((.02f*price*d)+fine)+"/-");
}
Output
Input Price : 45
Fine : Rs.70.0/-
Design a program to accept the amount from the user and display the
break-up in descending order of denominations. (i,e preference should
be given to the highest denomination available) along with the total
number of notes. [Note: only the denomination used should be displayed].
Also print the amount in words according to the digits.
Example 1:
INPUT: 14836
OUTPUT: ONE FOUR EIGHT THREE SIX
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
50 X 1 =50
5 X 1 =5
1 X 1 =1
EXAMPLE :
INPUT: 235001
OUTPUT: INVALID AMOUNT
Solution
import java.io.*;
class Bank
{
int rev=0,amount,dummy;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void intake() throws IOException
{
System.out.println("Enter the Amount:");
amount=Integer.parseInt(br.readLine());
if(amount >99999)
{
System.out.println("Invalid Amount...");
return;
}
dummy=amount;
while(dummy >0)
{
rev=rev*10+dummy%10;
dummy=dummy/10;
}
System.out.print("Amount in words :");
while(rev >0)
{
switch(rev%10)
{
case 0:
System.out.print(" ZERO");
break;
case 1:
System.out.print(" ONE");
break;
case 2:
System.out.print(" TWO");
break;
case 3:
System.out.print(" THREE");
break;
case 4:
System.out.print(" FOUR");
break;
case 5:
System.out.print(" FIVE");
break;
case 6:
System.out.print(" SIX");
break;
case 7:
System.out.print(" SEVEN");
break;
case 8:
System.out.print(" EIGHT");
break;
case 9:
System.out.print(" NINE");
break;
}
rev=rev/10;
}
System.out.println("\nDENOMINATORS:\n");
rev=amount/1000;
if(rev!=0)
System.out.println("1000 X " + rev + " = " + rev*1000);
amount=amount%1000;
rev=amount/500;
if(rev!=0)
System.out.println("500 X " + rev + " = " + rev*500);
amount=amount%500;
rev=amount/100;
if(rev!=0)
System.out.println("100 X " + rev + " = " + rev*100);
amount=amount%100;
rev=amount/50;
if(rev!=0)
System.out.println("50 X " + rev + " = " + rev*50);
amount=amount%50;
rev=amount/20;
if(rev!=0)
System.out.println("20 X " + rev + " = " + rev*20);
amount=amount%20;
rev=amount/10;
if(rev!=0)
System.out.println("10 X " + rev + " = " + rev*10);
amount=amount%10;
rev=amount/5;
if(rev!=0)
System.out.println("5 X " + rev + " = " + rev*5);
amount=amount%5;
rev=amount/2;
if(rev!=0)
System.out.println("2 X " + rev + " = " + rev*2);
amount=amount%2;
rev=amount/1;
if(rev!=0)
System.out.println("1 X " + rev + " = " + rev*1);
}
}
Output
DENOMINATORS:
1000 X 25 = 25000
500 X 1 = 500
100 X 1 = 100
20 X 2 = 40
5X1=5
2X1=2
1X1=1
PROGRAM-6
Given the two positive integers p and q, where p < q. Write a program to determine
how many kaprekar numbers are there in the range between 'p' and 'q' (both
inclusive) and output them.
About 'kaprekar' number:
A positive whole number 'n' that has 'd' number of digits is squared and split into
2 pieces, a right hand piece that has 'd' digits and a left hand piece that has
remaining 'd' or 'd-1' digits. If sum of the pieces is equal to the number then
it's a kaprekar number.
SAMPLE DATA:
INPUT:
p=1
Q=1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:
1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:7
Solution
import java.io.*;
class karpekar
{ int i,p,q,c=0;
int num;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{ System.out.println("Enter the Lower Range:");
p=Integer.parseInt(br.readLine());
System.out.println("Enter the Upper Range:");
q=Integer.parseInt(br.readLine());
if(p >=q)
{ System.out.println("Wrong Entry...");
return; }
System.out.println("THE KAPREKAR NUMBERS ARE:");
for(i=p;i<=q;i++)
{ show(i); }
System.out.println("\nFREQUENCY OF KAPREKAR NUMBERS IS:"+c);
}
// 'rev' holds the right part in reverse order and 'no' holds the left part
rev=reverse(rev);
if((rev+no)==x)
{ System.out.print(" "+x);
c++; } }
Output
Enter the Lower Range:
1
Enter the Upper Range:
500
THE KAPREKAR NUMBERS ARE:
1 9 45 55 99 297
FREQUENCY OF KAPREKAR NUMBERS IS:6
PROGRAM-7
Specify the class Point giving details of the constructor(), member functions
voidreadpoint(), Point midpoint(Point, Point) and void displaypoint() along with
the main() function to create an object and call the functions accordingly to
calculate the midpoint between any two given points.
Solution
import java.io.*;
class Point
{ double x,y;
Point()
{ x=0;
y=0; }
void displaypoint()
{ System.out.println ("("+x+","+y+")"); }
Output
value of x :2
value of y :3
value of x :4
value of y :5
(3.0,4.0)
PROGRAM-8
A super class Worker has been defined to store the details of a worker. Define a
subclass Wages to compute the monthly wages of the worker. The
details/specifications of both classes are given below:
Class Name Worker
Data Members/Instance
Variables:
Name Stores the name of the worker.
Basic Stores the basic pay in decimals.
Member Functions:
Worker(…) Parameterised constructor to assign
values to data members.
void display() Displays the worker's details.
Specify the class Worker, giving details of the constructor and void display().
Using the concept of inheritance. specify a class Wages giving details of the
constructor, double overtime() and void display().Write an appropriate main()
function.
Solution
class worker
{
String Name;
Double Basic;
void display()
{
System.out.println ("Name\t: "+Name+"\nBasic Pay\t: "+Basic);
}
double overtime(){
return (hrs*rate);
}
void display(){
Wages o=new Wages(hrs,rate,Name,Basic);
wage=o.overtime()+Basic;
super.display();
System.out.println ("Wage\t: "+wage);
}
}
Output
Wage : 6000.0
PROGRAM-9
Solution
import java.io.*;
class PerfectNumber
{
int no,sum=0,i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
For example:
0201103311=10*0+9*2+8*0+7*1+6*1+5*0+4*3+3*3+2*1+1*1=55
This is a valid ISBN
007462542X=10*0+9*0+8*7+7*4+6*6+5*2+4*5+3*4+2*2+1*10=176
This is a valid ISBN
Test Data:-
import java.io. *;
class Q19
String s1=obj.readLine();
int l=s1.length();
if(l!=10)
System.out.println("Sum=invalid input");
System.exit(0);
int n=0;
int i=0;
for(i=0;i<10;i++)
ch=s1.charAt(i);
if(ch=='X')
a[i]=10;
else
n=ch-48;
a[i]=n;
}
int s=0;
for(i=0;i<10;i++)
s=s+(a[i]*(10-i));
System.out.println("Sum="+s);
if(s%11==0)
else
Output-