Вы находитесь на странице: 1из 49

Q1. Consider the sequence of natural numbers.

1,2,3,4,5,6,7,8, 9,10,
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25………………….

Removing every second number produces the sequences

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25………………….

Removing every third number produces the sequences

1, 3, 7, 9, 13, 15, 19, 21, 25………………….

This process continues indefinitely by removing the fourth, fifth…and so on,


till after a fixed number of steps, certain natural numbers remain indefinitely.
These are known as Lucky Numbers.

SAMPLE INPUT: N=10

OUTPUT: The Lucky Numbers less than 10 are: 1 3 7

Algorithm

Step 1: Start

Step 2: Enter numbers in ‘n’ and store in ‘a[]’.

Step 3: Removing every second number in first for loop.

Step 4: Removing every third number in second for loop.

Step 5: Last for loop to print the sequence.

Step 6: End
import java.io.*;

classLuckyNumber

public static void main(String args[])throws IOException// main function

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

int n=Integer.parseInt(br.readLine());// accepting number

int a[]=new int[n];

int c=n;

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

a[i]=i+1;

int del=1;

System.out.println(“\nLucky Number Operation :\n”);

while(del<n){

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

for(int j=i;j<n-1;j++){

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

n--;

}
del++;

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

System.out.print(a[i]+“ ”);

System.out.println();

}// end of while

System.out.print(“\nHence, The Lucky Number is less than “+c+” are: ”);

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

System.out.print(a[i]+“ ”);

}}}
Q2. Write a program to display an entered number in words.

Algorithm

Step 1: Start

Step 2: Enter a number in ‘amt’.

Step 3: Enter string of 11,12,…, in words in x.

Step 4: Enter string of 1,2,….., in words in x1.

Step 5: Enter string of 10,20,…, in words in x2.

Step 6: Convert no. in words.

Step 7: End
import java.io.*;

class Num2Word

public static void main(String args[])throws IOException// main function

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter any Number(less than 99)”);

int amt=Integer.parseInt(br.readLine());//accepting number

int z,g;

String x[]={“”,“Ten”,“Eleven”,“Twelve”,“Thirteen”,“Fourteen”,“Fifteen”,“
Sixteen”,“Seventeen”,“Eighteen”,“Nineteen”};

String
x1[]={“”,“One”,“Two”,“Three”,“four”,“Five”,“Six”,“Seven”,“Eight”,“Nine”};

String
x2[]={“”,“Twenty”,“Thirty”,“Forty”,“Fifty”,“Sixty”,“Seventy”,“Eighty”,“Ninety”
};

z=amt%10;//finding the number in words

g=amt/10;

if(g!=1)

System.out.println(x2[g-1]+“ ”+x1[z]);

else

System.out.println(x[amt-9]);

}}
Q3. Write a program to create Pascal’s Triangle.

Algorithm

Step 1: Start

Step 2: Enter a number in ‘n’.

Step 3: (n+1) will be entered in pas[].

Step 4: Use first for loop with ‘i’ and second with ‘j<=i’ to print the pascal’s
triangle.

Step 5: Display Pascal’s Triangle.

Step 6: End
import java.io.*;

class Pascal

public void pascalw()throws IOException

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter a Number”);

int n=Integer.parseInt(br.readLine());

int [] pas=new int[n+1];

pas[0]=1;

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

for(int j=0;j<=i;++j)

System.out.print(pas[j]+{“ ”);

System.out.println();

for(int j=i+1;j>0;j--)

pas[j]=pas[j]+pas[j-1];

}}
Q4. Write a program to create a string and replace all vowels with ‘*’.

Algorithm

Step 1: Start

Step 2: Enter a string in ‘a’.

Step 3: First print the original string.

Step 4: With the length of the string use the for loop with variable ‘z’ and replace
all the vowels with ‘*’.

Step 5: Display the new string.

Step 6: End
import java.io.*;

classvowel_star

public static void main(String args[])throws IOException//main fuction

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter a String”);

