Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Write a Program in Java to input a number and check whether it is a Fascinating Number or not..
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property. The property is
such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the
original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.
Consider the number 192,
192
192
192
384
192
576
Programming Code:
import java.util.*;
class FascinatingNumber
{
void isFascinating( int q)
{
int r=q*1;
int s=q*2;
int t=q*3;
String num="";
num=num+r+s+t;
int i;
for(i=0;i<num.length();i++)
{
int count=0;
char c= num.charAt(i);
for(int j=0;j<num.length();j++)
{
char ch=num.charAt(j);
if(c==ch)
count++;
}
if(count>1)
break;
}
if(i==num.length())
System.out.println(q+" is a Fascinating Number");
else
System.out.println(q+" is not a Fascinating Number");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
ob.isFascinating(n);
}
}
Question:
Write a Program in Java to input a number and check whether it is a Pronic Number or not.
Pronic Number : A pronic number, is a number which is the product of two consecutive integers, that is, n (n
+ 1).
The
first
few
pronic
numbers
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 etc.
Programming Code:
import java.util.*;
class PronicNumber
{
void isPronic(int q)
{
int flag = 0;
for(int i=1; i<=q; i++)
{
if(i*(i+1) == q)
{
flag = 1;
break;
are:
}
}
if(flag == 1)
System.out.println(q+" is a Pronic Number.");
else
System.out.println(q+" is not a Pronic Number.");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
PronicNumber ob=new PronicNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
ob.isPronic(n);
}
}
Question:
Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or
not..
Harshad Number : A Harshad number is an integer that is divisible by the sum of its digits.
The number 18 is a Harshad 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
first
few
Harshad
Programming Code:
import java.util.*;
class HarshadNumber
{
void isHarshad(int q)
{
numbers
in
base
10
are:
int c = q, d, sum = 0;
while(c>0)
{
d = c%10;
sum = sum + d;
c = c/10;
}
if(q%sum == 0)
System.out.println(q+" is a Harshad Number.");
else
System.out.println(q+" is not a Harshad Number.");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
HarshadNumber ob=new HarshadNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
ob.isHarshad(n);
}
}
Question:
Write a Program in Java to input a number and check whether it is a Disarium Number or not.
A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the
original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)
Programming Code:
import java.io.*;
class DisariumNumber
{
void isDisarium(int q)
{
int copy = q, d = 0, Sum=0;
String s = integer.toString(q);
int len = s.length();
while(copy>0)
{
d = copy % 10;
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == q)
System.out.println(q+" is a Disarium Number.");
else
System.out.println(q+" is not a Disarium Number.");
}
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a number : ");
int n = Integer.parseInt(br.readLine());
DisariumNumber ob=new DisariumNumber();
ob.isDisarium(n);
}
}
Question:
A Smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors
obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94,
121 ..
Examples:
1. 666
Prime factors are 2, 3, 3, and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7)) = 18
Programming Code:
import java.io.*;
class Smith
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//function for finding sum of digits
int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");
}
}
Question:
Write a Program in Java to print all the Twin Prime numbers within a given range.
Note: Twin Prime numbers are a pair of numbers which are both prime and their difference is 2.
Example: Twin
Prime
numbers
in
the
Programming Code:
import java.io.*;
class TwinPrimeRange
{
boolean isPrime(int n)
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
range
to
100
are
return true;
else
return false;
}
if(p>q)
System.out.println("Invalid Range !");
else
{
System.out.println("The Twin Prime Numbers within the given range are : ");
for(int i=p; i<=(q-2); i++)
{
if(ob.isPrime(i) == true && ob.isPrime(i+2) == true)
{
System.out.print("("+i+","+(i+2)+") ");
}
}
}
}
}
Question:
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value
is equal to the number input, output the message special-two digit number otherwise, output the message
Not a special two-digit number.
Example:
Consider
the
Product
of
number
its
59.Sum
digits
of
5
digits
x
=
9
5+9=14
=
45
Programming Code:
import java.io.*;
class SpecialNumber
{
public void isSpecial(int q)
{
int first, last, sum, pro;
if(q<10 || q>99)
System.out.println("Invalid Input! Number should have 2 digits only.");
else
{
first = q/10;
last = q%10;
sum = first + last;
pro = first * last;
if((sum + pro) == q)
{
System.out.println("The number "+q+" is a Special Two-Digit Number.");
}
else
{
System.out.println("The number "+q+" is Not a Special Two-Digit Number.");
}
}
}
}
}
Question:
Write a Program in Java to input a number and check whether it is a Unique Number or not.
Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7,
135, 214 are all unique numbers whereas 33, 3121, 300 are not.
Programming Code:
import java.io.*;
class UniqueNumber
{
void isUnique(int q)
{
String s=Integer.toString(q);
int l=s.length();
int flag=0;
for(int i=0;i<l-1;i++)
{
for(int j=i+1;j<l;j++)
{
if(s.charAt(i)==s.charAt(j))
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");
}
}
}
Question:
An Emirp Number is anumber which is prime both forwards and backwards. Example, 13 is prime as well as
31 is also a prime number.Therefore 13 is an Emirp number
Programming Code:
import java.util.*;
class Emirp
{
int isPrime(int x)
{
int i,n=0;
for(i=1;i<=x;i++)
{
if(x%i==0)
n++;
}
return n;
}
void isEmirp(int q)
{
int copy=q,d=0,rev=0;
while(copy>0)
{
d=copy%10;
rev=rev*10+d;
copy=copy/10;
}
int a=isPrime(q);
int b=isPrime(rev);
else
System.out.println("It is Not an Emirp Number");
}
int n=sc.nextInt();
Emirp ob=new Emirp();
ob.isEmirp(n);
}
}
Question:
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.To verify an ISBN, calculate 10 times the first digit, plus 9 times the second
digit, plus 8 times the third and so on until we add 1 time the last digit. If the final number leaves no
remainder when divided by 11, the code is a valid ISBN.
Example:. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
Programming Code:
import java.io.*;
class ISBN
{
void isISBN(String s)
{
int len=s.length();
if(len!=10)
System.out.println("Output : Invalid Input");
else
{
char ch;
int dig=0, sum=0, k=10;
for(int i=0; i<len; i++)
{
ch=s.charAt(i);
sum=sum+ch*(k);
k--;
}
System.out.println("Output : Sum = "+sum);
if(sum%11==0)
System.out.println("Leaves No Remainder - Valid ISBN Code");
else
System.out.println("Leaves Remainder - Invalid ISBN Code");
}
}
public static void main(String args[])throws IOException
{
Question:
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 righthand 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 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Solution:
import java.io.*;
class Kaprekar
{
public int isKaprekar(int num)
{
int sq = num*num;
String s = Integer.toString(sq);
if(sq<=9)
s = "0"+s;
int l = s.length();
int mid = l/2;
String left=s.substring(0,mid);
String right=s.substring(mid);
int x = Integer.parseInt(left);
int y = Integer.parseInt(right);
int sum=x+y;
return(sum);
}
Question:
Write a program to input a word from the user and remove the consecutive repeated characters by replacing
the sequence of repeated characters by its single occurrence.
Example:
INPUTjaaavvvvvvvvaaaaaaaaaaa
OUTPUT Java
import java.io.*;
class RemoveRepChar
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word: ");
String s = br.readLine();
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);
}
}
Question:
Write a program to find the shortest and the longest word in a sentence and print them along with their length.
Sample Input: I am learning Java
Sample Output:
Shortest word = I
Length = 1
Longest word = learning
Length = 8
import java.io.*;
class Short_long_word
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any sentence : ");
String s=br.readLine();
s=s+" ";
int len=s.length();
String x="",maxw="",minw="";
char ch;
int p,maxl=0,minl=len;
for(int i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
x=x+ch;
}
else
{
p=x.length();
if(p<minl)
{
minl=p;
minw=x;
}
if(p>maxl)
{
maxl=p;
maxw=x;
}
x="";
}
}
System.out.println("Shortest word = "+minw+"\nLength = "+minl);
System.out.println("Longest word = "+maxw+"\nLength = "+maxl);
}
}
SUrname
import java.io.*;
public class Surname
{
public static void main(String args[])throws IOException
{
REVERSE STRING
import java.io.*;
public class StringReverse
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String st3="",st2="",st1="";
int x,y,z;
char b=0;
System.out.println("Enter a String");
st1=in.readLine();
st1=st1+" ";
x=st1.length();
for(y=--x;y>=0;y--)
st2+=st1.charAt(y);
System.out.println("The string writtn in the reversed order is");
System.out.println(st2);
}
}
Write a program to accept a number and check whether the given number is
palindrome or not, using function name reverse(int n) which returns the number
after reversing the digits.
import java.util.*;
public class Palindrome
{
public int reverse(int q)
{
int sum=0,i=1;
while(q!=0)
{
int rem=q%10;
sum+=(rem*i);
i*=10;
}
return(sum);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
int num=sc.nextInt();
int z=num;
Palindrome obj=new Palindrome();
int rev =obj.reverse(num);
if(rev==z)
System.out.println("Not a Palindrome Number");
else
System.out.println("Palindrome Number");
}
}
Write a program to input a number a number from the user and to check whether
the number is a neon number or not.
Sample Input : 9
Sample Output :9 is a Neon Number.
import java.util.*;
public class Neon
{
public int squaresum(int n)
{
int d=0;
while(n!=0)
{
int r=n%10;
d+=r;
n/=10;
}
return(d);
}
public static void main(String args[])
{
import java.util.*;
public class ArmstrongNumber
{
public int cube(int n)
{
int d=0,i=1;
while(n!=0)
{
int r=n%10;
d+=(r*r*r);
n/=10;
}
return(d);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
int num=sc.nextInt();
int z=num;
ArmstrongNumber obj=new ArmstrongNumber();
int c =obj.cube(num);
if(c==z)
System.out.println("Armstrong Number");
else
System.out.println("Not a Armstrong Number");
}
}
Magic number
import java.util.*;
public class MagicNumber
{
public boolean num(int n)
{
int s=0,d=0;
while(s>9)
{
n=s;
s=0;
while(n>0)
{
d=n%10;
s=s+d;
n/=10;
}
}
if(s==1)
return true;
else
return false;
}
public static void main(String args[])
{
int s=0,d=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Number");
int n=sc.nextInt();
MagicNumber obj=new MagicNumber();
boolean x=obj.num(n);
if(x==true)
System.out.println("Magic Number");
else
System.out.println("Not A Magic Number");
}
}
Write a program to input a Sentence from the user and two character variables
one of them which we have to replace with the other and display the new string
after converting it into a Piglatin String.
Sample Input : Landan
Replace ' A ' with ' O '.
Sample Output : London
Ondonlay
import java.util.*;
public class Replace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence ");
String s=sc.nextLine();
int i;
System.out.println("Enter A Character to be replaced");
char c=sc.nextLine().charAt(0);
System.out.println("Enter A Character that has to be placed in place of previous
Character");
char ch=sc.nextLine().charAt(0);
s=s.replace(c,ch);
for(i=0;i<s.length();i++)
{
char cha=s.charAt(i);
if(cha=='a'||cha=='e'||cha=='i'||cha=='o'||cha=='u')
break;
}
System.out.println(s.substring(i)+s.substring(0,i)+"ay");
}
}
Write a program to input a sentence in lower case and upper case both print the
new string after deleting all the Upper case letters of the String. The program
also prints the number of deleted Upper case letters
Sample Input : Unerstanding Computer Applications Class X
Sample Output : nderstanding omputer pplications lass
Number of deleted Uppercase Letters : 5
import java.util.*;
public class Delete_UpperCase
{
public static void main(String args[])
{
int c=0,i;String st="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence");
String s=sc.nextLine();
for(i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(Character.isUpperCase(ch)==false)
st+=ch;
else
c++;
}
System.out.println("New String After Deleting Upper Case Letters : ");
System.out.println();
System.out.println(st);
System.out.println();
System.out.println("Number Of Deleted Upper Case Letters : "+c);
}
}
}
}
Write a program to accept a sentence from the user and display the consecutive
letters in the String.
Sample Input : Understanding Computer Applications
Sample Output : D and E
R and S
S and T
import java.util.*;
public class Consecutive_Letters
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence");
String s=sc.nextLine();
System.out.println();
System.out.println("Consecutive Letters are :");
for(int i=0;i<s.length()-1;i++)
{
char c1=s.charAt(i);
char c2=s.charAt(i+1);
if(c2-c1==1)
System.out.println(c1+" and "+c2);
}
}
}
import java.io.*;
class Swap_Strings
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.print("Enter the 1st String : ");
String s1=br.readLine();
int len1=s1.length();
System.out.println("-------------------------------");
System.out.println("Strings Before Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
s1=s1+s2;
s2=s1.substring(0,len1);
s1=s1.substring(len1);
System.out.println("-------------------------------");
System.out.println("Strings After Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
}
}
COMBINATION OF LETTERS
import java.io.*;
}
}
}
}
FIND THE SHORTEST AND LONGEST WORDS IN A STRING
import java.io.*;
class Short_long_word
{
s=s+" ";
int len=s.length();
String x="",maxw="",minw="";
char ch;
int p,maxl=0,minl=len;
for(int i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
x=x+ch;
}
else
{
p=x.length();
if(p<minl)
{
minl=p;
minw=x;
}
if(p>maxl)
{
maxl=p;
maxw=x;
}
x="";
}
}
System.out.println("Shortest word = "+minw+"\nLength = "+minl);
System.out.println("Longest word = "+maxw+"\nLength = "+maxl);
}
}
DISPLAY STRING IN ALPHABETICAL ORDER
import java.io.*;
public class AlphabeticalOrder
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String str;
int i,j,p;
char chr;
System.out.println("Enter your String");
str=in.readLine();
p=str.length();
for(i=65;i<=90;i++)
{
for(j=0;j<p;j++)
{
chr=str.charAt(j);
if(chr==(char)i||chr==(char)(i+32))
System.out.println(chr);
}
}
}
}
Print two string such that there are chars of even pos of 1 st string and odd pos chars of
second string. However the loop should run till the length of the shorter string
import java.io.*;
public class Frame
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String str1,str2,str3=" ";
int i,j,l;
char chr1,chr2;
System.out.println("Enter first String");
str1=in.readLine();
System.out.println("Enter second String");
str2=in.readLine();
int l1=str1.length();
int l2=str2.length();
l=Math.min(l1,l2);
for(i=0;i<l;i+=2)
{
chr1=str1.charAt(i);
str3=str3+chr1;
chr2=str2.charAt(i+1);
str3=str3+chr2;
}
System.out.println("The new String");
System.out.println(str3);
}
}
import java.io.*;
public class PalindromeString
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String word1="";
int i,p;
char chr;
System.out.println("Enter your String");
String word=in.readLine();
p=word.length();
for(i=p-1;i>=0;i--)
{
chr=word.charAt(i);
word1=word1+chr;
}
if(word.equals(word1))
System.out.println("The String is a Palindrome String");
else
System.out.println("The String is not a palindrome String");
}
}
/**
* Write a description of class CommonCharacters here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class RemoveCommonCharacters
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Strings");
String s=sc.nextLine();
String st=sc.nextLine();
String ans="";
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(st.indexOf(c)==-1)
ans += c;
}
System.out.print("Non-Common Characters in 1st Strings are :");
System.out.print(ans);
System.out.println();
ans="";
for(int i=0;i<st.length();i++)
{
char c=st.charAt(i);
if(s.indexOf(c)==-1)
ans +=c;
}
System.out.print("Non-Common Characters in 1st Strings are :");
System.out.print(ans);
}
}