# final keyword – variable into constant- 2 marks

⭐⭐⭐⭐⭐

final keyword – converts the variable into constant whose value cannot be changed at any stage in the program.

Syntax:
final datatype variable name=actual value

eg: final int a=15; // value of ‘a’ is constant as 15

Find the output:

int x=10;
final int y=20;
x=x+4;
y=y+4;

What is the updated value of x and y? or Displays any error message, then What is the reason? Compile time error message – cannot assign a value to final variable ‘y’

Explanation:

int x=10; // an integer variable.

final int y= 20; // an integer variable declared as constant, so value of ‘y’ //cannot be changed anywhere in the program

x=x+4; //change the value of ‘x’ by adding 4 to it.

// so the value of ‘x’ is 14 (10+4)

y=y+4; //value of y’ will not be changed as keyword final has converted //variable ‘yinto constant

// In this case during compilation of the program the error message “Cannot assign a value to final variable y”

# Unique Number program by methods

How to write a program with methods?
Unique Number program by methods.

Each program has different parts
1. importing package
2. define class with instance variables
3. constructor to initialize variables
4. input method – to read an input
5. calculate method- processing
6. display method – to display the processed data
7. main method- invoke above mentioned methods by creating an object

``````import java.util.*;
public class UniqueNumber
{
// instance variables -
int n;
String s;
int flag,i, j,l;
/**
* Constructor for objects of class UniqueNumber
*/
public UniqueNumber()
{
// initialise instance variables
n = 0;
flag=0;
s=" ";
}
public void input() // to input an integer
{
Scanner sc=new Scanner(System.in);

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

n=sc.nextInt();
}
public void calculate()
{
s= Integer.toString(n); // converting the given number into String datatype
// Int datatype is converting to String,
// String helps to extract the character by charAt() and check each character is matching with the rest.
l=s.length();
//Loops to check the digits are repeated
for(i=0;i<l-1;i++)
{
for(j=i+1;j<l;j++)
{
if(s.charAt(i)==s.charAt(j)) // if any digits are repeated, then it is not a UniqueNumber
{ flag=1;
break;
}
}
}
}
public void display()
{
calculate(); //invoke calculate()
if(flag ==0)
System.out.println(" It is an UniqueNumber");
else
System.out.println(" It is not an UniqueNumber");
}
public static void main(String args[]) throws Exception
{
UniqueNumber un=new UniqueNumber(); //creating object
un.input();  //calling input() to main()
un.display(); //calling display() to main()
}
}

``````

Output:

Enter a number: 5673

It is an UniqueNumber

Enter a number: 5675

It is not an UniqueNumber

# Tips – ISC Computer Science Board Exam

Keep up on the Exam pattern and Syllabus

Learn the concepts with examples

Dont memorize programs, think logically and write

Practice previous year question papers and specimen copies

Notice and follow the examiners comment from analysis.

Revise everyday.

Be Careful – Whenever practising the programs

Use the variables, method and class name what has given in the questions. Instead of that dont use your own variable, method and class. (especially Section B & Section C questions)

You have written the program with correct logic and not following the same variable, method and class name reduce your marks.

Dont forget to create main() and invoke specified methods to main(), if its asked in the question paper.

In Section C, Inheritance programs
DONT WRITE super class, main() and algorithm. (if its specified as NOT to be written)

Do more practice on recursion programs.

# Back to blog

After a long days, I am back here..

Dear children, hope everyone is preparing well for the ICSE & ISC board examination 2020 …

Last year my students scored 99% in Computer Science ISC.. here I am taking the privilege to share .

I wish the current batch to score 100% in Computer Science, not only in Computer Science , other subjects also….

Wish you all the best my dear children…

# Happy New Year

May this New Year 2019 bring peace and prosperity to you and your family.

# Lucky Number

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 ………………………….

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.

Write a program to generate and print lucky numbers less than a given number N.

SAMPLE INPUT :  N = 10
OUTPUT :  The Lucky numbers less than 10 are:     1     3     7

SAMPLE INPUT :  N = 25
OUTPUT :  The Lucky numbers less than 25 are:     1     3     7     13     19