StringBuffer a=new StringBuffer(br.readLine();//accepting a string

System.out.println(“Original String – ” +a);

int z;

for(z=0;z<a.length();z++)//loop for replacing vowel with “*”

if(a.charAt(z)==‘a’||a.charAt(z)==‘e’||a.charAt(z)==‘i’||a.charAt(z)==‘o’||a.charAt(
z)==‘u’||a.charAt(z)==‘A’||a.charAt(z)==‘E’||a.charAt(z)==‘I’||a.charAt(z)==‘O’||a.
charAt(z)==‘U’)//checking and replacing the variables

a.setCharAt(z,‘*’);

System.out.println(“New String – “+a);//displaying the result

}
Q5. Write a program to display the frequency of each character in an entered
string.

Algorithm

Step 1: Start

Step 2: Enter string in ‘str’.

Step 3: Use ‘l’ to take out the length of string.

Step 4: Chop each character from the string.

Step 5: Count the number of times each character repeated.

Step 6: Display the frequencies.

Step 7: End
import java.io.*;

class Frequency

int al,l,p,j,freq;

public Frequency()//default construction

p=0,freq=0;//initialize instance variables

public void count(String str)//counting character frequency

int i;

l=str.length();

System.out.print(str);

for(i=0;i<l;i++)

char b=str.charAt(i);

if(a==b)

freq=freq+l;

System.out.println(a+“ ”+“occurs”+freq+“times”);

freq=0;

}
public static void main(String args[])throws IOException//main function

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter String”);

String str=br.readLine();

Frequency x=new Frequency();

x.count(str);

}
Q6. Write a program to print all prime number from 1 to 100.

Algorithm

Step 1: Start

Step 2: Start a for loop from 1 to 100.

Step 3: Check if the no. is divisible it ‘1’ and only from ‘itself’. If true then Step 4
else Step 5.

Step 4: Display the number.

Step 5: End
public class pro

public static void main()

int k=0; // for string no. of factors

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

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

if(i%j==0)

k++;

}}

if(k==2)

System.out.print(i+“ ”);

}
Q7. Write a program to print all palindrome number from 10 to 100.

Algorithm

Step 1: Start

Step 2: Take number 10 to 100 in first for loop with variable ‘i’.

Step 3: Take next for loop less than ‘i’ with variable ‘j’.

Step 4: Chop numbers one by one and add them.

Step 5: Compare if the number remains same as it was then Step 6 else Step7.

Step 6: Print the number.

Step 7: End
public class prog

public static void main()

int k=0;

for(int i=10;i<=100;i=i+1)

for(int j=i;j!=0;j=j/10)

r=j%10;

k=k*10+r;

if(i==k)

System.out,print(i+“ ”);

}}
Q8. Write a java program to print the Armstrong Numbers between the given
ranges.

Algorithm

Step 1: Start

Step 2: Enter the starting number in ‘start’ and last in ‘end’ and start for loop with
variable ‘i’.

Step 3: Take each number one by one store them in ‘temp’ variable for later
calculations and chop them into single digits.

Step 4: Count the length of the digits and the multiply each digit from itself to that
times the length is found and store them in ‘num’.

Step 5: After multiplying each digit add them and check if (num==temp) the
number is equal to given number. If true then Step 6 else Step 7.

Step 6: Display the number.

Step 7: End
import java.util.*;

public class JavaExample

public static void main(String args[])

int num, start, end, i, rem, temp, counter=0;

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the start number: ");

start = scanner.nextInt();

System.out.print("Enter the end number: ");

end = scanner.nextInt();

scanner.close(); //generate Armstrong numbers between start and end

for(i=start+1; i<end; i++)

temp = i;

num = 0;

while(temp != 0)

rem = temp%10;

num = num + rem*rem*rem;

temp = temp/10;

if(i == num)
{

if(counter == 0)

System.out.print("Armstrong Numbers Between "+start+" and "+end+": ");

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

counter++;

// if no Armstrong number is found

if(counter == 0)

System.out.print("There is no Armstrong number Between "+start+" and "+end);

}
Q9. Write a program to find perfect number or not.

A perfect number is a positive integer that is equal to the sum of its proper
positive divisors, that is, the sum of its positive divisors excluding the number
itself. Equivalently, a perfect number is a number that is half the sum of all of
its positive divisors. The first perfect number is 6, because 1, 2 and 3 are its
proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal
to half the sum of all its positive divisors:

(1 + 2 + 3 + 6 ) / 2 = 6.

Algorithm

Step 1: Start

Step 2:

class IsPerfectNumber

public booleanisPerfectNumber(int number)

int temp = 0;

for(int i=1;i<=number/2;i++)

if(number%i == 0)

{
temp += i;

if(temp == number)

System.out.println("It is a perfect number");

return true;

else

System.out.println("It is not a perfect number");

return false;

public static void main(String a[]){

IsPerfectNumber ipn = new IsPerfectNumber();

System.out.println("Is perfect number: "+ipn.isPerfectNumber(28));

}
Q10. Caesar Cipher is an encryption technique which is implemented as
ROT13(‘rotate by 13 places’). It is a simple letter substitution cipher that
replaces a letter with the letter 13 places after it in the alphabets, with the
other characters remaining unchanged.

Write a program to accept a plain text of length L, where L must be greater


than 3 and less than 100.

Encrypt the text if valid as per the Caesar Cipher.

Test your program with the sample data and some random data:

SAMPLE INPUT: Hello! How are you?

OUTPUT: The cipher text is:

Uryyb? Ubjnerlbh?

importjava.util.*; //importing the utility package

class Ques_3 //creating a class {

void rot13(String w)

charch;

int a = 0;

String ans = " ";

for(inti = 0; i<w.length(); i++)

//creating an executable loop

{ch = w.charAt(i);

if(Character.isLetter(ch)) // condition execution

a = ch + 13;
if((Character.isUpperCase(ch) && a>90) || (Character.isLowerCase(ch) &&

a>122))

a = a - 26;

ch = (char)a;

ans = ans + ch;

System.out.println("OUTPUT : The cipher text is :\n"+ans); //printing the val.

} public static void main(String args[]) //creating the main funtion {

Ques_3 ob = new Ques_3();

Scanner sc = new Scanner(System.in);

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

String s = sc.nextLine();

int L = s.length();

if(L<4 || L>99)

System.out.println("INVALID LENGTH");

}else

ob.rot13(s);

}}}
Q11. Write a program to accept a sentence which may be terminated by
either’.’, ‘?’ or ’!’ only. The words may be separated by more than one blank
space and are in UPPER CASE.

Perform the following tasks:

1. Find the number of words beginning and ending with a vowel.


2. Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.

SAMPLE INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO


QUARRELANYMORE.

OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH


AVOWEL= 3

ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

importjava.util.*;

class Ques_4

booleanisVowel(String w) // Function to check if a word begins and ends with a


vowel or not

int l = w.length();

char ch1 = w.charAt(0); // Storing the first character

char ch2 = w.charAt(l-1); // Storing the last character

if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') && (ch2=='A' ||


ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U')) {

return true;

}else
{

return false;

public static void main(String args[])

{Ques_4 ob = new Ques_4();

Scanner sc = new Scanner(System.in);

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

String s = sc.nextLine();

s = s.toUpperCase();

int l = s.length();

char last = s.charAt(l-1); // Extracting the last character

/* Checking whether the sentence ends with '.' or '?' or not */ if(last != '.' &&last !=
'?' &&last != '!') {System.out.println("Invalid Input. End a sentence with either '.',
'?' or '!'only");

}else

{StringTokenizerstr = new StringTokenizer(s," .?!");

int x = str.countTokens();

int c = 0;

String w = "", a = "", b = " ";

for(inti=1; i<=x; i++)

w = str.nextToken(); // Extracting words and saving them in w


if(ob.isVowel(w))

c++; // Counting all words beginning and ending with a vowel

a = a + w + " "; // saving all words beginning and ending with a vowel in variable
'a'

else

b = b + w + " "; // Saving all other words in variable 'b'

System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND


ENDING WITH A VOWEL = " + c);

System.out.println(a+b);

}}
Q12. Write a program to declare a square matrix A[][] of order (M x M)
where ‘M’ must be greater than 3 and less than 10. Allow the user to input
positive integers into this matrix. Perform the following tasks on the matrix:

 Sort the non-boundary elements in ascending order using any standard


sorting technique and rearrange them in the matrix.
 Calculate the sum of both the diagonals.
 Display the original matrix, rearranged matrix and only the diagonal
elements of the rearranged matrix with their sum.

importjava.util.*;

class Ques_5

int A[][],B[],m,n;

void input() //Function for taking all the necessary inputs

{Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the square matrix : ");

m=sc.nextInt();

if(m<4 || m>10){

System.out.println("Invalid Range");

System.exit(0);

else{

A = new int[m][m];

n = (m-2)*(m-2);

B = new int[n]; //Array to store Non-Boundary Elements

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


for(inti=0;i<m;i++)

for(int j=0;j<m;j++)

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

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

}}}}//The below function stores Non-Boundary elements

 from array A[][] to array B[] if s = 1


 else stores the Non-Boundary elements in array A[][] from array B[]

void convert(int s)

int x=0;

for(inti=0;i<m;i++)

for(int j=0;j<m;j++)

if(i != 0 && j != 0 &&i != m-1 && j != m-1)

{if(s==1)

B[x] = A[i][j];

else

A[i][j] = B[x];

x++;

}}}}
voidsortArray() //Function for sorting Non-Boundary elements stored in array B[]

