QUESTION 08: Write a program to calculate 3D and 2D distance between two points using distance
formula.
Algorithm:
1. Start.
2. Accept and store the co ordinates of the First point as X1, Y1, Z1.
3. Accept and store the co ordinates of the Second point as X2, Y2, Z2.
4. [calculation of 2D distance]
X=(X1-X2)*(X1-X2)
Y=(Y1-Y2)*(Y1-Y2)
D=Square root of (X+Y)
Display D.
5. [calculation of 3D distance]
X=(X1-X2)*(X1-X2)
Y=(Y1-Y2)*(Y1-Y2)
Z=(Z1-Z2)*(Z1-Z2)
D=Square root of (X+Y+Z)
Display D.
6. Stop.
Code:
import java.util.*; //importing package
public class program_08 { //declaring class
int x,y,z;
program_08(){
x=0;y=0;z=0;
}
void input(){ //taking inputs
Scanner scr=new Scanner(System.in);
x=scr.nextInt();
y=scr.nextInt();
z=scr.nextInt();
}
double distance1(program_08 a, program_08 b){ //calculates 2D distance
double x=Math.pow((a.x-b.x),2);
double y=Math.pow((a.y-b.y),2);
double d=Math.sqrt((x+y));
return d;
}
double distance2(program_08 p, program_08 q){ //calculates 3d distance
double x=Math.pow((p.x-q.x),2);
double y=Math.pow((p.y-q.y),2);
double z=Math.pow((p.z-q.z),2);
double D=Math.sqrt((x+y+z));
15
return D;
}
public static void main(String[] args) {
program_08 ob1=new program_08 ();
System.out.println("Enter the co-ordinates of first point");
ob1.input();
program_08 ob2=new program_08 ();
System.out.println("Enter the co-ordinates of second point");
ob2.input();
program_08 ob3=new program_08();
System.out.println("2D distance between the points"+(ob3.distance1(ob1,ob2)));
System.out.println("3D distance between the points"+(ob3.distance2(ob1,ob2)));
}
} //end of class
Output:
QUESTION 09: A unique-digit integer is a positive integer (without leading zeros) with no duplicates
digits. For example 7, 135, 214 are all unique- digit integers whereas 33,3121, 300 are not. Program
to print the unique numbers within given limits and also print the frequency.
Algorithm:
1. Start.
2. Accept and store the lower and upper limits in L1 and L2 respectively.
3. [Function check( n) to check for unique numbers]
FLAG=0
repeat for i=0,1,2,....9
COUNT=0
while(n not equal 0)
T=n%10
if(T=i)
Increment of COUNT
n=n/10
end while
if(COUNT >=2)
16
FLAG=1
end loop i
if(FLAG=0)
returns 0
else
returns 1
4. [Displaying the unique numbers within the given limits]
Repeat for i from L1 to L2
if(check(i)=0)
Print i
COUNT+1
end if
end loop i
print COUNT.
5. Stop.
Code:
import java.util.*; //importing package
public class program_09{ //declaring class
int u,l;
void input(){ //taking inputs
Scanner scr=new Scanner(System.in);
System.out.println("Enter lower limit");
l=scr.nextInt();
System.out.println("Enter upper limit");
u=scr.nextInt();
}
int check(int n){ //checking inputs
int c,m,t,i,f=0;
for(i=0;i<=9;i++){
c=0;m=n;
while(m!=0){
t=m%10;
if(t==i)
c++;
m=m/10;
}
if(c>=2)
f=1;
}
if(f==0)
return 0;
else
return 1;
17
}
void display(program_09 x, program_09 y){ //displaying
int c=0;
for(int i=x.l;i<=y.u;i++){
if(check(i)==0){
c++;
System.out.println(i);
}
}
System.out.println(“Frequency:” +c);
}
public static void main(String[] args) {
program_09 ob=new program_09();
ob.input();
program_09 obj=new program_09();
obj.display(ob,ob);
}
} //end of class program
Output:
QUESTION 10 : Program to calculate the Kaprekar Numbers within a certain limit: A given number 'n'
with ’d’ digits is squared and if the last 'd' digits is equal to the first remaining digits then the number
'n' is a Kaprekar number.
Algorithm:
1. Start.
2. Accept and store the lower and upper limits in L1 and L2 respectively.
3. [function check(n) to check for the karprekar number]
SQUARE=n*n
COUNT=0
while 'n' is greater than 0:
18
Increment 'COUNT' by 1
Update 'n' by dividing it by 10
End loop
HALF1=SQUARE % (10 to the power COUNT)
HALF2= SQUARE /(10 to the power COUNT)
if(HALF1+HALF2=n)
returns true
else
returns false
4. Repeat for i=L1 to L2
if(check(i))
Print i
end loop i
5. Stop.
Code:
import java.util.*; //importing package
class program_10{ //declaring class
int n;
program_10(int x){
n=x;
}
boolean karpreker(){ //check for karpreker number
int sq=n*n;
int c=0;
for(int i=n;i>0;i=i/10)
c++;
int h1=sq%(int)Math.pow(10,c);
int h2=sq/(int)Math.pow(10,c);
if((h1+h2)==n)
return true;
else
return false;
}
public static void main(String args[]){
Scanner scr=new Scanner(System.in);
System.out.println("Enter the lower limit");
int l=scr.nextInt();
System.out.println("Enter the upper limit");
int u=scr.nextInt();
for(int i =l;i<u;i++)
{
program_10 ob=new program_10(i);
if(ob.karpreker())
19
System.out.println(i);
}
}
} //end of class
Output:
QUESTION 11: Program to input a word and perform the following operations on it:
a) If word begins with a vowel, print the word followed by 'Y'
b) If word contains but does not begin with a vowel, print the portion of the word after the first
vowel followed by portion before the vowel along with 'C'
c) If word does not contain vowel, print the word followed by 'N'
Algorithm:
1. Start.
2. Accept and store the word in String s
3. [Function vowel(c) to check for vowel]
if c = 'a' or c = 'e' or c = 'i' or c ='o' or c = 'u'
return true
else
return false
4. [Displaying the output]
c=0
for i = 0 to length of s - 1
if vowel(s[i]) and i is not 0
print s + " Y"
c=1
break
end if
if vowel(s[i]) and i is not 0
print substring of s from i to end
print substring of s from start to i + " C"
20
c=1
break
end if
end for
if c is 0
print s + " N"
end if
5. Stop.
Code:
import java.util.*; //importing package
public class program_11 { //declaring class name
String s;
program_11(String x){
s=x;
}
boolean vowel(char c){ //check for vowel
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
return true;
else
return false;
}
void display(){ //displaying the output
int c=0;
for(int i=0;i<s.length();i++){
if(vowel(s.charAt(i)) && i==0){
System.out.println(s+" Y");
c=1;
break;
}
if(vowel(s.charAt(i)) && i!=0){
System.out.println(s.substring(i));
System.out.println(s.substring(0,i)+" C");
c=1;
break;
}
}
if(c==0)
System.out.println(s+" N");
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
System.out.println("Enter the word");
String word=scr.next();
21
program_11 ob=new program_11(word);
ob.display();
}
} //end of class
Output:
QUESTION 12: Program to print possible ways of summation. Eg., 15 = 1+2+3+4+5 = 4+5+6 = 7+8
Algorithm:
1. Start.
2. Accept and store the number in N
3. [function display(x,y) to display the output]
for i from x to y:
print i and “+”
print newline
end loop i
4. [Function limits() to get the sequence which gives the number]
m = (N / 2) + 1
for i from 1 to m:
sum = 0
for j from i to m:
sum = sum + j
if sum = N:
display(i, j)
end loop j
end loop i
5. Stop.
22
Code:
import java.util.*; //importing package
class program_12{ //declaring class name
int n,m;
program_12(int x){
n=x;
m=(x/2)+1;
}
void display(int x,int y){ //displaying the output
for(int i=x;i<=y;i++)
System.out.print(i+"+");
System.out.println();
}
void limits(){ //getting the limits
for(int i=1;i<=m;i++){
int sum=0;
for(int j=i;j<=m;j++){
sum+=j;
if(sum==n){
display(i,j);
}
}
}
}
public static void main (String args[]){
Scanner scr =new Scanner(System.in);
System.out.println("Enter a number");
int num=scr.nextInt();
program_12 ob=new program_12(num);
ob.limits();
}
} //end of class
Output:
23