``````import java.io.*;
class luckyNumber
{
public static void main(String args[])throws IOException
{
System.out.print("Enter the Number of Elements : ");

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]=a[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 Numbers Less than "+c+" are : ");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+"   ");
}
}
}``````

Output

Enter the Number of Elements : 30

Lucky Number Operation :

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
1 3 7 9 13 15 19 21 25 27
1 3 7 13 15 19 25 27
1 3 7 13 19 25 27
1 3 7 13 19 27

Hence, the Lucky Numbers Less than 30 are : 1 3 7 13 19 27

# Magic square matrix

A magic square is an NxN matrix in which every row, column, and diagonal add up to the same number.

``````import java.io.*;
class magicmatrix
{
public static void main (String args[]) throws IOException
{
System.out.println("Enter the size of the matrix");
if(n>5)
System.out.println("Enter the number from 1 to 5");
else
{
int A[][]=new int[n][n];
int i,j,k,t;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=0;
}
}
if(n%2!=0)
{
i=0;
j=n/2;
k=1;
while(k<=n*n)
{
A[i][j]=k++;
i--;
j++;
if(i<0&&j>n-1)
{
i=i+2;
j--;
}
if(i<0)
i=n-1;
if(j>n-1)
j=0;
if(A[i][j]>0)
{
i=i+2;
j--;
}
}
}
else
{
k=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=k++;
}
}
j=n-1;
for(i=0;i<n/2;i++)
{
t=A[i][j];
A[i][i]=A[j][j];
A[j][j]=t;
t=A[i][j];
A[i][j]=A[j][i];
A[j][i]=t;
j--;
}
}
System.out.println("The magic matrix of size "+n+"x"+n+" is:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();

}

}
}
}

``````

Output:

Enter the size of the matrix
3
The magic matrix of size 3×3 is:
8 1 6
3 5 7
4 9 2

# Kaprekar number

Write a Program in Java to input a number and check whether it is a Kaprekar number or not.

Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.

If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..

Example  45

452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20

Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.

``````import java.io.*;
class KaprekarNum
{
public static void main(String args[]) throws IOException
{
System.out.print("Enter a Number : ");
int n = Integer.parseInt(br.readLine()); //Inputting the number

int sq = n*n; //finding the square of the number
String s = Integer.toString(sq); //converting the square into a String

if(sq<=9)
s = "0"+s; //Adding a zero in the beginning if the square is of single digit

int l = s.length(); //finding the length (i.e. no. of digits in the square).
int mid = l/2; //finding the middle point

String left=s.substring(0,mid); //extracting the left digits from the square
String right=s.substring(mid); //extracting the right digits from the square

int x = Integer.parseInt(left); //converting the left String into Integer
int y = Integer.parseInt(right); //converting the right String into Integer

//if sum of left and right numbers is equal to the original number then it is a Kaprekar number
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
else
System.out.println(n+" is Not a Kaprekar Number");
}
}``````

Enter a Number : 297
297 is a Kaprekar Number
Enter a Number : 9
9 is a Kaprekar Number
Enter a Number : 67
67 is Not a Kaprekar Number

# IMEI number

An IMEI number/ International Mobile Station Equipment Identity is a 15 digit number and it is said to be IMEI number if and only if the sum of the number is exactly divisible by 10.

The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model, and serial number of the device.

The IMEI number is used by a GSM network to identify valid devices and therefore can be used for stopping a stolen phone from accessing that network

The IMEI is validated in three steps:

1. Starting from the right, double every other digit (e.g., 7 becomes 14).
2. Sum the digits (e.g., 14 → 1 + 4).
3. Check if the sum is divisible by 10.
`Input:  47415403237518`
`Output:`
`47415403237518 – Is an IMEI Number.`
``````import java.io.*;
public class CheckIMEINumber{
// Function for finding and returning sum of digits of a number
int sumDig(int n)
{
// initialise here.
int a = 0;
while(n>0)
{
a = a + n%10;
n = n/10;
}
return a;}
public static void main(String args[])throws IOException
{
// create object here.
CheckIMEINumber ob = new CheckIMEINumber();
// The 'Long' data type is used to store 15 digits.
System.out.print("Enter a 15 digit IMEI code : ");
// Converting the number into String for finding length
String s = Long.toString(n);
int l = s.length();
// If length is not 15 then IMEI is Invalid
if(l!=15)
System.out.println("Output : Invalid Input");
else
{  	int d = 0, sum = 0;
for(int i=15; i>=1; i--)
{
d = (int)(n%10);
if(i%2 == 0)
{
// Doubling every alternate digit
d = 2*d;
}
// Finding sum of the digits
sum = sum + ob.sumDig(d);
n = n/10;
}

System.out.println("Output : Sum = "+sum);
if(sum%10==0)
System.out.println("Valid IMEI Code");
else
System.out.println("Invalid IMEI Code");
}
}}

