Practice For Practical
Practice For Practical
import java.util.Scanner;
if (n % 2 != 0) {
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
}
System.out.println("PRIME PAIRS ARE:");
for(int i=3;i<=n/2;i++)
{
Hamming numbers are positive integer numbers whose prime factors include 2,3 and 5 only
Example:n=6 is an hamming number as 6=2x3 .So its prime factors are limited to 2 ,3
n=8 is an hamming number as 8=2x2x2 and it has only 2 as its prime factors
n=90 is an hamming number as 90=2x3x3x5 which has only 2,3,5 as prime factors
n=14 is not a hamming number as 14=2x7 .It has 7 as one of its prime factor
n=44 is not a hamming number as 44=2x2x11. It has 11 as one of its prime factors
Design a program to accept any positive integer number and check if it is a Hamming number
or not. Display the result with an appropriate message in the format specified below. The
program should also generate error message if a negative number is entered. Test your
program for the following data and some random data.
OUTPUT: 3600= 2 x 2 x 2 x 2 x 3 x 3 x 5 x 5
OUTPUT: 5832= 2 x 2 x 2 x 3 x 3 x 3 x 3 x 3 x 3
OUTPUT: 7854= 2 x 3 x 7 x 11 x 17
import java.util.Scanner;
int n=num,p=2;
while (n > 1)
{
while (n % p == 0)
System.out.print(p+"x");
n=n/p;
p++;
if(n%p==0)
System.out.print(p);
flag=false;
break;
System.out.println();
if(flag)
else
}
}
Question 3
Hamming numbers are positive integer numbers whose prime factors include 2,3 and 5 only
Example:
n=6 is an hamming number as 6=2x3 .So its prime factors are limited to 2 ,3
Design a program to accept any positive integer number and check if it is a Hamming number
or not.
import java.util.*;
boolean f=true;
System.out.println("Enter no");
int n=sc.nextInt();
for(int i=2;i<=n;i++)
if(n%i==0)
if(ob.isprime(i)==true)
{
f=false;
break;
if(f==true)
System.out.println("Hamming");
else
System.out.println("Not Hamming");
boolean isprime(int x)
int c=0;
for(int i=1;i<=x;i++)
if (x%i==0)
c++;
if(c==2)
return true;
else
return false;
}
}
Question 4
A Vampire number is a composite natural number with an even number of digits that can be factored
into two natural numbers each with half as many digits as the original number and not both with
trailing zeros, where the two factors contain precisely all the digits of the original number, in any order
of counting multiplicity.
Example: 1260 = 21 x 60 ( where, 21 and 60 contain precisely all the digits of the number )Thus,
1260 is a Vampire number.
import java.util.*;
int count(int x)
int c=0;
while(x!=0)
x=x/10;
c++;
return (c);
int j=0,y2=0,q1=0,r,r1,c1,c2,f1=0;
boolean f=true;
int n=sc.nextInt();
int q=n;
int p=ob.count(n);
if(p%2!=0)
else
int x1=(int)Math.pow(10,((p/2)-1));
int y1=(int)Math.pow(10,(p/2));
for(int i=x1;i<y1;i++)
f=true;
if(n%i==0)
j=n/i;
int x2=ob.count(j);
if(x2==p/2)
y2=i*(int)Math.pow(10,p/2)+j;
for(int k=0;k<9;k++)
q=n;
c1=0;c2=0;
while(q!=0)
{
r=q%10;
if(k==r)
c1++;
q=q/10;
q1=y2;
while(q1!=0)
r1=q1%10;
if(k==r1)
c2++;
q1=q1/10;
if(c1!=c2)
f=false;
break;
if(f==true)
f1=1;
break;
if(f1==1)
System.out.println("Vampire No");
else
System.out.println("Not Vampire");
Question 5
Design a program to accept any positive integer number and check if it is a kaprekar numbers or not.
if (n == 1)
return true;
int c= 0;
while (sqn != 0)
c++;
sqn /= 10;
sqn=n*n;
if (eq_parts == n)
continue;
int sum = sqn/eq_parts + sqn%eq_parts;
if (sum == n)
return true;
return false;
if (kaprekar(i))
import java.util.Scanner;
int n=in.nextInt();
boolean f=true;
a[i][j]= in.nextDouble();
System.out.println("Original Matrix:");
System.out.println();
double s=0.0,s1=0.0;
s=s+a[i][j];
s1=s1+a[j][i];
if(s!=1 || s1!=1)
f=false;
break;
}
if(f==true)
else
Snowball string
import java.util.*;
String s,st1="";
boolean f= true;
s=in.nextLine();
char c=s.charAt(s.length()-1);
if(c=='.' ||c=='?')
int p=st.length();
for(int i=0;i<p1;i++)
char ch=str.charAt(i);
if(ch!=' ')
st1=st1+ch;
else
int p2=st1.length();
if(p>p2)
f=false;
break;
st1="";
if(f==true)
System.out.println("Snowball string");
else
else
}
}
class sort {
int m= sc.nextInt();
int n= sc.nextInt();
int k = 0;
a[i][j] = sc.nextInt();
System.out.println("Original matrix:");
System.out.println();
}
int x=0;
b[x]= a[i][j];
x++;
if(b[j]>b[j+1])
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
x=0;
a[i][j]=b[x];
x++;
}
System.out.println("Sorted matrix:");
System.out.println();
Write a program to accept a sentence which may be terminated by either ‘.’ ,‘?’or ‘!’ only. The words may be
separated by a single blank space and should be case-insensitive.
(a)
[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"
(b)
Display the first occurring longest and shortest word in the accepted sentence.
Test your program for the following data and some random data:
Example 1
OUTPUT: IT IS A PANGRAM
SHORTEST WORD: my
Example 2
INPUT: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
OUTPUT: IT IS A PANGRAM
import java.util.Scanner;
String s = in.nextLine();
int c,f=1;
int l=s.length();
s=s.toUpperCase();
for(int i=65;i<=90;i++)
c=0;
for(int j=0;j<l;j++)
if(s.charAt(j)==(char)i)
c++;
if(c==0)
f=0;
break;
}
if(f==1)
System.out.println("Pangram");
else
System.out.println("Not Pangram");