You are on page 1of 106

Class SMITH

1/2

import java.io.*;
public class SMITH
{
int n;
void input()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("ENTER A NUMBER");
n=Integer.parseInt(d.readLine());
}
int sumDigits()
{
int m=n,d=0,s=0;
while(m>0)
{
d=m%10;
s+=d;
m/=10;
}
return s;
}
boolean prime(int m)
{
int d=m/2,c=0;
for(int i=2;i<=d;i++)
{
if(m%i==0)
c++;
}
if(c==0)
return true;
else
return false;
}
int sumPrime()
{
int m=n,s=0,d=0,j=0;
while(m!=1)
{
for(int i=2;i<=n;i++)
{
if(prime(i)&&m%i==0)
{
j=i;
while(j>0)
{
d=j%10;
Jan 20, 2015 1:55:00 PM

Class SMITH (continued)

2/2
s+=d;
j/=10;
}
m=m/i;
break;

}
}
}
return s;
}
void main()throws IOException
{
SMITH s=new SMITH();
s.input();
if(s.sumDigits()==s.sumPrime())
System.out.println("SMITH NUMBER");
else
System.out.println("not a SMITH NUMBER");
}
}

Jan 20, 2015 1:55:00 PM

ENTER
666
SMITH
ENTER
908
not a

A NUMBER
NUMBER
A NUMBER
SMITH NUMBER

Class fibonacci

1/1

//fibonacci series
import java.io.*;
class fibonacci
{
int a,b,c,limit;
fibonacci()
{
a=0;
b=1;
c=1;
}
void input()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter limit of sequence");
limit=Integer.parseInt(d.readLine());
}
//method to find nth fibonacci number using recursive technique
int fib(int n)
{
if(n==1)
return a;
if(n==2)
return b;
return (fib(n-1)+fib(n-2));
}
void generateFibSeries()
{
for(int i=1;i<=limit;i++)
System.out.print(fib(i)+",");
System.out.println();
}
void main()throws IOException
{
fibonacci f=new fibonacci();
f.input();
f.generateFibSeries();
}
}

Nov 27, 2014 7:39:34 PM

Enter limit of sequence


5
0,1,1,2,3,
Enter limit of sequence
10
0,1,1,2,3,5,8,13,21,34,
Enter limit of sequence
11
0,1,1,2,3,5,8,13,21,34,55,
Enter limit of sequence
7
0,1,1,2,3,5,8,

Class factorial

1/1

//to find factorial using recursion


import java.io.*;
class factorial
{
int n,f;
factorial()
{
n=0;
f=0;
}
//finding factorial of n using recursive technique
int fact(int num)
{
if(num==0)
return 1;
return (num*fact(num-1));
}
void getnumber(int x)
{
n=x;
f=fact(n);
System.out.println("factorial of "+x+"="+f);
}
void main()throws IOException
{
factorial fac=new factorial();//object creation
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a number");
n=Integer.parseInt(d.readLine());
fac.getnumber(n);
}
}

Nov 27, 2014 7:43:38 PM

Enter a number
5
factorial of 5=120
Enter a number
10
factorial of 10=3628800
Enter a number
11
factorial of 11=39916800
Enter a number
2
factorial of 2=2
Enter a number
1
factorial of 1=1
Enter a number
0
factorial of 0=1

Class poweris

1/1

//to find n raised to m using recursive technique


import java.io.*;
class poweris
{
int n,m;
double p;
poweris()
{
n=0;
m=0;
p=0.0;
}
double power(int n,int m)
{
if(m<0)
{
int x=-m;
return (1/(n*power(n,x-1)));
}
if(m>0)
return (n*power(n,m-1));
return 1.0;
}
void findresult()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter base and exponent");
n=Integer.parseInt(d.readLine());
m=Integer.parseInt(d.readLine());
p=power(n,m);
}
void printresult()
{
System.out.println(n+" to the power "+m+" equals "+p);
}
void main()throws IOException
{
poweris po=new poweris();
po.findresult();
po.printresult();
}
}

Nov 27, 2014 7:45:54 PM

Enter base and


2
3
2 to the power
Enter base and
1
3
1 to the power
Enter base and
2
-2
2 to the power
Enter base and
8
-2
8 to the power

exponent

3 equals 8.0
exponent

3 equals 1.0
exponent

-2 equals 0.25
exponent

-2 equals 0.015625

Class Revstr

1/1

//to reverse string using recursion


import java.io.*;
class Revstr
{
String Str,Revst;
void getStr()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a string");
Str=d.readLine();
}
void recReverse(int i)
{
if(i>0)
{
Revst+=Str.charAt(i);
recReverse(i-1);
}
if(i==0)
Revst+=Str.charAt(0);
}
void check()
{
Revst="";
System.out.println("original "+Str);
recReverse(Str.length()-1);
System.out.println("reversed "+Revst);
int i=0;
for(i=0;i<Str.length();i++)
{
if(Str.charAt(i)!=Revst.charAt(i))
break;
}
if(i==Str.length())
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
void main()throws IOException
{
Revstr object=new Revstr();
object.getStr();
object.check();
}
}

Nov 27, 2014 7:46:39 PM

Enter a string
this is a jolly good place
original this is a jolly good place
reversed ecalp doog ylloj a si siht
Not Palindrome
Enter a string
computer retupmoc
original computer retupmoc
reversed computer retupmoc
Palindrome
Enter a string
78987
original 78987
reversed 78987
Palindrome

Class theseries

1/2

//to find primes from a list of data using recursion


import java.io.*;
class theseries
{
int limit;
int arr[]=new int[150];
theseries()
{
limit=0;
for(int i=0;i<150;i++)
arr[i]=0;
}
void readList()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter limit of array");
limit=Integer.parseInt(d.readLine());
System.out.println("enter elements of array");
for(int i=0;i<limit;i++)
{
System.out.println("enter element "+(i+1));
arr[i]=Integer.parseInt(d.readLine());
}
}
int isPrime(int x,int y)
{
if(x==1)
return 0;
if(y==x)
return 1;
if(x%y==0)
return 0;
return isPrime(x,y+1);
}
void printPrimes()
{
System.out.println("list of primes from the array");
for(int i=0;i<limit;i++)
if(isPrime(arr[i],2)==1)
System.out.print(arr[i]+" ");
System.out.println();
}
void main()throws IOException
Jan 19, 2015 7:06:07 AM

Class theseries (continued)

2/2

{
theseries ts=new theseries();
ts.readList();
ts.printPrimes();
}
}

Jan 19, 2015 7:06:07 AM

enter limit of array


5
enter elements of array
enter element 1
11
enter element 2
111
enter element 3
71
enter element 4
29
enter element 5
2
list of primes from the array
11 71 29 2
enter limit of array
3
enter elements of array
enter element 1
6
enter element 2
12
enter element 3
14
list of primes from the array

Class check

1/1

//to count number of words in a string starting with a capital letter


import java.io.*;
class check
{
String Str;
int w;
void inputString()throws IOException
{
Str=" ";
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a sentence");
Str+=d.readLine();
}
void counter(int p)
{
if((p+1)<Str.length())
{
if(Str.charAt(p)==32)
{
if(Str.charAt(p+1)>=65&&Str.charAt(p+1)<=90)
w++;
}
counter(p+1);
}
}
void Disp()
{
System.out.println("String entered"+Str);
System.out.println("Number of words starting with capital letter"+w)
;
}
void main()throws IOException
{
check c=new check();
c.inputString();
c.counter(0);
c.Disp();
}
}

Nov 27, 2014 7:47:38 PM

Enter a sentence
This Is a joLly Good daY.
String entered This Is a joLly Good daY.
Number of words starting with capital letter3
Enter a sentence
this Computer Project iS Created by Prasoon.
String entered this Computer Project iS Created by Prasoon.
Number of words starting with capital letter4
Enter a sentence
killing animals for fun has been banned
String entered killing animals for fun has been banned
Number of words starting with capital letter0