{int c = 0;

for(inti=0; i<n-1; i++)

{for(int j=i+1; j<n; j++)

{if(B[i]>B[j])

c = B[i];

B[i] = B[j];

B[j] = c;

}}}}

voidprintArray() //Function for printing the array A[][]

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

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

System.out.print(A[i][j]+"\t");

}System.out.println();

}}

voidprintDiagonal() //Function for printing the diagonal elements and their sum

{int sum = 0;

for(inti=0;i<m;i++)

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

{if(i==j || (i+j)==m-1)

{System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];

}else

System.out.print("\t");

}System.out.println();

}System.out.println("Sum of the Diagonal Elements : "+sum);

}public static void main(String args[])

Ques_5 ob = new Ques_5();

ob.input();

System.out.println("*********************");

System.out.println("The original matrix:");

System.out.println("*********************");

ob.printArray(); //Printing the original array

ob.convert(1); //Storing Non-Boundary elements to a 1-D array

ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal Elements)

ob.convert(2); //Storing the sorted Non-Boundary elements back to original

System.out.println("*********************");

System.out.println("The Rearranged matrix:");

System.out.println("*********************");

ob.printArray(); //Printing the rearranged array

System.out.println("*********************");

System.out.println("The Diagonal Elements:");

System.out.println("*********************");
ob.printDiagonal(); //Printing the diagonal elements and their sum

}}
Q13. A Circular Prime is a prime number that remains prime under cyclic
shifts of its digits. When the leftmost digit is removed and replaced at the end
of the remaining string of digits, the generated number is still prime. The
process is repeated until the original number is reached again.

A number is said to be prime if it has only two factors I and itself.

SAMPLE INPUT:N = 197

OUTPUT:

197

971

719

197 IS A CIRCULAR PRIME

importjava.util.*; //importing the utility package

class Ques_6 //creation of our class {

booleanisPrime(int n)

int c = 0;

for(inti = 1; i<=n; i++){

if(n%i == 0) //condition check

c++;

if(c == 2)

return true;

else

return false;

}int circulate(int n) //Function for circulating the digits to form new number {
String s = Integer.toString(n);

String p = s.substring(1)+s.charAt(0);

int a = Integer.parseInt(p);

return a;

voidisCircularPrime(int n) //Function to check for circular prime

{int f = 0,a = n;

do

System.out.println(a);

if(isPrime(a)==false)

{f = 1;

break;

}a = circulate(a);

}while(a!=n);

if(f==1)

System.out.println(n+" IS NOT A CIRCULAR PRIME"); else

System.out.println(n+" IS A CIRCULAR PRIME");

}public static void main(String args[]) //main fn. for calling the methods {

Ques_6 ob = new Ques_6();

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();
ob.isCircularPrime(n);

}}
Q14. Write a Program in Java to input a number and check whether it is a
Bouncy Number or not.

Increasing Number:-