``````

Enter a 15 digit IMEI code : 47415403237518
Output : Invalid Input
Enter a 15 digit IMEI code : 490154203237518
Output : Sum = 60
Valid IMEI Code
Enter a 15 digit IMEI code : 123456789101112131
Output : Invalid Input

# Pronic Number/oblong number/ rectangular number / heteromecic number

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 …

`Input :56Output :Pronic NumberExplanation: 56 = 7 * 8 i.e 56 is a product of two consecutive integers 7 and 8. `
``````import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;

for(int i=0; i<n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}

if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}``````

Enter a number : 123
123 is not a Pronic Number.
Enter a number : 110
110 is a Pronic Number.

# Display as Lower triangular matrix program in java

A square matrix is called lower triangular if all the entries above the main diagonal are zero. Similarly, a square matrix is called upper triangular if all the entries below the main diagonal are zero. A triangular matrix is one that is either lower triangular or upper triangular.

``````
import java.util.*;
class lowerTriangular
{
void lowermat(int matrix[][],
int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i < j)
{
System.out.print("0" + " ");
}
else
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}

public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
lowerTriangular lt=new lowerTriangular();
int i,j,count=0,no,size,flag=0;
System.out.println("Enter size");
size =sc.nextInt();
int a[][]=new int[size][size];

for(i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
System.out.println("Enter the element("+i+","+j+")");
a[i][j]=sc.nextInt();
}
}
System.out.println("The given matrix:");
for(i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
System.out.print(a[i][j]);

}
System.out.println();
}
System.out.println("Lower triangular matrix");
lt.lowermat(a,size,size);
}
}``````

Output:

Enter size
3
Enter the element(0,0)
1
Enter the element(0,1)
2
Enter the element(0,2)
3
Enter the element(1,0)
4
Enter the element(1,1)
5
Enter the element(1,2)
6
Enter the element(2,0)
7
Enter the element(2,1)
8
Enter the element(2,2)
9
The given matrix:
123
456
789
Lower triangular matrix
1 0 0
4 5 0
7 8 9

# Diagonal matrix program in java

A square matrix is said to be diagonal matrix if the elements of matrix except main diagonal are zero.

A square null matrix is also a diagonal matrix whose main diagonal elements are zero.

``````import java.util.*;
class DiagonalMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];

/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

int p=0, q=0;

for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i!=j && A[i][j]!=0) // Checking non-diagonal elements
{
p=1;
break;
}
if(i==j && A[i][j]==0) // Checking diagonal elements
{
q++;
}
}
}

if(p==0 && q<m)
System.out.println("The matrix is Diagonal");
else
System.out.println("The matrix is not Diagonal");
}
}``````

Output:

Enter the size of the matrix : 3
Enter an element : (0,0)1
Enter an element : (0,1)0
Enter an element : (0,2)0
Enter an element : (1,0)0
Enter an element : (1,1)3
Enter an element : (1,2)0
Enter an element : (2,0)0
Enter an element : (2,1)0
Enter an element : (2,2)45
The Matrix is :
1 0 0
0 3 0
0 0 45
The matrix is Diagonal

# Scalar matrix program in java

Write a Program in Java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.

Scalar Matrix : A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements except main diagonal are zero. Scalar matrix can also be written in form of n * I, where n is any real number and I is the identity matrix.

``````import java.util.*;
class ScalarMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];

/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element :("+i+","+j+") ");
A[i][j]=sc.nextInt();
}
}

/* Printing the matrix */

System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

int p = 0, q = 0, x = A; // 'x' is storing the 1st main diagonal element