Class binary

1/1

//to convert binary(base 2) to decimal(base 10)


import java.io.*;
class binary
{
long bin;
long dec;
void readBinary()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a binary number");
bin=Long.parseLong(d.readLine());
}
long convertDec(long c)
{
if(bin==0)
return 0;
long d=bin%10;
bin=bin/10;
dec=convertDec(c+1)+d*(long)Math.pow(2,c);
return dec;
}
void show()
{
System.out.println("binary number entered "+bin);
System.out.println("Equivalent decimal form "+convertDec(0));
}
void main()throws IOException
{
binary b=new binary();
b.readBinary();
b.show();
}
}

Nov 27, 2014 7:48:50 PM

Enter a binary number


100
binary number entered 100
Equivalent decimal form 4
Enter a binary number
10101
binary number entered 10101
Equivalent decimal form 21
Enter a binary number
11111
binary number entered 11111
Equivalent decimal form 31
Enter a binary number
110110
binary number entered 110110
Equivalent decimal form 54

Class sum_series

1/1

//to find sum of series (x*x)/1+(x*x*x*x)/(2*2)+(x*x...6times)/(3*3*3)...n t


erms
import java.io.*;
class sum_series
{
int x;
int n;
double sum;
DataInputStream d=new DataInputStream(System.in);
sum_series()
{
x=0;
n=0;
sum=0.0;
}
void readLimit()throws IOException
{
System.out.println("Enter limit of series");
n=Integer.parseInt(d.readLine());
}
int getPower(int m,int p)
{
if(p>0)
return (m*getPower(m,p-1));
return 1;
}
void Sum()
{
for(int i=1;i<=n;i++)
{
sum+=((getPower(x,2*i)*1.0)/(getPower(i,i)*1.0));
}
System.out.println("Sum of Series "+sum);
}
void main()throws IOException
{
sum_series ss=new sum_series();
System.out.println("Enter variable x");
ss.x=Integer.parseInt(d.readLine());
ss.readLimit();
ss.Sum();
}
}

Nov 27, 2014 7:49:24 PM

Enter variable x
2
Enter limit of series
5
Sum of Series 11.698050370370371
Enter variable x
3
Enter limit of series
6
Sum of Series 112.16521125
Enter variable x
1
Enter limit of series
5
Sum of Series 1.291263287037037
Enter variable x
0
Enter limit of series
20
Sum of Series NaN
Enter variable x
2
Enter limit of series
12
Sum of Series 11.746671032880755

Class words

1/2

//to find number of words in a string


import java.io.*;
class words
{
String text;
int w;
words()
{
text=null;
w=0;
}
void Accept()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a sentence");
text=d.readLine();
}
int FindWords(int c)
{
if(c>0)
{
if(text.charAt(c)==32)
{
if(text.charAt(c-1)==32)
{
w=FindWords(c-1);
return w;
}
else
{
w=FindWords(c-1)+1;
return w;
}
}
w=FindWords(c-1);
return w;
}
w=1;
return w;
}
void Result()
{
System.out.println("Sentence entered"+text);
System.out.println("Number of words in the sentence "+FindWords(text
.length()-1));
}
void main()throws IOException
{
words wrds=new words();
Nov 27, 2014 7:49:43 PM

Class words (continued)

2/2

wrds.Accept();
text=" "+text;
wrds.Result();
}
}

Nov 27, 2014 7:49:43 PM

Enter a sentence
a terrible time awaited the country.
Sentence entereda terrible time awaited the country.
Number of words in the sentence 6
Enter a sentence
attack on sierra leone is at 1600 hrs. sharp
Sentence enteredattack on sierra leone is at 1600 hrs. sharp
Number of words in the sentence 9
Enter a sentence
This output is generated by BlueJ 3.0.0
Sentence enteredThis output is generated by BlueJ 3.0.0
Number of words in the sentence 7

Class matrix_image

1/2

import java.io.*;
class matrix_image
{
double arr1[][]=new double[15][15];
double arr2[][]=new double[15][15];
int m,n;//integers to store number of rows and columns
matrix_image()
{
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
arr1[i][j]=0;
arr2[i][j]=0;
}
}
m=0;
n=0;
}
//storing actual rows and columns
matrix_image(int mm,int nn)
{
m=mm;
n=nn;
}
void getmat()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("ENTER ELEMENT ON ADDRESS "+i+","+j);
arr1[i][j]=Integer.parseInt(d.readLine());
}
}
}
void mirror_image()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr2[i][n-1-j]=arr1[i][j];
}
}
}
void show()
{
Nov 27, 2014 8:32:05 PM

Class matrix_image (continued)

2/2

System.out.println("MATRIX INPUTED");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
System.out.println("MIRROR IMAGE OF THE MATRIX INPUTED");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr2[i][j]+" ");
}
System.out.println();
}
}
/*main not specified in question
* but still written to bring out
* the execution of program.
*/
void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter number of rows");
int m=Integer.parseInt(d.readLine());
System.out.println("Enter number of columns");
int n=Integer.parseInt(d.readLine());
matrix_image mi=new matrix_image(m,n);
mi.getmat();
mi.mirror_image();
mi.show();
}
}

Nov 27, 2014 8:32:05 PM

Enter number of rows


2
Enter number of columns
3
ENTER ELEMENT ON ADDRESS 0,0
2
ENTER ELEMENT ON ADDRESS 0,1
3
ENTER ELEMENT ON ADDRESS 0,2
4
ENTER ELEMENT ON ADDRESS 1,0
6
ENTER ELEMENT ON ADDRESS 1,1
10
ENTER ELEMENT ON ADDRESS 1,2
12
MATRIX INPUTED
2.0 3.0 4.0
6.0 10.0 12.0
MIRROR IMAGE OF THE MATRIX INPUTED
4.0 3.0 2.0
12.0 10.0 6.0
Enter number of rows
3
Enter number of columns
2
ENTER ELEMENT ON ADDRESS 0,0
3
ENTER ELEMENT ON ADDRESS 0,1
5
ENTER ELEMENT ON ADDRESS 1,0
7
ENTER ELEMENT ON ADDRESS 1,1
11
ENTER ELEMENT ON ADDRESS 2,0
13
ENTER ELEMENT ON ADDRESS 2,1
17
MATRIX INPUTED
3.0 5.0
7.0 11.0
13.0 17.0
MIRROR IMAGE OF THE MATRIX INPUTED
5.0 3.0
11.0 7.0
17.0 13.0

Class series

1/1

import java.io.*;
class series
{
int n;
int a,b,c;
void inputlimit()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Input Total number of terms");
n=Integer.parseInt(d.readLine());
}
void assign()
{
a=0;
b=1;
}
void generateseries()throws IOException
{
inputlimit();
assign();
if(n==1)
System.out.println(a);
if(n==2)
System.out.println(a+" "+b);
if(n>2)
{
System.out.print(a+" "+b+" ");
for(int i=3;i<=n;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
System.out.println();
}
}
}

Jan 17, 2015 11:43:20 AM

Class maxmin

1/1

import java.io.*;
class maxmin extends series
{
int ar[]=new int[100];
int s;
int gn,sn;
void readarray()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter size of array");
s=Integer.parseInt(d.readLine());
System.out.println("Enter the array");
for(int i=0;i<s;i++)
{
System.out.println("Enter the array element "+(i+1));
ar[i]=Integer.parseInt(d.readLine());
}
}
void findmaxmin()
{
gn=ar[0];
sn=ar[0];
for(int i=0;i<s;i++)
{
if(ar[i]>gn)
gn=ar[i];
if(ar[i]<sn)
sn=ar[i];
}
}
void printresults()throws IOException
{
series f=new series();
f.generateseries();
readarray();
findmaxmin();
System.out.println("Greatest number in array"+gn);
System.out.println("Smallest number in array"+sn);
}
}

Jan 17, 2015 11:45:39 AM