Working from left-to-right if no digit is exceeded by the digit to its left it is


called an increasing number; for example, 22344.

Decreasing Number:-

Similarly if no digit is exceeded by the digit to its right it is called a decreasing


number; for example, 774410.

Bouncy Number:-

We shall call a positive integer that is neither increasing nor decreasing a


“bouncy” number; for example, 155349. Clearly there cannot be any bouncy
numbers below 100.

importjava.util.*;

classBouncyNumber //creating the working class

booleanisIncreasing(int n) //Function to check whether a number is Increasing

String s = Integer.toString(n);

charch;

int f = 0;

for(inti=0; i<s.length()-1; i++)

ch = s.charAt(i);

if(ch>s.charAt(i+1))// If any digit is more than next digit then we have to stop
checking
{f = 1;

break;

}if(f==1)

return false;

else

return true;

}booleanisDecreasing(int n) //Function to check whether a number is Decreasing {

String s = Integer.toString(n);

charch;

int f = 0;

for(inti=0; i<s.length()-1; i++) //creating the main loop

ch = s.charAt(i);

if(ch<s.charAt(i+1))// If any digit is less than next digit then we have to stop
checking

f = 1;

break;

}if(f==1)

return false;

else
return true;

}voidisBouncy(int n)

if(isIncreasing(n)==true)

System.out.println("The number " + n + " is Increasing and Not Bouncy");

else if(isDecreasing(n)==true)

System.out.println("The number " + n + " is Decreasing and Not Bouncy");

else

System.out.println("The number " + n + " is bouncy");

public static void main(String args[])

Scanner sc = new Scanner(System.in);

BouncyNumberob = new BouncyNumber();

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

int n = sc.nextInt();

ob.isBouncy(n);

}}
Q15. Write a Program in Java to input a number and check whether it is an
Evil Number or not.

Evil number: An Evil number is a positive whole number which has


evennumber of 1’s in its binary equivalent.

Example:

Binary equivalent of 9 is 1001, which contains even number of 1’s.

A few evil numbers are 3, 5, 6, 9….

Design a program to accept a positive whole number and find the binary
equivalent of the number and count the number of 1’s in it and display
whether it is an Evil number or not with an appropriate message.

importjava.util.*;

classEvilNumber

String toBinary(int n) // Function to convert a number to Binary {int r;

String s=""; //variable for storing the result

char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number
system

while(n>0)

{r=n%2; //finding remainder by dividing the number by 2

s=dig[r]+s; //adding the remainder to the result and reversing at the same time

n=n/2;

}return s;

}intcountOne(String s) // Function to count no of 1's in binary number {

int c = 0, l = s.length();
charch;

for(inti=0; i<l; i++)

{ch=s.charAt(i);

if(ch=='1')

{c++;

}return c;

}public static void main(String args[])

{EvilNumberob = new EvilNumber();

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

String bin = ob.toBinary(n);

System.out.println("Binary Equivalent = "+bin);

int x = ob.countOne(bin);

System.out.println("Number of Ones = "+x);

if(x%2==0)

System.out.println(n+" is an Evil Number.");

else

System.out.println(n+" is Not an Evil Number.");

}}
Q16. Write a Program in Java to input a number and check whether it is a
Pronic Number or Heteromecic Number or not.

Pronic Number:

A pronic number, oblong number, rectangular number or heteromecic


number, is a number which is the product of two consecutive integers, that is,
n (n + 1).

The first few pronic numbers are:

0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306,

342, 380, 420, 462 … etc.

importjava.util.*; // importing the utility package

classPronicNumber // creating the class

{public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt(); // demanding the input

int flag = 0;

for(inti=0; i<n; i++) // creating the main executable loop {

if(i*(i+1) == n) //checking the pronic condition {

flag = 1;

break;

}}

if(flag == 1) // printing the output on the basis of result of test condition

System.out.println(n+" is a Pronic Number.");


else

System.out.println(n+" is not a Pronic Number.");

}}
Q17. Write a Program in Java to input a number and check whether it is a
NIVENNumber or Niven Number or not.

NIVEN Number:

In recreational mathematics, a NIVEN number (or Niven number), is an