for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
/* Checking that the matrix is diagonal or not */
if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero
{
p=1;
break;
}
/* Checking the matrix for scalarity */
// All main diagonal elements must be equal to 'x' and non-zero
if(i==j && (A[i][j]==0 || A[i][j]!=x))
{
q=1;
break;
}
}
}

if(p==0 && q==0)
System.out.println("The matrix is scalar");
else
System.out.println("The matrix is not scalar");
}
}``````

Output:

Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 2
Enter an element :(0,2) 1
Enter an element :(1,0) 3
Enter an element :(1,1) 1
Enter an element :(1,2) 3
Enter an element :(2,0) 1
Enter an element :(2,1) 1
Enter an element :(2,2) 3
The Matrix is :
1 2 1
3 1 3
1 1 3
The matrix is not scalar
Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 0
Enter an element :(0,2) 0
Enter an element :(1,0) 0
Enter an element :(1,1) 1
Enter an element :(1,2) 0
Enter an element :(2,0) 0
Enter an element :(2,1) 0
Enter an element :(2,2) 1
The Matrix is :
1 0 0
0 1 0
0 0 1
The matrix is scalar

# Frequency of alphabets

``````import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int ci, i, j, k, l=0;
String str, str1;
char c, ch;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a String : ");
str=scan.nextLine();

i=str.length();
for(c='A'; c<='z'; c++)
{
k=0;
for(j=0; j<i; j++)
{
ch = str.charAt(j);
if(ch == c)
{
k++;
}
}
if(k>0)
{
System.out.println("The character " + c + " has occurred for " + k + " times");
}
}
}
}
``````

Enter a String : hai how are you bye take care
The character a has occurred for 4 times
The character b has occurred for 1 times
The character c has occurred for 1 times
The character e has occurred for 4 times
The character h has occurred for 2 times
The character i has occurred for 1 times
The character k has occurred for 1 times
The character o has occurred for 2 times
The character r has occurred for 2 times
The character t has occurred for 1 times
The character u has occurred for 1 times
The character w has occurred for 1 times
The character y has occurred for 2 times

# Decimal to roman number

``````import java.io.*;
class dec2rom
{
public static void main(String args[])throws IOException
{
System.out.println("enter a number");
if(num>0&&num<4000)
{
String thou[]={"","M","MM","MMM"};
String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
int th=num/1000;
int h=(num/100)%10;
int t=(num/10)%10;
int u=num%10;
System.out.println("roman equivalent="+thou[th]+hund[h]+ten[t]+unit[u]);
}
else
System.out.println("\nyou entered a number out of range \nplease enter a number in the range[1-3999]");
}
}
``````

enter a number
1354
roman equivalent=MCCCLIV

# Data Structure- Dequeue Program In Java

``````import java.io.*;
class dequeue
{
int arr[]=new int;
int f,r;
int i,n;
String str;
dequeue()
{
f=-1;
r=-1;
}
public void push()throws IOException
{
if(r==9)
{
System.out.println("Queue Overflow");
return;
}
System.out.println("Specify the location(front or rear):");
System.out.println("Enter the value to insert:");
if(f==-1)
{
arr[++f]=n;
r=0;
}
else if(str.charAt(0)=='f')
{
for(i=r+1;i>f;i--)
arr[i]=arr[i-1];
arr[i]=n;
r++;
}
else
{
arr[++r]=n;
}
}
public void display()
{
if(f==-1)
return;
for(i=f;i<=r;i++)
System.out.println(""+arr[i]);
}
public void pop()throws IOException
{
if(f==-1)
{
System.out.println("Queue Overflow");
return;
}
System.out.println("Specify the location(front  or rear):");
if(f==r)
{
f=-1;
r=-1;
}
else if(str.charAt(0)=='f')
{
f++;
}
else
{
r--;
}
}
public static void main(String args[])throws IOException
{
char op;
dequeue ob=new dequeue();
while(true)
{
System.out.println("\nPress'P' for Push,'D' for Pop and 'Q' for Quit:");

if(op=='p' || op=='P')
ob.push();
else if(op=='d' || op=='D')
ob.pop();
ob.display();
if(op=='q' || op=='Q')
break;
br=null;
}
}
}

``````

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
5

5

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
r
Enter the value to insert:
6

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
4