Class definingMain

1/1

import java.io.*;
class definingMain
{
void main()throws IOException
{
maxmin mm=new maxmin();
mm.printresults();
}
}

Jan 17, 2015 11:46:19 AM

Input Total number of terms


10
0 1 1 2 3 5 8 13 21 34
Enter size of array
5
Enter the array
Enter the array element 1
11
Enter the array element 2
13
Enter the array element 3
19
Enter the array element 4
2
Enter the array element 5
3
Greatest number in array19
Smallest number in array2

Class IscScores

1/1

import java.io.*;
class IscScores
{
int number[][]=new int[6][2];
IscScores()
{
for(int i=0;i<6;i++)
for(int j=0;j<2;j++)
number[i][j]=0;
}
void inputMarks()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
for(int i=0;i<6;i++)
{
for(int j=0;j<2;j++)
{
if(j==0)
{
System.out.println("Enter Subject Code");
number[i][0]=Integer.parseInt(d.readLine());
}
if(j==1)
{
System.out.println("Enter Marks");
number[i][1]=Integer.parseInt(d.readLine());
}
}
}
}
int point(int c)
{
if(c==100)
return 1;
return 10-c/10;
}
}

Jan 17, 2015 11:47:47 AM

Class BestFour

1/1

import java.io.*;
class BestFour extends IscScores
{
void bestsubjects()throws IOException
{
IscScores isc=new IscScores();
isc.inputMarks();
int s=0;
for(int i=0;i<6;i++)
{
s+=isc.point(isc.number[i][1]);
}
System.out.println("TOTAL POINTS"+s);
/*sorting in ascending order the
* marks obtained and acoordingly
* the subject codes
*/
int t=0;
for(int i=0;i<5;i++)
{
for(int j=i+1;j<6;j++)
{
if(isc.number[i][1]>isc.number[j][1])
{
t=isc.number[i][1];
isc.number[i][1]=isc.number[j][1];
isc.number[j][1]=t;
t=isc.number[i][0];
isc.number[i][0]=isc.number[j][0];
isc.number[j][0]=t;
}
}
}
for(int i=2;i<6;i++)
System.out.println(isc.number[i][0]);
}
}

Jan 17, 2015 11:48:13 AM

Class defineMain

1/1

import java.io.*;
class defineMain
{
void main()throws IOException
{
BestFour bf=new BestFour();
bf.bestsubjects();
}
}

Jan 17, 2015 11:48:35 AM

Enter
983
Enter
100
Enter
981
Enter
95
Enter
589
Enter
100
Enter
232
Enter
100
Enter
897
Enter
86
Enter
789
Enter
96
TOTAL
789
232
983
589

Subject Code
Marks
Subject Code
Marks
Subject Code
Marks
Subject Code
Marks
Subject Code
Marks
Subject Code
Marks
POINTS7

 

 
       
 
 
    


  
 
 

   
     


    !"#
 

