0% found this document useful (0 votes)
67 views19 pages

Practice For Practical

Uploaded by

biswanathk1969
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)
67 views19 pages

Practice For Practical

Uploaded by

biswanathk1969
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
You are on page 1/ 19

Question 1

A number is said to be a Goldbach number, if the number can be expressed


as the addition of two odd prime number pairs. If we follow the above
condition, then we can find that every even number larger than 4 is a
Goldbach number because it must have any pair of odd prime number pairs.
Example: 6 = 3,3 ( ONE PAIR OF ODD PRIME )
10 = 3,7 and 5 , 5 ( TWO PAIRS OF ODD PRIME )
Write a program to enter any positive EVEN natural number ‘N’ where
(1<=N<=50) and generate odd prime twin of ‘N’
Test your program for the following data and some random data.
Example 1
INPUT: N = 14
OUTPUT: ODD PRIME PAIRS ARE: 3, 11
7, 7
Example 2
INPUT: N = 20
OUTPUT: ODD PRIME PAIRS ARE: 17, 3
13, 7
Example 3
INPUT: N = 44
OUTPUT: ODD PRIME PAIRS ARE: 41, 3
37, 7
31, 13
Example 4
INPUT: N = 25
OUTPUT: INVALID INPUT

import java.util.Scanner;

public class Gold


{
boolean isPrime(int n) {
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 in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();
Gold ob=new Gold();

if (n <= 1 || n >= 50) {


System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
return;
}

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++)
{

if ((ob.isPrime(i)==true) && (ob.isPrime(n-i)==true))


{
System.out.println(i + ", " + (n-i));
}
}
}
}
Question 2

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.

Example 1INPUT: Enter any number: 3600

OUTPUT: 3600= 2 x 2 x 2 x 2 x 3 x 3 x 5 x 5

3600 IS A HAMMING NUMBER

Example 2INPUT: Enter any number: 5832

OUTPUT: 5832= 2 x 2 x 2 x 3 x 3 x 3 x 3 x 3 x 3

5832 IS A HAMMING NUMBER

Example 3INPUT: Enter any number: 7854

OUTPUT: 7854= 2 x 3 x 7 x 11 x 17

7854 IS NOT A HAMMING NUMBER

import java.util.Scanner;

public class Hamming

public static void main(String args[])

Scanner scan = new Scanner(System.in);

System.out.print("Enter a number : ");

int num = scan.nextInt();

int n=num,p=2;

boolean flag = true;

while (n > 1)
{

while (n % p == 0)

System.out.print(p+"x");

n=n/p;

p++;

if (p!=2 && p!=3 && p!=5)

if(n%p==0)

System.out.print(p);

flag=false;

break;

System.out.println();

if(flag)

System.out.println(num+" is a Hamming number");

else

System.out.println(num+" is Not a Hamming number");

}
}

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.*;

public class Hamming

public static void main(String args[])

Hamming ob=new Hamming ();

boolean f=true;

Scanner sc=new Scanner (System.in);

System.out.println("Enter no");

int n=sc.nextInt();

for(int i=2;i<=n;i++)

if(n%i==0)

if(i!=2 && i!=3 && i!=5)

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.*;

public class vmp

int count(int x)

int c=0;

while(x!=0)

x=x/10;

c++;

return (c);

public static void main()

int j=0,y2=0,q1=0,r,r1,c1,c2,f1=0;

boolean f=true;

Scanner sc=new Scanner(System.in);


System.out.println("Enter the value:");

int n=sc.nextInt();

int q=n;

vmp ob=new vmp();

int p=ob.count(n);

if(p%2!=0)

System.out.println("Not Vampire No");

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.

import java .util.*;

public class kapre

static boolean kaprekar(int n)

if (n == 1)

return true;

int sqn = n*n;

int c= 0;

while (sqn != 0)

c++;

sqn /= 10;

sqn=n*n;

for (int r_digits=1; r_digits<c; r_digits++)

int eq_parts = (int) Math.pow(10, r_digits);

if (eq_parts == n)

continue;
int sum = sqn/eq_parts + sqn%eq_parts;

if (sum == n)

return true;

return false;

public static void main ()

System.out.println ("kaprekar numbers");

for (int i=1; i<=1000; i++)

if (kaprekar(i))

System.out.print(i + " ");

Doubly Markov matrix

import java.util.Scanner;

public class Markov

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Enter the Number of elements:");

int n=in.nextInt();

boolean f=true;

if(n>=3 && n<=9)


{

double a[][] = new double[n][n];

for (int i = 0; i < n; i++) {

for (int j = 0; j <n; j++) {

a[i][j]= in.nextDouble();

System.out.println("Original Matrix:");

for (int i = 0; i < n; i++) {

for (int j = 0; j <n; j++) {

System.out.print(a[i][j]+ " ");

System.out.println();

for (int i = 0; i < n; i++) {

double s=0.0,s1=0.0;

for (int j = 0; j <n; j++) {

s=s+a[i][j];

s1=s1+a[j][i];

if(s!=1 || s1!=1)

f=false;

break;

}
if(f==true)

System.out.println("Doubly Markov matrix");

else

System.out.println("Not Doubly Markov matrix");

Snowball string

import java.util.*;

public class snowball

public static void main(String args[])

String s,st1="";

boolean f= true;

Scanner in=new Scanner(System.in);

System.out.println("Enter a sentence : ");

s=in.nextLine();

char c=s.charAt(s.length()-1);

if(c=='.' ||c=='?')

int x=s.indexOf(' ');

String st= s.substring(0,x);

int p=st.length();

String str= s.substring((x+1),(s.length()-1));


int p1=str.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

System.out.println("Not Snowball string");

else

System.out.println("Termination character should be . or ?");

}
}

sort Full matrix in ascending order.


import java.util.*;

class sort {

public static void main(String args[])

Scanner sc=new Scanner (System.in);

int m= sc.nextInt();

int n= sc.nextInt();

int a[][]= new int[m][n];

int b[] = new int[m * n];

int k = 0;

for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++) {

a[i][j] = sc.nextInt();

System.out.println("Original matrix:");

for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++) {

System.out.print(a[i][j]+ " ");

System.out.println();
}

int x=0;

for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++) {

b[x]= a[i][j];

x++;

for (int i = 0; i < ((m*n)-1); i++){

for (int j = 0; j <((m*n)-1-i) ; j++)

if(b[j]>b[j+1])

int t=b[j];

b[j]=b[j+1];

b[j+1]=t;

x=0;

for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++) {

a[i][j]=b[x];

x++;

}
System.out.println("Sorted matrix:");

for (int i = 0; i < m; i++){

for (int j = 0; j < n; j++) {

System.out.print(a[i][j]+ " ");

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.

Perform the following tasks:

(a)

Determine if the accepted sentence is a Pangram or not.

[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

INPUT: Pack my box with five dozen liquor jugs.

OUTPUT: IT IS A PANGRAM

LONGEST WORD: dozen

SHORTEST WORD: my

Example 2

INPUT: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
OUTPUT: IT IS A PANGRAM

import java.util.Scanner;

public class pan

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a sentencr: ");

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");

You might also like