4

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
r
Enter the value to insert:
7

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
3

3

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
d
Specify the location(f or r):
f

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
d
Specify the location(f or r):
r

4

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
q

4

5

6

# Remove repeated words

write a program in java to remove the repeated strings(words) from the input sentence.

``````import java.io.*;
class repeatwords
{
public static void main(String args[]) throws IOException
{
System.out.println("enter any word");
s=s+" ";
int l=s.length();
String ans="";
char ch1,ch2;
for(int i=0;i<l-1;i++)
{
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2)
{
ans=ans+ch1;
}
}
System.out.println("word after removing repeated characters="+ans);
}
}
``````

Output:

enter any word
javaaaaaa
word after removing repeated characters=java

# Time in words

write a program to convert time in numbers to time in words.

``````import java.io.*;
public class timeinwords
{

public static void main(String args[]) throws IOException
{
System.out.println("enter hours");
System.out.println("enter minutes");
if((h>=1&&h<=12)&&(m>=0&&m<=59))
{
String word[]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten "," eleven "," twelwe "," thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twenty one","twenty two","twenty three","twenty four","twenty five","twenty six","twenty seven","twenty eight","twenty nine"};
String plu,a;
if(m==1||m==59)
plu="minute";
else
plu="minutes";
if(h==12)
a=word;
else
a=word[h+1];
System.out.print("output: "\n+h+":"+m+"-----");
if(m==0)
System.out.println(word[h]+"o'clock");
else  if(m==15)
System.out.println("quarter past"+word[h]);
else  if(m==30)
System.out.println("half past"+word[h]);
else  if(m==45)
System.out.println("quarter to"+a);
else  if(m<30)
System.out.println(word[m]+" "+plu+" past+words[h] ");
else
System.out.println(word[60-m]+" "+plu+" to "+a);
}
else
System.out.println("invalid input!");
}
}

``````

output:

enter hours
10
enter minutes
55
output:
10:55—–five minutes to eleven

# Reverse the string

``````
import java.io.*;
import java.util.*;
class revstr
{
public void main()throws IOException
{
Scanner sc=new Scanner(System.in);
String str="",word;
int l,l1,i;
String a[]=new String;
System.out.println("enter string");
str=sc.nextLine();
StringTokenizer data=new StringTokenizer(str);
l=data.countTokens();
for(i=0;i<l;i++)
{
word=data.nextToken();
a[i]=word;
}
for(i=l-1;i>=0;i--)
{
System.out.print(a[i]+" ");
}
}
}

``````

OUTPUT:

enter string
one important thing to know, seattle delivered his speech in his native language.
language. native his in speech his delivered seattle know, to thing important one

# Prime Triplets

``````import java.util.*;
class primeTriplets
{
boolean isPrime(int n) //funton for checking prime
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the lower and upper range:");
int m=sc.nextInt();
int n=sc.nextInt();
primeTriplets pt=new primeTriplets();
int prime=0;
//pt.get();
if(m>0 && n> 0  && m<n)
{
System.out.println("Prime Triplets:");
for(int i=m;i<n;i++)
{
if(pt.isPrime(i)==true && pt.isPrime(i+2)==true && pt.isPrime(i+6))
{System.out.print(i+" " + (i+2) +" " +(i+6)+"\n");
prime++;
}
else if(pt.isPrime(i)==true && pt.isPrime(i+4)==true && pt.isPrime(i+6))
{  System.out.print(i+" " + (i+4) +" " +(i+6)+"\n");
prime++;
}

else
{
}
}

}
else
System.out.println("invalid");
System.out.println("Total prime triplets are:"+prime);
}
}
``````

Output:

Enter the lower and upper range:
2
17
Prime Triplets:
5 7 11
7 11 13
11 13 17
13 17 19
Total prime triplets are :  4

# Exercise 1 :. Write a java program to check Prime Triplets

The consecutive Prime numbers are known as Prime Triplets if they satisfy the follows;
(n,n+2,n+6) are all Prime or (n,n+4,n+6) are all Prime . Where n is an integer number >0

if n=5,then 5,7,11 are all Prime so they arre Prime Triplets.

If n=7, then (7,7+2, 7+6) 7,9,13 all are not primes so they are not the Prime Triplets.