$  
% &$  
% %' 
%'  
( )*+,-(
% & .  
& 
% & /012
% , 3&% , 3
& ,  
%  45&% 45     '
 &/677

 45&#, 
    '
8
 &/677

 &/61277

 45 6 4725 

%  & 45
 45& 4725
 4725& 
8
8
8
%'  
(!+,*+,-(
 &/677

%'  
9   +

9  45 :/


%'  
 45 2
%'  
((
8
%'  
((
%'  

8
8

 
  



 
  
  
  
  
 




 
  

 
 
  
 
 
 
 





 
      

       


   
 
!

 "#"#
 
$% &'()
 
!
*  &
+ *  &
+ $+ %
+  
$,( $%,%
& 
&$ -$%%
. $  //%
 "#/0
1
 
 $%
!
 
"#"#"0#"#
. $  //%
"#"# "#
23033
$4%
!
 
. $  //%
!
.$$/0%56%
!"#"#"70#"# // 1
//
1
7$%
//
//
1
+  
$,-89:;<8=>(?+-(++@AB<,/ /,B?(C,%
. $  //%
+  
$"72#"#/,,%
+  
$%
1
  $% &'()
 
!
$%

$%

 $%
1
1
 
   

 



  
 

!
!
    "
 


 
 "! "#"


 





 
  
    

  
   
    
 
 
     
    

  

   



  
   
 


  !"
 "


# $ 
   

# $ 
%$&  
    


   
%&   #'()  

$  %*#+,-./*&

  # 
#%  
0%&&  

 


 
%
 122222&    


$  %*'-.,-./*&
$  %*#+340# 45'-+.*&
 
6

 
%1!&

 7!897!   
 
 7!
6
$  %*'-.,-./*&
% 1!&

% 97!&  
    


 !/
$  %* :(;'*&
 


 7/
$  %* '+(*&
 


 </
$  %* .='*&
 


 >/
$  %* .?;((*&
 


 @/
$  %* A'-;*&
 


 B/
$  %* A#3(*&
 

 


   



 


 C/
$  %* $#D*&
 


 E/
$  %* $(3(+*&
 


 F/
$  %* (#G?.*&
 


 2/
$  %* +#+(*&
 

6
 7!
6
  !
$  %&
$  %*(+'5#+4.#'+/*&

 7!!! 

 7!!!  
 8 
% H !&
$  %*7!!! D * 8  8 * * 8 7!!!&

 
 97!!!

 B!! 

 B!!  
 8 
% H !&
$  %*B!! D * 8  8 * * 8 B!!&

 
 9B!!

 7!! 

 7!!  
 8 
% H !&
$  %*7!! D * 8  8 * * 8 7!!&

 
 97!!

 B! 

 B!  
 8 
% H !&
$  %*B! D * 8  8 * * 8 B!&

 
 9B!

 <! 

 <!  
 8 
% H !&
$  %*<! D * 8  8 * * 8 <!&

 
 9<!

 7! 

 7!  
 8 
% H !&
$  %*7! D * 8  8 * * 8 7!&

 
 97!
 

 


   




 B 

 B  
 8 
% H !&
$  %*B D * 8  8 * * 8 B&

 
 9B

 < 

 <  
 8 
% H !&
$  %*< D * 8  8 * * 8 <&

 
 9<

 7 

 7  
 8 
% H !&
$  %*7 D * 8  8 * * 8 7&
$  %*.'.40 *8
&   
 I  
  
$   %*.'.40 +-5J(; 'A +'.($ *8&
6
6

 

 




    
  
    
    
    
 

 

    




    
  
    
    
    
    
    
    
 
    

Class Student

1/1

package Inheritance.sample2;
import java.io.*;
public class Student
{
String std;
int roll;
DataInputStream d=new DataInputStream(System.in);
void accept() throws IOException
{
System.out.println("ENTER NAME");
std=d.readLine();
System.out.println("ENTER ROLL NUMBER");
roll=Integer.parseInt(d.readLine());
}
void show()throws IOException
{
System.out.println("NAME "+std);
System.out.println("ROLL NUMBER "+roll);
}
}

Jan 17, 2015 9:43:47 AM

Class Marks

1/1

package Inheritance.sample2;
import java.io.*;
public class Marks extends Student
{
protected float sub1;
protected float sub2;
DataInputStream d=new DataInputStream(System.in);
void getMarks(float x,float y)throws IOException
{
sub1=x;
sub2=y;
}
void dispMarks()throws IOException
{
System.out.println("SUB1 "+this.sub1);
System.out.println("SUB2 "+this.sub2);
}
}

Jan 17, 2015 9:44:12 AM

Class Result

1/1

package Inheritance.sample2;
import java.io.*;
public class Result extends Marks
{
float total;
DataInputStream d=new DataInputStream(System.in);
void Display()throws IOException
{
total=sub1+sub2;
show();
dispMarks();
System.out.println("TOTAL "+total);
}
}

Jan 17, 2015 9:44:24 AM

Class run

1/1

package Inheritance.sample2;
import java.io.*;
public class run
{
DataInputStream d=new DataInputStream(System.in);
void main()throws IOException
{
Result obj1=new Result();
obj1.accept();
float x,y;
System.out.println("ENTER MARKS IN SUB1 ");
x=Float.parseFloat(d.readLine());
System.out.println("ENTER MARKS IN SUB2");
y=Float.parseFloat(d.readLine());
obj1.getMarks(x,y);
obj1.Display();
}
}

Jan 17, 2015 9:44:32 AM

run2.main();
ENTER NAME
AMRENDRA
ENTER ROLL NUMBER
013
ENTER MARKS IN SUB1
99
ENTER MARKS IN SUB2
100
NAME AMRENDRA
ROLL NUMBER 13
SUB1

99.0

SUB2

100.0

TOTAL 199.0

Class basePro

1/1

package Inheritance.sample1;
import java.io.*;
public class basePro
{
float n1;
float n2;
DataInputStream d=new DataInputStream(System.in);
void enter()throws IOException
{
System.out.println("ENTER NUMBER");
n1=Float.parseFloat(d.readLine());
System.out.println("ENTER NUMBER");
n2=Float.parseFloat(d.readLine());
}
void show()
{
System.out.println("NUMBER 1="+n1);
System.out.println("NUMBER 2="+n2);
}
}

Jan 17, 2015 9:40:19 AM

Class dervPro

1/1

package Inheritance.sample1;
import java.io.*;
public class dervPro extends basePro
{
float result;
DataInputStream d=new DataInputStream(System.in);
void prod()throws IOException
{
enter();
result=n1*n2;
}
void disp()
{
System.out.println("MULTIPLICATION RESULT="+result);
}
}

Jan 17, 2015 9:40:40 AM

Class run

1/1

package Inheritance.sample1;

import java.io.*;
public class run
{
DataInputStream d=new DataInputStream(System.in);
public void main() throws IOException
{
dervPro obj1=new dervPro();
obj1.prod();
obj1.disp();
}
}

Jan 17, 2015 9:41:01 AM

run1.main();
ENTER NUMBER
6
ENTER NUMBER
3
MULTIPLICATION RESULT=18.0
run1.main();
ENTER NUMBER
12
ENTER NUMBER
8
MULTIPLICATION RESULT=96.0

Class Alpha

1/3

/**
* QUESTION
*
* design a class Alpha which enables the characters of a string to
* be arranged in alphabetical order.
* the details of the members of the class are given below:
*
* Data members:
* Str
:to store the original sentence
* Rstr
:to store the arranged letters
*
* Member functions:
* Alpha()
:dafault constructor
* void readString()
:to accept a sentence in uppercase
* void remove_spaces
:removes all the white spaces in the string
* void arrange
:to arrange the letters of the sentence in
*
alphabetical order using bubble sort
*
technique
* void disp()
:displays the original sentence
*
and the arranged string.
*/

import java.io.*;
public class Alpha
{
String Str;
String Rstr;

//CREATING DATA MEMBERS


//CREATING DAT MEMBERS

DataInputStream d=new DataInputStream(System.in);


public Alpha() //DEFAULT CONSTUCTOR
{
Str=""; //initialising data members
Rstr="";
}
void readString() throws IOException
/*
* method to input a string in upper case
*/
{
System.out.println("ENTER A SENTENCE IN UPPERCASE");
Str=d.readLine();
}
void remove_spaces()
/*
* method to remove spaces from the string
*/
{
int l=Str.length();
String s="";
Jan 16, 2015 10:37:52 PM

Class Alpha (continued)

2/3

for(int i=0;i<l;i++)
{
if(Str.charAt(i)!=' ')
{
s=(s + Str.charAt(i));
/*
* if character is not awhite space then we
* append it the string storing the new string
*/
}
}
Rstr=s;
/*
* giving the value of space remved string
* to data member Rstr
*/
}
void arrange()
{
int l=Rstr.length();
/*
finding length of the string without spaces
*/
char c[]=new char[l];
/*
creating a character type array with it's size equal to size
of space removed string to store every character of the string
*/
char t;
for(int i=0;i<l;i++)
{
c[i]=Rstr.charAt(i);
}
for(int i=0;i<l;i++)
{
for(int j=0;j<l-1;j++)
{
if(c[j]>c[j+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}
String a="";
for(int i=0;i<l;i++)
{
a=a+c[i];
Jan 16, 2015 10:37:52 PM

Class Alpha (continued)

3/3

}
Rstr=a;
/*
* giving the data member Rstr the value of the arranged string a
*/

}
void disp()
{
System.out.println("Original sentence\t" + Str);
/*
* displaying original string
*/
System.out.println("arranged string\t" + Rstr);
/*
* displaying arranged string
*/
}
public void main() throws IOException
{
Alpha o1=new Alpha();
/*
* creating object using default
* constructor
*/
o1.readString();
o1.remove_spaces();
o1.arrange();
o1.disp();
/*
* calling member functions using object
*/
}
//end of main
//end of class

Jan 16, 2015 10:37:52 PM

alpha1.main();
ENTER A SENTENCE IN UPPERCASE
SUN RISES FROM THE EAST
Original sentence
SUN RISES FROM THE EAST
arranged string AEEEFHIMNORRSSSSTTU
alpha1.main();
ENTER A SENTENCE IN UPPERCASE
AMIR KHAN IS A VERY GOOD ACTOR
Original sentence
AMIR KHAN IS A VERY GOOD ACTOR
arranged string AAAACDEGHIIKMNOOORRRSTVY

Class armstrong_number
/**
*
*
*
*
*
*
*
*
*/

1/1

QUESTION
Design a program in java to check whether a number
is an armstrong number or not.
An armstrong number is a number in which the sum
ofcubes of every digit of the number is equal to
the number itself.
eg. 153 = 1^3 + 5^3 +3^ 3 = 1+125+27 = 153

import java.io.*;
public class armstrong_number
{
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int m=n; //creating dummy variable
int s=0;
int r=0;
while(m>0)
{
r=m%10;
s=s+(r*r*r); //finding sum of cubes of digits of the number
m=m/10;
}
if(n==s)
/** checking if sum of cubes of every digit if the number
* is equal to the number itself and then displaying messages
* accordingly. */
{
System.out.println("Number is an armstrong number");
}
else
{
System.out.println("Number is not an armstrong number");
}
}
}

Jan 16, 2015 11:02:54 PM

armstron1.main();
enter the number
153
Number is an armstrong number
armstron1.main();
enter the number
523
Number is not an armstrong number

Class Change

1/2

package Recursion;
/**
* QUESTION
* PROGRAM TO CONVERT THE CASE OF A STRING
*
*/
import java.io.*;
public class Change
{
//data members
String str;
String newstr; //data members
int len;
//data members
DataInputStream d=new DataInputStream(System.in);
Change()
{
str="";
newstr="";
len=0;
}

//default constructor

//method to input the word


void inputword()throws IOException
{
System.out.println("ENTER A WORD");
str=d.readLine();
}
//function to convert case of a character
char caseconvert(char ch)
{
int k=0;
if(ch!=' ')
{
if(ch>92)
{
k=ch-32;//case conversion to capital
}
else
{
k=ch+32;//case conversion to small
}
}
else
Jan 1, 2003 1:13:37 AM

Class Change (continued)

2/2

{
k=ch;
}
return (char)k; //returning character of opp. case
}
//recursive function
void recchange(int n)
{
if(n<(str.length()))
{
newstr=newstr+caseconvert(str.charAt(n));
//extracting a character and changing its case simultaneously
recchange(n+1);
//recursive technique
}
}
void display()
{
System.out.println("ORIGINAL WORD
System.out.println("CHANGED WORD
//displaying output
}

"+str);
"+newstr);

void main()throws IOException


{
Change obj1=new Change();
//creating object
obj1.inputword();
//calling method to execute program
obj1.recchange(0);
//calling method to execute program
obj1.display();
//calling method to execute program
}
//end of main
}

Jan 1, 2003 1:13:37 AM

change1.main();
ENTER A WORD
CITy MONTESSori scHOOL
ORIGINAL WORD
CITy MONTESSori scHOOL
CHANGED WORD

citY montessORI SChool

change1.main();
ENTER A WORD
The HOunD OF basKERvilleS
ORIGINAL WORD
The HOunD OF basKERvilleS
CHANGED WORD

tHE hoUNd of BASkerVILLEs

Class Check

1/1

package Recursion;
import java.io.*;
public class Check
{
String Str;
int w;
DataInputStream d=new DataInputStream(System.in);
void InputString() throws IOException
{
System.out.println("ENTER STRING");
Str=" "+(d.readLine()).trim()+" 0";
}
void Counter(int n)
{
if(n>-1)
{
if(Str.charAt(n)==' ')
{
if(Str.charAt(n+1)=='A'||Str.charAt(n+1)=='E'||Str.charAt(n+
1)
=='I'||Str.charAt(n+1)=='O'||Str.charAt(n+1)=='U')
{
w++;
}
}
Counter(n-1);
}
}
void Disp()
{
Counter((Str.length()-1));
System.out.println("NO OF WORDS BEGINING WITH CAPITAL VOWELS ARE "+w
);
}
void main() throws IOException
{
Check ob1=new Check();
ob1.InputString();
ob1.Disp();
}
}

Jan 1, 2003 1:02:30 AM

check1.main();
ENTER STRING
ANDREW IS a good boy
NO OF WORDS BEGINING WITH CAPITAL VOWELS ARE 2
check1.main();
ENTER STRING
I am Out of work since last Month
NO OF WORDS BEGINING WITH CAPITAL VOWELS ARE 2

Class climbing_number
/**
*
*
*
*
*
*
*
*
*
*/

1/2

QUESTION
Design a program in java to check whether a number
is climbing number or not.Also print the digits of
the number.
A climbing number is a number whose digits are in
ascending order.
eg. 123 ,159 ,126 ,etc.

import java.io.*;
public class climbing_number
{
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int c=0;
//counter to store number of digits of the number
int a=n;
int z=n;
while(a>0)//loop to count no of digits
{
c=c+1;
//updating counter variable to count no. of digits
a=a/10;
}
int m[]=new int[c];
int y=0;
for(int i=0;i<c;i++)
{
y=z%10; //extracting digits
m[i]=y; //storing digits of the number in an array
z=z/10; //updating loop variable
}
for(int w=0;w<c;w++)
{
System.out.print(m[c-1-w]+"\t");
//printing the digits of the number
}
int p=0;
for(int e=0;e<(c-1);e++)
{
if(m[e]>m[e+1])
//comparing digits of the number
p=p+1;
//counting the no. of times where a digit is smaller than
//the next digit.(array is storing the digits fom last to first)
/*
Jan 16, 2015 10:22:36 PM

Class climbing_number (continued)

2/2

* we compare every digit eith the next digit.


* If every digit is smaller than the next digit
* then the number must be climbing.
* So we count the number of times the required condition
* is true for the number .If it is 1 less than the total
* number of digits then the number must be clmbing because
* we have to compare the digits c-1 times .
* So if every time it is true then our condition is fullfiled.
*/
}
if(p==(c-1))//checking if digits are in increasing order
{
System.out.println("climbing number");
}
else
{
System.out.println("not a climbing number");
}
}//end of main method
}//end of class

Jan 16, 2015 10:22:36 PM

climbing1.main();
enter the number
123
1

climbing number

climbing1.main();
enter the number
564
5

not a climbing number

Class climbing_prime
/**
*
*
*
*
*
*
*
*/

1/2

QUESTION
Design a program in java to check whether a number is a
climbing prime or not.
A climbing prime is a number which is prime and also a
climbing number(whose digits are in ascending order)
eg, 137

import java.io.*;
public class climbing_prime
{
boolean prime(int n)
{
int v=0;
for(int g=1;g<=n;g++)
{
if(n%g==0)
{
v=v+1;
}
}
if(v==2)
{
return true;
}
else
{
return false;
}
}
public boolean climbing(int n)
{
int c=0;
//counter to store number of digits of the number
int a=n;
int z=n;
while(a>0)
//loop to count no of digits
{
c=c+1;
//updating counter variable to count no. of digits
a=a/10;
}
int m[]=new int[c];
int y=0;
for(int i=0;i<c;i++)
{
y=z%10;
//extracting digits
m[i]=y;
Jan 16, 2015 10:33:47 PM

Class climbing_prime (continued)

2/2

//storing digits of the number in an array


z=z/10;
//updating loop variable
}
int p=0;
for(int e=0;e<(c-1);e++)
{
if(m[e]>m[e+1])
//comparing digits of the number
{
p=p+1;
//counting the no. of times where a digit is smaller than
//the next digit.(array is storing the digits fom last to firs
t)
}
}
if(p==(c-1))
//checking if digits are in increasing order
{
return true;
}
else
{
return false;
}
}
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
climbing_prime obj1=new climbing_prime();
/** If the number is climbing and prime both simultaneously
* then only is the number a climbing prime.*/
if(climbing(n))
{
if(prime(n))
{
System.out.println("its a climbing prime");
}
else
{
System.out.println("its not a climbing prime");
}
}
else
{
System.out.println("its not a climbing prime");
}
}//end of main
}//end of class

Jan 16, 2015 10:33:48 PM

climbing1.main();
enter the number
137
its a climbing prime
climbing1.main();
enter the number
123
its not a climbing prime
climbing1.main();
enter the number
31
its not a climbing prime

Class factorial

1/1

import java.io.*;
public class factorial
{
int n;
int f;
DataInputStream d=new DataInputStream(System.in);
public factorial()
{
n=0;
f=0;
}
int fact(int num)//recursive method
{
if(num==0)//base case
{
return 1;
}
return (num*fact(num-1));//recursive case
}
void getnum(int x)
{
n=x;
f=fact(n);
System.out.println("FACTORIAL "+f);
}
void main()throws IOException
{
System.out.println("ENTER NUMBER");
int a=Integer.parseInt(d.readLine());
getnum(a);
}//end of main
}//end of class

Jan 16, 2015 11:18:27 PM

factoria1.main();
ENTER NUMBER
5
FACTORIAL 120
factoria1.main();
ENTER NUMBER
6
FACTORIAL 720

Class good_number

1/1

/**
*
* QUESTION
*
* Design a program in java to check whether a number is
* a good number or not.
* A good number is a number in which the sum of reciprocals
* of every digit equals 1.
* eg,
*
22 -- 1/2 +1/2 =1
*
333 -- 1/3 +1/3 +1/3 =1
*/
import java.io.*;
public class good_number
{
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int a=Integer.parseInt(d.readLine());
double c=0;
//variable to store sum of reciprocals of digits
int m=a;
//creating dummy variable
double r=0;
while(m>0)
{
r=m%10;
c=c+(1/r);
/*
* finding the sum of reciprocals of the digits of the
* inputed number.
*/
m=m/10;
}
/*
* if the sum is equal to 1 then the number is
* good number.
*/
if(c==1)
{
System.out.println("its a good number");
}
else
{
System.out.println("its not a good number");
}
}//end of main
}//end of clss

Jan 16, 2015 10:18:47 PM

good_num1.main();
enter the number
22
its a good number
good_num1.main();
enter the number
56
its not a good number

Class Change

1/2

package Recursion;
/**
* PROGRAM TO CHANGE THE CASE OF A STRING
*
*/
import java.io.*;
public class Change
{
//data members
String str;
String newstr; //data members
int len;
//data members
DataInputStream d=new DataInputStream(System.in);
Change()
{
str="";
newstr="";
len=0;
}

//default constructor

//method to input the word


void inputword()throws IOException
{
System.out.println("ENTER A STRING");
str=d.readLine();
}
//function to convert case of a character
char caseconvert(char ch)
{
int k=' ';
if(ch!=' ')
{
if(ch>92)
{
k=ch-32;//case conversion to capital
}
else
{
k=ch+32;//case conversion to small
}
}
return (char)k; //returning character of opp. case
}
//recursive function
void recchange(int n)
{
Jan 19, 2015 11:16:52 PM

Class Change (continued)

2/2

if(n<(str.length()))
{
newstr=newstr+caseconvert(str.charAt(n));
//extracting a character and changing its case simultaneously
recchange(n+1);
//recursive technique
}
}
void display()
{
System.out.println("ORIGINAL WORD
System.out.println("CHANGED WORD
//displaying output
}

"+str);
"+newstr);

void main()throws IOException


{
Change obj1=new Change();
//creating object
obj1.inputword();
//calling method to execute program
obj1.recchange(0);
//calling method to execute program
obj1.display();
//calling method to execute program
}
//end of main
}

Jan 19, 2015 11:16:52 PM

change1.main();
ENTER A STRING
ToDAy iS THursDAy
ORIGINAL WORD
ToDAy iS THursDAy
CHANGED WORD

tOdaY Is thURSdaY

change1.main();
ENTER A STRING
THe tAj maHal iS in AgRA
ORIGINAL WORD
THe tAj maHal iS in AgRA
CHANGED WORD

thE TaJ MAhAL Is IN aGra

Class happy

1/2

/**
* A Happy number is a number for which sum of squares of
* digits finally comes out o be 1.
* For Example: 139
* 1. 139 : 1 + 9 +81 = 91
* 2. 91 : 81 + 1
= 82
* 3. 82 : 64 + 4
= 68
* 4. 68 : 36 + 64
= 100
* 5. 100 : 1 + 0 +0 = 1
*
* Number of steps =5
*
* CLASS NAME
: happy
* DATA MEMBERS : n TO STORE THE NUMBER
* FUNCIONS :
* happy()
: constructor to assign 0 to n
* void getnum(int nn)
: to assign parameter value to n
* int power(int nn)
:to return square of a number
* int sumOfSquares(int nn)
:returns sum of square of digits of a number
* void ishappy()
:to check if the given number is happy or not
*
* Also find the number of steps required in the process for each number.
* Also define the main() function to exeute the program by creating object.
*/
import java.io.*;
public class happy
{
int n;
public happy() //default constuctor
{
n=0;//initialing data member
}
DataInputStream d=new DataInputStream(System.in);
public void getnum(int nn)//method to assign inputed value to n
{
n=nn;
}
public int power(int nn)//to find and return square of a number
{
int p=nn*nn;
return p;
}
public int sumOfSquares(int nn)//to calculate sum of square
{
int m=nn;//creating dummy variable
int r=0;
int ps=0;//to store sum of squares
int t=0;
Jan 16, 2015 11:14:20 PM

Class happy (continued)

2/2

System.out.print(nn+"\t=");
//to print the process(only for understaning)
while(m>0)
{
r=m%10;
t=power(r);
ps=ps+t;//calculating sum
m=m/10;//updating loop variable
System.out.print("\t"+t+( m>0 ? "\t+":" " ));
//to print the process(only for understaning)
}
System.out.println();
return ps;//returning sum of squares of the digits
}
public void ishappy()
{
int mm=n;
int c=0;//counter to cont no of steps taken
do
{
mm=sumOfSquares(mm);
c++;
}while(mm>9);
//checking if sum of square is single digit
if(1==mm)//checking if final sum is equal to 1
{
System.out.println("It is a happy number");
System.out.println("No of steps = "+c);
//printing number of steps
System.out.println();
}
else
{
System.out.println("It is not a happy number");
}
}
public void main()throws IOException
{
System.out.println("Enter a number");
int nn=Integer.parseInt(d.readLine());//inputing number
System.out.println();
happy ob1=new happy();//creating object
ob1.getnum(nn);//calling function via object ob1
ob1.ishappy();//calling function via object ob1
}//end of main
}//end of class

Jan 16, 2015 11:14:20 PM

happy1.main();
Enter a number
139

139

81

91

81

82

64

68

64

36

100

It is a happy number
No of steps = 5

Class Numeric

1/2

/**
* QUESTION
* A class called Numeric has been defined yo find and display the frequency
* of each digit present in the number and the product of the digits .
* Some of the members of the class are given below :
*
* Data Member : n - long integer type
*
* Member functions :
* Numeric(long a)
:constructor to assign a to n
* void frequency()
:to find the frequency of each digit in the number
* int product()
:to return the product of the digits of the number
*
* Write main() function to implement the above
*/

import java.io.*;
public class Numeric
{
long n;
public Numeric(long a)//parameterised constructor
{
n=a;
}
void frequency()//method to find frequency of digits
{
int a[]=new int[10];
//creating an array to store
//frequency of digits
for(int i=0;i<10;i++)//initialising the array
{
a[i]=0;
}
long m=n;//creating dummy variable
int r=0;//variabble to extract digits
int p=1;//variable to store product of digits
while(m>0)
{
r=(int)(m%10);
a[r]=(a[r]+1);//finding frequency
m=m/10;
//updating loop variable
}
System.out.println("Number entered is "+n);
System.out.println("Digit\tfrequency");
for(int j=0;j<10;j++)
{
if(a[j]!=0)
{
System.out.println(j+"\t"+a[j]);
Jan 16, 2015 10:50:33 PM

Class Numeric (continued)

2/2

//printing frequency if it is not 0


}
}
} //end of frequency()
int product()
{
long m=n;
int p=1;
long r=0;
while(m>0)
{
r=m%10;
p=(int)(p*r);
m=m/10;
}
return p;
}//end of product
public static void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a number");
long a=Long.parseLong(d.readLine());
Numeric obj1=new Numeric(a);
//creating object using
//parameterised constructor
obj1.frequency();//calling functions
int p=obj1.product();
System.out.println("Products of digits of the number is "+p);
}//end of main()
}

Jan 16, 2015 10:50:33 PM

Numeric.main();
Enter a number
156
Number entered is 156
Digit

frequency

Products of digits of the number is 30


Numeric.main();
Enter a number
555
Number entered is 555
Digit

frequency

Products of digits of the number is 125

Class pallindrome

1/1

/**
* QUESTION
* Design a program in java to check whether a number
* is a pallindrome number or not.
* A pallindrome number is a number is a number which is
* identical to its reverse.
* eg, 121,212,etc.
*/
import java.io.*;
public class pallindrome
{
public int reverse(int n) throws IOException
//method to create reverse of a number
{
int m=n;
//creating dummy variable
int r=0;
int s=0;
while(m>0)//loop condition
{
r=m%10;
//last digit extraction
s=(s*10)+r;
//creating reverse number
m=m/10;
//updating loop variable
}
return s;
//returning reverse of a number
}
public void main() throws IOException
/** method to check if a number is pallindrome*/
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
//inputing the number
int n=Integer.parseInt(d.readLine());
int s= reverse(n);
//calling reverse function
if(s==n)
/** comparing reverse of number to original number*/
{
System.out.println("Number is pallindrome");
}
else
{
System.out.println("Number is not a pallindrome");
}
}//end of main
}//end of clss

Jan 16, 2015 10:56:21 PM

pallindr1.main();
enter the number
15151
Number is pallindrome
pallindr1.main();
enter the number
122
Number is not a pallindrome
pallindr1.main();
enter the number
22
Number is pallindrome
pallindr1.main();
enter the number
7548
Number is not a pallindrome
pallindr1.main();
enter the number
45654
Number is pallindrome

Class perfect

1/1

/**
* QUESTION
*
* Design a program in java to check whether a number
* is a PERFECT number or not.
*/
import java.io.*;
public class perfect
{
public int digit_count(int n)throws IOException
/** method to count no of digts in a number */
{
int c=0;
int m=n;
//creating dummy variable
while(m>0)
{
c=c+1; //counter variable
m=m/10; //updating loop variable
}
return c; //returning no of digits
}//end of digit_count
public void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter the number");
int n=Integer.parseInt(d.readLine());
int c=digit_count(n);
//counting digits
int p=(int)Math.pow(n,2);
//squaring the number
int e=(int)Math.pow(10,c);
int ex=p%e;
//extracting c no of digits from end of square of number
if(ex==n)
{
System.out.println("The number is a PERFECT number");
}
else
{
System.out.println("The number is not a PERFECT number");
}
}
}

Jan 16, 2015 10:59:04 PM

perfect1.main();
enter the number
25
The number is a PERFECT number
perfect1.main();
enter the number
35
The number is not a PERFECT number

Class poweris

1/1

/**
* PROGRAM TO FIND A NUMBER RAISED TO A GIVEN POWER
*/
import java.io.*;
public class poweris
{
int n;
int m;
double p;
DataInputStream d=new DataInputStream(System.in);
poweris()
{
n=0;
m=0;
p=0.0;
}
double power(int n,int m)
{
if(m==0||n==1)
{
return 1;
}
if(m>-1)
{
return (n*power(n,m-1));
}
return (power(n,m+1)/n);
}
void findresult()throws IOException
{
System.out.println("ENTER NUMBER");
n=Integer.parseInt(d.readLine());
System.out.println("ENTER POWER");
m=Integer.parseInt(d.readLine());
p=power(n,m);
}
void printresult()throws IOException
{
System.out.println(n+" raised to "+m+" is "+p);
}
void main()throws IOException
{
poweris obj=new poweris();
obj.findresult();
obj.printresult();
}
}

Jan 16, 2015 11:24:51 PM

poweris1.main();
ENTER NUMBER
5
ENTER POWER
3
5 raised to 3 is 125.0
poweris1.main();
ENTER NUMBER
6
ENTER POWER
2
6 raised to 2 is 36.0

Class Prime_Triplets
package
/**
*
*
*
*
*
*/

1/2

Practical_Questions.Sample_Questions;
QUESION
Program to generate PRIME TRIPLETS between two
given limits.
If no ne are found print appropriate error.

import java.io.*;
public class Prime_Triplets
{
int s;
int l;
int c;
DataInputStream d=new DataInputStream(System.in);
Prime_Triplets()
{
c=0;
s=0;
l=0;
}
boolean is_prime(int a,int n)
{
boolean j=true;
if(a==1)
{
j=false;
}
if(n<a)
{
if(a%n==0)
{
j=false;
}
if(a%n!=0)
{
j= is_prime(a,++n);
}
}
return j;
}
boolean generate_triplets(int x)
{
Jan 19, 2015 11:20:35 PM

Class Prime_Triplets (continued)

2/2

if(is_prime(x,2))
{
if(is_prime(x+2,2))
{
if(is_prime(x+6,2))
{
System.out.println(x+"\t"+"\t"+(x+2)+"\t"+(x+6));
c++;
return true;
}
}
if(is_prime(x+4,2))
{
if(is_prime(x+6,2))
{
c++;
System.out.println(x+"\t"+"\t"+(x+4)+"\t"+(x+6));
return true;
}
}
}
return false;
}
void main()throws IOException
{
Prime_Triplets obj=new Prime_Triplets();
System.out.println("ENTER LOWER LIMIT");
s=Integer.parseInt(d.readLine());
System.out.println("ENTER UPPER LIMIT");
l=Integer.parseInt(d.readLine());
System.out.println("PRIME TRIPLETS ARE");
for(int i=s;i<l+1;i++)
{
generate_triplets(i);
}
if(c==0)
{
System.out.println("NO TRIPLETS FOUND");
}
}
}

Jan 19, 2015 11:20:35 PM

prime_Tr1.main();
ENTER LOWER LIMIT
1
ENTER UPPER LIMIT
20
PRIME TRIPLETS ARE
5

11

11

13

11

13

17

13

17

19

17

19

23

prime_Tr1.main();
ENTER LOWER LIMIT
10
ENTER UPPER LIMIT
30
PRIME TRIPLETS ARE
11

13

17

13

17

19

17

19

23

Class Revstr

1/1

/**
* PROGRAM TO CHECK FOR PALLINDROME STRING
*/
import java.io.*;
public class Revstr
{
String Str;
String Revst;
DataInputStream d=new DataInputStream(System.in);
public Revstr()
{
Str="";
Revst="";
}
void getStr() throws IOException
{
System.out.println("ENTER STRING");
Str=d.readLine();
}
void recReverse(int n)
{
if(n>-1)
{
Revst=Revst+Str.charAt(n);
recReverse(n-1);
}
}
void check()
{
int l=Str.length();
recReverse(l-1);
if(Str.equals(Revst)) //checking equality of strings
{
System.out.println("PALLINDROME"); //printing appropriate output
}
else
{
System.out.println("NOT PALLINDROME");
//printing appropriate output
}
}
public void main() throws IOException//main() method
{
Revstr obj =new Revstr();
obj.getStr();
obj.check();
}//end of main
}

Jan 19, 2015 11:09:13 PM

revstr1.main();
ENTER STRING
AADDAADDAA
PALLINDROME
revstr1.main();
ENTER STRING
ASDFGHJ
NOT PALLINDROME
revstr1.main();
ENTER STRING
ASDFGHJKLKJHGFDSA
PALLINDROME

Class Transarray

1/4

/**
* QUESTION
*
* A transpose of an array obtained by interchanging the
* elements of the rows and columns.
* A class Transarray contains a two dimensional integer
* array of order [m*n]. The maximum value possible for
* both 'm' and 'n' is 20.
*
* Design a class Transarray to find the transpose of a
* given matrix . the details of the members are given
* below :
*
* CLASS NAME
: Transarray
*
* DATA MEMBERS
* arr[][]
: stores th matrix elements
* m
: integer to store the number of rows
* n
: integer to store the number of columns
*
* MEMBER FUNCTIONS
* Transarray()
: default constructor
* Transarray(int mm,int nn)
: to initialise the size of matrix,
*
m=mm , n=nn
* void fillarray()
: to enter the elements of the matrix
* void transpose(transarray A)
: to find the transpose of a given matrix
* void disparray()
: displays the array in a matrix form
*
* Define main() method to execute the class.
*/

import java.io.*;
public class Transarray
{
int arr[][]=new int[20][20];
/*
* data member to store the matrix
*/
int m;
/*
* data memebr to store number of rows in the matrix
*/
int n;
/*
* data memebr to store number of columns in the matrix
*/
DataInputStream d=new DataInputStream(System.in);
/*
* input stram object.
* YOUCAN ALSO USE BUFFERED READER
Jan 16, 2015 10:45:01 PM

Class Transarray (continued)

2/4

*/

/*
* default constructor to initialise the array
*/
public Transarray()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
arr[i][j]=0;
}
}
}//end of default constructor Transarray()

/*
* parameterised consructor to initialise size of matrix
*/
public Transarray(int mm,int nn)
{
m=mm;
n=nn;
}//end of parameterised constructor
void fillarray()throws IOException
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("enter "+i+"*"+j+" th element");
arr[i][j]=Integer.parseInt(d.readLine());
/*
* inputing elements of the matrix
*/
}
}
}//end of method fillarray()

/*
* method to find the transpose of the inputted array
*/
void transpose(Transarray A)
{
int arr1[][]=new int[20][20];
/*
* creating a local array to store the
* transpose of the matrix
*/
Jan 16, 2015 10:45:01 PM

Class Transarray (continued)

3/4

for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
arr1[i][j]=A.arr[j][i];
/*
* storing the element of original array arr with
* poaition i,j at a position j,i in the local array
* i,e., creating the transpose of the matrix.
*/
}
}
System.out.println("transpose");
/*
* printing the transpose array i,e.,arr1 in matrix form
*/
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(arr1[i][j]+"\t");
}
System.out.println();
}
}//end of transpose(transarray A)

/*
* method to display the array (original)
*/
void disparray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
/**
* main() method to create object and call methods using the object
* to execute the program.
*/
public void main() throws IOException
{
int mm=0;
int nn=0;
System.out.println("enter no of rows");
Jan 16, 2015 10:45:01 PM

Class Transarray (continued)

4/4

mm=Integer.parseInt(d.readLine());
System.out.println("enter no of columns");
nn=Integer.parseInt(d.readLine());
/*
* inputting the number of rows and number of columns from the user
*/
/**
* if and only when no of rows or and columns both are less than 20,
* then only we have to find the transpose.
* if any one or both of them exceed 20 we will display the
* appropriate message and not find the transpose, as we have
* defined the matrix with maximum 20 rows and columns only.
*/
if(mm<20&&nn<20)
{
Transarray o1=new Transarray(mm,nn);
/** creating object using parameterised constructor
* and then calling functins using it to execute the program.*/
o1.fillarray();
System.out.println("original");
o1.disparray();
o1.transpose(o1);
}
else
{
System.out.println("no of rows or columns cannot exceed 20");
}
}//end of main
}//end of class

Jan 16, 2015 10:45:01 PM

transarr1.main();
enter no of rows
3
enter no of columns
3
enter 0*0 th element
1
enter 0*1 th element
2
enter 0*2 th element
3
enter 1*0 th element
4
enter 1*1 th element
5
enter 1*2 th element
6
enter 2*0 th element
7
enter 2*1 th element
8
enter 2*2 th element
9
original
1

transpose
1

Class merging

1/2

import java.io.*;
class merging
{
int size;
int a[]=new int[200];
public void read()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("ENTER SIZE OF ARRAY");
size=Integer.parseInt(d.readLine());
System.out.println("ENTER THE ARRAY");
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(d.readLine());
}
public merging merge(merging x,merging y)
{
merging z=new merging();
int a1=0,b1=0,c1=0;
while(a1<x.size&&b1<y.size)
{
if(x.a[a1]<y.a[b1])
z.a[c1++]=x.a[a1++];
if(x.a[a1]>y.a[b1])
z.a[c1++]=y.a[b1++];
if(x.a[a1]==y.a[b1])
{
z.a[c1]=x.a[a1];
c1++;
b1++;
a1++;
}
}
if(a1==x.size)
while(b1<y.size)
z.a[c1++]=y.a[b1++];
if(b1==y.size)
while(a1<x.size)
z.a[c1++]=x.a[a1++];
z.size=c1;
return z;
}
public void display()
{
System.out.println();
for(int i=0;i<size;i++)
System.out.print(a[i]+" ");
System.out.println();
}
public void main()throws IOException
Jan 20, 2015 2:00:06 PM

Class merging (continued)

2/2

{
merging x=new merging();
merging y=new merging();
merging z=new merging();
System.out.println("ENTER DETAILS OF THE FIRST ARRAY");
x.read();
System.out.println("ENTER DETAILS OF THE SECOND ARRAY");
y.read();
z=z.merge(x,y);
System.out.println("THE FIRST ARRAY");
x.display();
System.out.println("THE SECOND ARRAY");
y.display();
System.out.println("THE MERGED ARRAY");
z.display();
}
}

Jan 20, 2015 2:00:06 PM

ENTER DETAILS OF THE FIRST ARRAY


ENTER SIZE OF ARRAY
5
ENTER THE ARRAY
1
3
5
7
9
ENTER DETAILS OF THE SECOND ARRAY
ENTER SIZE OF ARRAY
6
ENTER THE ARRAY
2
4
6
7
8
10
THE FIRST ARRAY
1 3 5 7 9
THE SECOND ARRAY
2 4 6 7 8 10
THE MERGED ARRAY
1 2 3 4 5 6 7 8 9 10

Class duplicate

1/2

import java.io.*;
class duplicate
{
int num[]=new int[10];
void readList()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("ENTER INTEGERS IN ASCENDING ORDER");
for(int i=0;i<10;i++)
{
num[i]=Integer.parseInt(d.readLine());
}
}
void packList()
{
int pack[]=new int[10];
pack[0]=num[0];int k=0;
for(int i=0;i<9;i++)
{
if(num[i]!=num[i+1])
pack[++k]=num[i+1];
}
for(int i=0;i<10;i++)
{
num[i]=0;
}
for(int i=0;i<=k;i++)
{
num[i]=pack[i];
}
for(int i=k+1;i<10;i++)
{
num[i]=-9999;
}
}
void dispList(int x)
{
System.out.println("REQUIRED ARRAY");
for(int i=0;i<=x;i++)
System.out.print(num[i]);
System.out.println();
}
void main()throws IOException
{
duplicate a=new duplicate();
a.readList();
a.packList();
int i=0;
for(i=0;i<10;i++)
Jan 20, 2015 2:05:48 PM

Class duplicate (continued)

2/2

{
if(a.num[i]==-9999)
{a.dispList(i-1);
break;
}
}
}
}

Jan 20, 2015 2:05:48 PM

ENTER INTEGERS IN ASCENDING ORDER


1
1
1
2
2
3
3
3
4
4
REQUIRED ARRAY
1234
ENTER INTEGERS IN ASCENDING ORDER
4
5
6
7
7
7
7
8
8
8
REQUIRED ARRAY
45678

Class matrix_operation

1/2

import java.io.*;
public class matrix_operation
{
int ar[][]=new int[10][10];
int m;
int n;
int x;
DataInputStream d=new DataInputStream(System.in);
public void getrowcolumn()throws IOException
{
System.out.println("enter the number of rows");
m=Integer.parseInt(d.readLine());
System.out.println("enter the number of column");
n=Integer.parseInt(d.readLine());
}
public void getmatrix()throws IOException
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter the term of("+(i+1)+","+(j+1)+") e
lement");
ar[i][j]=Integer.parseInt(d.readLine());
}
}
}
public void print_mat_and_sum()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(ar[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n\n");
int s=0;
int k=0;
for(int i=0;i<m;i++)
{
k=ar[i][0];
for(int j=0;j<n;j++)
{
if(ar[i][j]>k)
Jan 20, 2015 2:13:12 PM

Class matrix_operation (continued)

2/2

{
k=ar[i][j];
}
}
s=s+k;
}
System.out.println("Sum of greatest element from each row\t"+s);
}
public void change_diagonal()throws IOException
{
if(m!=n)
{
System.out.println("Its is not a square matrix,so change is not
possible.");
}
else
{
System.out.println("value of x=");
x=Integer.parseInt(d.readLine());
for(int i=0;i<m;i++)
{
ar[i][i]=x;
ar[(m-i-1)][i]=x;
}
System.out.println("Resultant Matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(ar[i][j]+"\t");
}
System.out.println();
}
}
}
public void main()throws IOException
{
matrix_operation o=new matrix_operation();
o.getrowcolumn();
o.getmatrix();
o.print_mat_and_sum();
o.change_diagonal();
}// end of main
}//end of class

Jan 20, 2015 2:13:12 PM

Sum of greatest element from each row


value of x=
9
Resultant Matrix:
9
5
7
9
1
9
9
9
2
9
9
5
9
3
2
9
enter the number of rows
3
enter the number of column
4
Enter the term of(1,1) element
3
Enter the term of(1,2) element
5
Enter the term of(1,3) element
7
Enter the term of(1,4) element
4
Enter the term of(2,1) element
1
Enter the term of(2,2) element
6
Enter the term of(2,3) element
4
Enter the term of(2,4) element
9
Enter the term of(3,1) element
2
Enter the term of(3,2) element
4
Enter the term of(3,3) element
7
Enter the term of(3,4) element
5
3
5
7
4
1
6
4
9
2
4
7
5

31

Sum of greatest element from each row


23
Its is not a square matrix,so change is not possible.

You might also like