integer (in base 10) that is divisible by the sum of its digits.

Let’s understand the concept of NIVEN Number through the following


example:

 The number 18 is a NIVEN number in base 10, because the sum of the
digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9=0)
 The number 1729 is a NIVEN number in base 10, because the sum of
the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by
19 (1729 = 19 * 91)
 The number 19 is not a NIVEN number in base 10, because the sum of
the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19
% 10 = 9)

The first few NIVEN numbers in base 10 are:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50,54, 60,
63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117,120, 126, 132,
133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190,192, 195, 198, 200 etc.

importjava.util.*; //importing the utility package

classNIVENNumber //creating the class

{public static void main(String args[])

{Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt(); //entering the number.

int c = n, d, sum = 0; // initializing the numbers.

//finding sum of digitswhile(c>0)


{d = c%10;

sum = sum + d;

c = c/10;

if(n%sum == 0) //giving the output after the test condition

System.out.println(n+" is a NIVEN Number.");

else

System.out.println(n+" is not a NIVEN Number.");

}}
Q18. Write a Program in Java to input a word and print its Anagrams Note:
Anagrams are words made up of all the characters present in the original
word by re-arranging the characters.

Example:

Anagrams of the word TOP are: TOP, TPO, OPT, OTP, PTO and POT

importjava.util.*; //importing the utility package

class Anagrams //creating the class {

int c = 0; //initializing the variable c

void input()throws Exception //function for inputting all values {Scanner sc = new
Scanner(System.in);

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

String s = sc.next(); //entering the text

System.out.println("The Anagrams are : ");

display("",s);

System.out.println("Total Number of Anagrams = "+c);

void display(String s1, String s2) //for displaying all outputs {

if(s2.length()<=1) //condition check for the counter {

c++;

System.out.println(s1+s2);

}else

for(inti=0; i<s2.length(); i++)

{
String x = s2.substring(i, i+1);

String y = s2.substring(0, i);

String z = s2.substring(i+1);

display(s1+x, y+z);

}}}

public static void main(String args[])throws Exception //for calling all methods.

Anagrams ob=new Anagrams();

ob.input();

}}
Q19. Write a Program in Java to input a number in Decimal number system
and convert it into its equivalent number in the Binary number system.

Note:-

Binary Number system is a number system which can represent a number in


any other number system in terms of 0 and 1 only. This number system
consists of only two basic digits i.e. 0 and 1.

For Example: 25 in the Decimal number system can be represented as 11001


in the Binary number system.

import java.io.*; //importing the utility package

class Dec2Bin //creation of the class {

public static void main(String args[])throws IOException {

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));

System.out.print(“Enter a Decimal Number”);

int n=Integer.parseInt(br.readLine());

int r;

String s=""; //variable for storing the result

char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number
system

while(n>0)

r=n%2; //finding remainder by dividing the number by 2

s=dig[r]+s; //adding the remainder to the result and reversing at the sametime

n=n/2;

}
System.out.println("Output = "+s);

}}
Q20. WAP to check whether a number is a valid ISBN or not.

An ISBN (International Standard Book Number) is a ten digit code which


uniquely identifies a book.

The first nine digits represent the Group, Publisher and Title of the book and
the last digit is used to check whether ISBN is correct or not.

Example: For an ISBN “1259060977”

Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209

Now divide it with 11 = 20%/11 = 0. If the resultant value will be Zero then it
is a valid ISBN.

importjava.util.*; //importing the utility package

public class ISBN //creating the class

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter digit code : ");

String s=sc.nextLine(); //Entering the number in form of string

intlen=s.length();

if(len!=10)//condition check for further working

System.out.println("Invalid Input");

else{char ch;

int dig=0, sum=0, k=10;

for(inti=0; i<len; i++)

{
ch=s.charAt(i);

if(ch=='X') //checking for a character and placing 10 in place of it.

dig=10;

else

dig=ch-48;

sum=sum+dig*k;

k--;}

System.out.println("Output = "+sum); //printing the output on basis of a condition


check

if(sum%11==0) //printing whether the entered code is valid or not

System.out.println("Valid Code");

else

System.out.println("Invalid Code");

Вам также может понравиться