But if n=7, then (7,7+4,7+6) 7,11,13 all are Prime and they are Prime Triplets.

Write a program to input a start limit S>0 and a last limit L>0. Print all the Prime Triplets between S and L with sutiable message . The Prime Triplets may be greater or be greater or lesser than L depending upon the conditions used for generating Prime combinations. Print the total number of primer Triplets at the end.

Input
s=3, l=15

Output
Prime Triplets
5 7 11
7 11 13
11 13 17
13 17 19
Total Prime Triplet combinations are =4

Write a program.

# Twin prime number program in java

A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers.

In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair.

`3, 5), (5, 7), (11, 13), (17, 19), (29, 31), `
``````import java.io.*;
class TwinPrime
{
boolean isPrime(int n) //funton for checking prime
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}

public static void main(String args[]) throws IOException
{
TwinPrime tw= new TwinPrime();

System.out.print("Enter the lower range : ");
System.out.print("Enter the upper range : ");

if(p>q)
System.out.println("Invalid Range !");
else
{
System.out.println("nThe Twin Prime Numbers within the given range are : ");
for(int i=p; i<=(q-2); i++)
{
if(tw.isPrime(i) == true && tw.isPrime(i+2) == true)
{
System.out.print("("+i+","+(i+2)+") ");
}
}
}
}
}``````

Output:

Enter the lower range : 12
Enter the upper range : 24
The Twin Prime Numbers within the given range are :
(17,19)

# Mobius Function in java

The MOBIUS function M(N) for a natural number N is defined as follows:

1. M(N) = 1                          if N = 1

2. M(N) = 0                          if  any prime factor of N is contained in N more than once

3. M(N) = (-1)p                    if N is a product of ‘p’ distinct prime factors

Example :

M(78) = -1                ( for 78 = 2 * 3 * 13     M(78) = ( -1)3 = -1 )

M(34) = 1                 ( for 34 = 2 * 17           M(34) = ( -1)2 = 1 )

M(12) = 0                 ( for 12 = 2 * 2 * 3       M(12) = 0 for 2 appears two times)

M(17) = -1                ( for 17 = 17                 M(17) = ( -1)1 = -1 )

``````import java.util.*;
class MobiusFun
{
int n;

MobiusFun()
{
n = 0;
}

void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n = sc.nextInt();
}

/*  The function primefac() either returns '0' if prime factors are repeated
*  or returns the no.of prime factors */
int primeFac()
{
int a=n, i=2, m=0, c=0, f=0;

while(a > 1) // loop to generate prime factors
{
c = 0; // variable to store frequency of every prime factor
while(a%i == 0) // if 'i' is a prime factor
{
c++; // counting frequency of 'i'
f++; // counting no of prime factors
a=a/i;
}
i++;

if(c > 1) // returning '0' if prime factors are repeated
return 0;
}
return f; // returning no. of prime factors
}

void display() // function to display value of mobius function
{
int mob,x;
if(n == 1) // condition 1
mob = 1;
else
{
x = primeFac();
if(x == 0) // condition 2
mob = 0;
else // condition 3
mob = (int)Math.pow(-1,x);
}
System.out.println("Value of Mobius Function : "+mob);
}
public static void main(String args[])
{
MobiusFun ob = new MobiusFun();
ob.input();
ob.display();
}
}``````

Output:

Enter a number : 56
Value of Mobius Function : 0
Enter a number : 78
Value of Mobius Function : -1
Enter a number : 34
Value of Mobius Function : 1

# MagicComposite

A Composite Magic number is a positive integer which is composite as well as a magic number.

Composite number:
A composite number is a number that has more than two factors.
For example: 10
Factors are: 1, 2, 5, 10

Magic number:
A magic number is a number in which the eventual sum of the digits is equal to 1
For example: 28=2+8=10=1+0=1

Accept two positive integers m and n, where m is less than n as user input. Display the number of Composite magic integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format specified below.

Example 1:

INPUT:

m = 100
n = 200

OUTPUT:

The Composite Magic Integers are:
100, 118, 136, 145, 154, 172, 190
The frequency of Composite Magic Integers is : 7

