1.
ISBN NUMBER
The international standard book number (ISBN) is a unique numeric book
identifier which is printed on every book.
The ISBN is based upon a 10-digit code.
import java.util.*;
class ISBN
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your ISBN number") ;
long num=sc.nextLong();
long s=0;
int c=1;
while(num>0)
{
long r=num%10;
s=s+(r*c);
c++;
num=num/10;
}
if(s%11==0)
System.out.println("It is a ISBN number");
else
System.out.println("It is not a ISBN number");
}
}
2.PRONIC NUMBER
Write a program to input a number and check and print whether it is
a Pronic number or not. Pronic number is the number which is the product of
two consecutive integers.
import java.util.*;
class Pronic
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int c=0;
for(int i=1;i<=n/2;i++)
if((n%i)==0 && (n%(i+1))==0)
c++;
}
if(c==0)
System.out.println("Not a pronic number");
else
System.out.println("It is a pronic number");
}
}
3.ARMSTRONG NUMBER
Armstrong number of three digit, the sum of cubes of each digits is equal to the
number itself.
import java.util.*;
class Armstrong
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a three digit
number");
int n=sc.nextInt();
int d1=n/100;
int d2=(n/10)%10;
int d3=n%10;
if((d1*d1*d1+d2*d2*d2+d3*d3*d3)==n)
System.out.println("It is a Armstrong number");
else
System.out.println("It is not a Armstrong number");
}
}
4.SPECIAL NUMBER
A number is said to be a Special number, if the sum of factorials of the input
number's every digit is equal to the same input number.
import java.util.*;
class Special
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int num=n;
int sum=0;
while(num>0)
int r=num%10;
int fact=1;
for(int i=1;i<=r;i++)
fact=fact*i;
sum+=fact;
num=num/10;
}
if(sum==n)
System.out.println("It is a Special number");
else
System.out.println("It is not a special number");
}}
5.TWIN PRIME NUMBER
Write a program to input two numbers and check whether they are twin prime
numbers or not.
import java.util.*;
class TwinPrime
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
System.out.println("Enter another number");
int n2=sc.nextInt();
int c=0,c2=0;
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
for(int i=1;i<=n2;i++)
if(n2%i==0)
c2++;
}
if(c==2&&c2==2)
{
if((n-n2)==2 || (n2-n)==2)
System.out.println("They are twin prime numbers");
else
System.out.println("They are not twin prime numbers");
} else
System.out.println("They are not twin prime numbers");
}
}
6.TWISTED PRIME
A prime number is said to be 'Twisted Prime', if the new number obtained after
reversing the digits is also a prime number. Write a program to accept a number
and check whether the number is 'Twisted Prime' or not.
import java.util.*;
class TwistedPrime
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int n=num;
int c=0;
for(int i=1;i<=num;i++)
{if(num%i==0)
c++;
int s=0;
while(num>0)
{int r=num%10;
s=(s*10)+r;
num=num/10;
}
int d=0;
for(int i=1;i<=s;i++)
{if(s%i==0)
d++;
if(c==2 && d==2)
System.out.println( “\'twisted
prime\' ");
else
System.out.println(n+" is not a \'twisted prime\'");
}
}
7.DUCK NUMBER
Write a Java Program to check whether a number is a Duck number or not. Note:
A duck number is a number which has zeroes present in it, but there should be no
zero present in the beginning of the number.
import java.util.*;
class DuckNum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
String n=sc.nextLine();
boolean D=false;
if(n.charAt(0)=='0')
D=false;
else
{
for(int i=1;i<n.length();i++)
{
if(n.charAt(i)=='0')
D=true;
}
}
if(D==true)
System.out.println("It is a Duck Number");
else
System.out.println("It is not a Duck Number");
}
}
8.ABUNDANT NUMBER
Write a program to input a number and check whether it is an abundant number or
not. A number for which the sum of its proper devisor is greater than the number
itself.
import java.util.*;
class AbundantNum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0)
s=s+i;}
if(s>n)
System.out.println("It is a Abundant number");
else
System.out.println("It is not a Abundant number");
}
}
9.SPY NUMBER
import java.util.*;
class SpyNum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0;
int m=1;
while(n>0)
int r=n%10;
s=s+r;
m=m*r;
n=n/10;
if(m==s)
System.out.println("It is a spy number");
else
System.out.println("It is not a spy number");
10.HARSHAD NUMBER
Write a program to input a number and check whether it is a Harshad number or
not. A number is said to be Harshad number, if it is divisible by the sum of its
digits. The program displays the message accordingly.
import java.util.*;
class Harshad
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int num=n;
int s=0;
while(n>0)
int r=n%10;
s=s+r; n=n/10;
if(num%s==0)
System.out.println("Sum of digits = "+s+" and "+num+" is divisible by "+s);
System.out.println("Output: It is a Harshad Number");
}
else
{
System.out.println("Sum of digits = "+s+" and "+num+" is not divisible by "+s);
System.out.println("Output: It is not a Harshad Number");
}
11.LEAD NUMBER
Write a program to input a number and check whether it is a palindrome number
or not. A palindrome number is a number whose reverse is equal to the original
number input by the user.
import java.util.*;
class LeadNum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int even=0;
int odd=0;
while(n>0)
int r=n%10;
if(r%2==0)
even=even+r;
else
odd=odd+r;
n=n/10;
if(odd==even)
System.out.println("It is a lead number");
else
System.out.println("It is not a lead number");
}
}
12.PALINDROME
Write a program to input a number and check whether it is a palindrome number
or not. A palindrome number is a number whose reverse is equal to the original
number input by the user.
import java.util.*;
class Palindrome
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int n=num;
int s=0;
while(num>0)
int r=num%10;
s=(s*10)+r;
num=num/10;
if(s==n)
System.out.println("The number is palindrome");
else
System.out.println("The number is not palindrome");
}
13.SWITCH PROGRAM
Using switch statement, write a menu driven program for the following:
(a)To find and display the sum of the series given below:
S=x¹-x²+x³-x⁴+x⁵-...................-x²⁰
(b)to find and display the sum of the series given below:
S=1/a²+1/a⁴+1/a⁶+1/a⁸+..................to n terms
Or and incorrect option, and appropriate error message should be displayed
import java.util.*;
class Switch
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Menu");
System.out.println("Enter \'a\' for displaying the sum of the series x^1 -x^2
+x^3 -x^4 +x^5 x^20");
System.out.println("Enter \'b\' for displaying the sum of the series 1/a^2 +1/a^4
+ 1/a^6
+1/a^8 to n terms");
char ch=sc.next().charAt(0);
switch(ch)
case 'a':System.out.println("Enter the value for x");
int x=sc.nextInt();
double sum=0;
for(int i=1;i<=20;i++)
{if(i%2==0)
sum=sum-Math.pow(x,i);
else
sum=sum+Math.pow(x,i);}
System.out.println("The sum of the series is "+sum);
break;
case 'A':System.out.println("Enter the value for
x");
int X=sc.nextInt();
double Sum=0;
for(int i=1;i<=20;i++)
{
if(i%2==0)
Sum=Sum-Math.pow(X,i);
else
Sum=Sum+Math.pow(X,i);
}
System.out.println("The sum of the series is "+Sum);
break;
case 'b':System.out.println("Enter the value for a and
n");
int a=sc.nextInt();
int n=sc.nextInt();
double sum2=0;
for(int i=1;i<=n;i++)
{sum2=sum2 + 1/(Math.pow(a,i*2));
}
System.out.println("The sum of the series is "+sum2);
break;
case 'B':System.out.println("Enter the value for a and
n");
int A=sc.nextInt();
int N=sc.nextInt();
double Sum2=0;
for(int i=1;i<=N;i++)
{Sum2=Sum2 + 1/(Math.pow(A,i*2));
}
System.out.println("The sum of the series is "+Sum2);
break;
default:System.out.println("Invalid!");
}
}
}
14.MATHEMATICAL MEANS
Write a menu driven program using switch case statement to find the Arithmetic
mean, geometric mean and harmonic mean.
import java.util.*;
class Mean
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the two numbers whose mean is to be
calculated");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Menu");
System.out.println("Enter \'a\' to calculate the Arithmetic mean=(a+b)/2");
System.out.println("Enter \'b\' to calculate the Geometric
mean=sqrt(a*b)");
System.out.println("Enter \'c\' to calculate the Harmonic
mean=(2*a*b)/(a+b)");
char ch=sc.next().charAt(0);
double mean=0;
switch(ch)
{
case 'a': mean=(a+b)/2;
System.out.println("The mean is "+mean);
break;
case 'b': mean=Math.sqrt(a*b);
System.out.println("The mean is "+mean);
break;
case 'c': mean=(2*a*b)/(a+b);
System.out.println("The mean is "+mean);
break;
case 'A': mean=(a+b)/2;
System.out.println("The mean is "+mean);
break;
case 'B': mean=Math.sqrt(a*b);
System.out.println("The mean is "+mean);
break;
case 'C': mean=(2*a*b)/(a+b);
System.out.println("The mean is "+mean);
break;
default:System.out.println("Invalid!");
}
}
}
15.TRIANGLE
Write a program to input three angles of a triangle and check whether a triangle is
possible or not. If
possible then check whether it is an acute angled triangle, right angle triangle
or or obtuse angled triangle otherwise, display 'triangle not possible'.
import java.util.*;
class Triangles
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the three angles of a
triangle");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a+b+c==180 && a!=0 && b!=0 && c!=0)
{
if(a==90 || b==90 || c==90)
System.out.println("Right-angled Triangle");
else if(a>90 || b>90 || c>90 )
System.out.println("Obtuse-angled Triangle");
else
System.out.println("Acute-angled Triangle");
} else
System.out.println("Triangle not possible");
}
16.WORD SORTING
Write a program in Java to input a word. For the word alphabetically, removing
all repetitive characters
(a character should come only once). Display the original word and sorted word
with appropriate messages.
import java.util.*;
class Arrangement
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.next();
int l=s.length();
String w=" ";
int C=0;
for(char x='a',y='A';x<='z';x++,y++)
for(int i=0;i<l;i++) {
char ch=s.charAt(i);
if(w.charAt(C)==ch)
continue;
else if(x==ch || y==ch)
{
w=w+ch;
C++;
}
}
w=w.trim();
System.out.println("The original word is: "+s);
System.out.println("The sorted word is : "+w);
}
}
17.PALINDROME STRING
Write a program to enter a string and check whether it is palindrome or not.
import java.util.*;
class PalindromeStr
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.next();
s=s.trim();
String Str="";
for(int i=s.length()-1;i>=0;i--)
Str=Str+s.charAt(i);
if(s.equalsIgnoreCase(Str)==true)
System.out.println("It is a palindrome");
else
System.out.println("It is not a palindrome");
}
}
18.STRING CONVERT
Define a class to accept a string and convert the same to upper case, create and
display the news by replacing
each vowel by immediate next character and every consonant by the previous
character. The other characters remain same.
import java.util.*;
class StrConvert
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.next();
String w="";
for(int i=0;i<s.length();i++)
char ch=s.charAt(i);
int x="AEIOU".indexOf(ch);
if(Character.isLetter(ch))
if(x>=0)
ch++;
Else ch—;
}
w=w+ch;
}
System.out.println("The converted string is "+w);
}
}
19.ARRAY TRANSFER
Define a class to declare and integer array of size 10 and accept the elements into
the array from the user.
Create another array of same size and transfer all even elements from left and odd
elements from the right side.
import java.util.*;
class Array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
System.out.println("Enter 10 elements”);
for(int i=0;i<=9;i++)
System.out.println("enter");
arr[i]=sc.nextInt();
int a[]=new int[10];
int e=0,o=9;
for(int i=0;i<=9;i++)
if(arr[i]%2==0) {
a[e]=arr[i];
e++;
else
a[o]=arr[i];
o--;
}
System.out.println("The arrays are");
System.out.print("Array A : ");
for(int i=0;i<=9;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
System.out.print("Array B : ");
for(int i=0;i<=9;i++)
{System.out.print(a[i]+" ");
}
}
}
20.BINARY SEARCH
Write a program in Java initialise and integer array with the following values:
98,85,82,70,66,51,44,34,23,11
Enter a number from user, search using binary search searching technique.
Display whether the search was successful or not. If yes, then print the index
position of the number in the array.
import java.util.*;
class BinarySearch
{
public static void main()
{
int arr[]={98,85,82,70,66,51,44,34,23,11};
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to be
searched");
int n=sc.nextInt();
boolean Ans=false;
int l=0;
int u=9;
int index=0;
while(l<=u)
{
int m=(l+u)/2;
if(n>arr[m])
u=m-1;
else if(n<arr[m])
l=m+1;
else if(n==arr[m])
{
Ans=true;
index=m;
break;
}
if(Ans)
{
System.out.println("The search was successful");
System.out.println("Index number : "+index);
else
System.out.println("The search was unsuccessful");
}
}