``````import java.io.*;
class MagicComposite
{
boolean isComposite(int n) // Function to check for Composite number
{
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count>2)
return true;
else
return false;
}

int sumDig(int n) // Function to return sum of digits of a number
{
int s = 0;
while(n>0)
{
s = s + n%10;
n = n/10;
}
return s;
}

boolean isMagic(int n) // Function to check for Magic number
{
int a = sumDig(n);
while(a>9)
{
a = sumDig(a);
}

if(a == 1)
return true;
else
return false;
}

public static void main(String args[])throws IOException
{
MagicComposite mc = new MagicComposite();

System.out.print("Enter the lower limit(m) : ");
System.out.print("Enter the upper limit(n) : ");

int c=0;
if (m<n)
{
System.out.println("The Composite Magic Integers are: ");
for(int i=m; i<=n; i++)
{
if(mc.isComposite(i)==true && mc.isMagic(i)==true)
{
if (c==0) // Printing the first number without any comma
System.out.print(i);
else
System.out.print(", "+i);
c++;
}
}
System.out.println("\nThe frequency of Composite Magic Integers is : "+c);
}
else
System.out.println("INVALID RANGE");
}
}``````

Output:

Enter the lower limit(m) : 123
Enter the upper limit(n) : 567
The Composite Magic Integers are:
136, 145, 154, 172, 190, 208, 217, 226, 235, 244, 253, 262, 280, 289, 298, 316, 325, 334, 343, 352, 361, 370, 388, 406, 415, 424, 442, 451, 460, 469, 478, 496, 505, 514, 532, 550, 559
The frequency of Composite Magic Integers is : 37

# Magic number

A number is said to be a Magic number if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If the single digit comes to be 1 then the number is a magic number.

``````
import java.util.*;
class MagicNum
{
public static boolean isMagic(int n)
{
int sum = 0;

// Note that the loop continues
// if n is 0 and sum is non-zero.
// It stops when n becomes 0 and
// sum becomes single digit.
while (n > 0 || sum > 9)
{
if (n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}

// Return true if sum becomes 1.
return (sum == 1);
}

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n ,c;
System.out.println("Enter the number:");
n=sc.nextInt();
if (isMagic(n))
System.out.println("Magic Number");

else
System.out.println("Not a magic Number");
}
} ``````

Output:

Enter the number:
123
Not a magic Number
Enter the number:
1234
Magic Number

# Disarium number program in java

A Disarium number is a number defined by the following process:

Sum of its digits powered with their respective position is equal to the original number. Some other DISARIUM are 89, 135,175, 518 etc

`Input   : n = 135Output  : Yes 1^1 + 3^2 + 5^3 = 135Therefore, 135 is a Disarium number`
``````import java.io.*;
class DisariumNumber
{
public static void main(String[] args)throws IOException
{
System.out.print("Enter a number : ");
int copy = n, rem = 0, sum = 0;
String s = Integer.toString(n); //converting the number into a String
int len = s.length(); //finding the length of the number

while(copy>0)
{
rem = copy % 10; //extracting the last digit
sum = sum + (int)Math.pow(rem,len);
len--;
copy = copy / 10;
}
if(sum == n)
System.out.println(n+" is a Disarium Number.");
else
System.out.println(n+" is not a Disarium Number.");
}
}``````

Output:

Enter a number : 135
135 is a Disarium Number.
Enter a number : 121
121 is not a Disarium Number.

# Java program to convert from octal to decimal

Program to  convert  octal  to decimal

``````import java.util.*;
public class Oct2Decimal{

//method definition to convert octal numbers into decimal

public static int getDecimal(int octal){
int decimal = 0;
//Declaring variable to use in power
int ct = 0;
while(true){
if(octal == 0){
break;
} else {
int temp = octal % 10;
decimal += temp*Math.pow(8, ct); //
octal = octal/10;
ct++;
}
}
return decimal;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter an octal number:");
int num=sc.nextInt();
Oct2Decimal od=new Oct2Decimal();

System.out.println("Decimal of given octal is: "+ od.getDecimal(num));
}
}
``````

Enter an octal number:
11
Decimal of given octal is: 9
Enter an octal number:
2
Decimal of given octal is: 2
Enter an octal number:
134
Decimal of given octal is: 92