You are on page 1of 11

PROGRAM 1

To check whether the entered number is kapreker or not


import java.util.*;
class
Program 1
{
public static void main(String args[])throws InputMismatchException
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the range : ");
int p=scan.nextInt();
int q=scan.nextInt();
int d,i,n,a,b,s,freq;
freq=0; // to find the frequency of kaprekar numbers
System.out.println("The Kaprekar numbers are : ");
for(i=p;i<=q;i++)
{
n=i;
d=0; //to store the number of digits
//count the number of digits in the number
while(n>0){
d++;
n/=10;
}
s=i*i; // find the square of the number
//extract 'd' digits from the right of the square of the number
a=s%(int)Math.pow(10,d);
//extract 'd' or 'd-1' digits from the left of the square of the number
b=s/(int)Math.pow(10,d);
//Check if the two parts add up to the original number
i.e. Condition for Kaprekar number
if(a+b==i){
System.out.print(i+" ");
freq++;
}
System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "+freq);
}
//end of main
}
//end of class
PROGRAM 2
To convert the entered number to words

import java.io.*;
class Num2Words
{public static void main(String args[])throws IOException //main function
{BufferedReader br=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;
Stringx[]={“”,"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Se
venteen","Eighteen","Nineteen"};
Stringx1[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
Stringx2[]={"","Twenty","Thirty","Fourty","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]);
}}

Output
PROGRAM 3
To check whether the entered number is smith or not

import java.util.*;
class
Program 3
{
public static void main(String sr
[])throws InputMismatchException{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
int p,q,i,sod=0,sopf=0,t;
p=q=n; //Find the sum of all the digits of the number
while(p>0){
sod+=p%10;
p/=10;
}
for(i=2;i<=q;i++){
if(q%i==0){ //check if ‘i’ is a factor
t=i;
while(t>0){ //find the sum of the digits of the factor
sopf+=t%10;
t/=10;
}
q=q/i;
i--; //decrement the factor so that next time the same factor is checked again and
again until it is not a factor. This is
the prime factorization method.
}
}
if(sod==sopf) // if sum of digits and sum of prime factors are equal, it is smith number
System.out.println("Smith number");
else
System.out.println("Not Smith number");
}
//end of main
}
//end of class
PROGRAM 4
To check whether the entered number is evil or not
//import required classes and packages  
import Java.util.*;   
import java.io.*;   
import java.util.Scanner;  
  
//create EvilNumberExample class to check whether the given number is an Evil number or 
not  
public class EvilNumberExample {  
  
    // create checkNumber() method that returns true when it founds number Evil   
    public static boolean checkNumber(int n) {  
  
        // find the equivalence binary number using user defined convertToBinary() method  
        long binaryNumber = convertToBinary(n);  
  
        // find total number of 1's in binary number  
        int count = 0;  
  
        // iterate each digit of binary number  
        while(binaryNumber != 0) {  
  
            // if the last digit of binary number is 1, increase the count value  
            if(binaryNumber % 10 == 1)   
                count++;  
    private static long convertToBinary(int number) {  
        long binaryNumber = 0;  
        int rem = 0;  
        int j = 1;  
       while(number != 0) {  
            rem = number % 2;  
            binaryNumber += rem * j;  
            number = number / 2;  
            j = j * 10;  
        }  
          
        return binaryNumber;    //return the binary equivalent number of the decimal number  
    }  
      
    //main() method start  
    public static void main(String[] args) {  
      
        // declare variable in which the user entered value will be store  
        int num = 0;  
  
        // create scanner class object   
        Scanner sc =  new Scanner(System.in);  
          
        //display custom message  
        System.out.print("Enter a number : ");  
          
        //get input from user  
        num = sc.nextInt();  
  
        // check whether the number is evil number or not  
        if(checkNumber(num))  
            System.out.println(num + " is an evil number");  
        else  
            System.out.println(num + " is not an evil number");  
  
    }  
}  
PROGRAM 5
To check whether the entered number is composite magic number

import java.util.*;
class CompositeMagic
{
     Scanner ob = new Scanner(System.in);
     int n, m, s, i;
     boolean x,y;
     public CompositeMagic( )                                                                        // Constructor
     {
            s=0;
        }
     public void check( )                                                    // Function to  check for Composite Magic Number
      {
            System.out.println("Enter integer value for m ");
            m=ob.nextInt();                                                                                     // Input value for m      
            System.out.println("Enter integer value for n ");
            n=ob.nextInt();                                                                                        // Input value for n
            if(m<n)                                                                                                        // check if m is smaller than n
                {
                        System.out.println("COMPOSITE MAGIC INTEGERS ARE:");
                        for(i=m;i<=n;i++)                                              // generate loop from m to n
                            {
                                    x=isComposite(i);                               // calling function to check composite number
                                    y=isMagic(i);                                         // calling function to check magic number
                                    if(x==true && y==true)
                                        {
                                      System.out.print(i + "\t");                            // printing composite magic number
                                      s++;                                               // counting frequency of composite magic number
                                            }
                             }
                     System.out.println("\n FREQUENCY OF COMPOSITE MAGIC INTEGERS
IS:" + s);
                }
            else System.out.println("INVALID INPUT");
       }

      boolean isComposite(int p)
        {
            int j, c=0;
            for(j=2;j<p;j++)
                    if(p%j==0) return true;
            return false;
          }

        boolean isMagic(int p)
         {
             int a, z;
              while(p>9)
                {
                        z=0;
                        while(p>0)
                        {
                                 a=p%10;
                                 z=z+a;
                                 p=p/10;
                           }
                        p=z;
                }
              if(p==1) return true;
                else return false;
         }
    public static void main(String arg[])
     {
        CompositeMagic obj = new CompositeMagic();
        obj.check();
        }
}
PROGRAM 9
To print frequency of each digit

public class Frequency {  
    public static void main(String[] args) {  
        //Initialize array  
        int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};  
        //Array fr will store frequencies of element  
        int [] fr = new int [arr.length];  
        int visited = -1;  
        for(int i = 0; i < arr.length; i++){  
            int count = 1;  
            for(int j = i+1; j < arr.length; j++){  
                if(arr[i] == arr[j]){  
                    count++;  
                    //To avoid counting same element again  
                    fr[j] = visited;  
                }  
            }  
            if(fr[i] != visited)  
                fr[i] = count;  
        }  
  
        //Displays the frequency of each element present in array  
        System.out.println("---------------------------------------");  
        System.out.println(" Element | Frequency");  
        System.out.println("---------------------------------------");  
        for(int i = 0; i < fr.length; i++){  
            if(fr[i] != visited)  
                System.out.println("    " + arr[i] + "    |    " + fr[i]);  
        }  
        System.out.println("----------------------------------------");  
    }}  
Output:-
PROGRAM 12
Fibonacci series

class FibonacciExample1{  
public static void main(String args[])  
{    
 int n1=0,n2=1,n3,i,count=10;    
 System.out.print(n1+" "+n2);//printing 0 and 1    
    
 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already 
printed    
 {    
  n3=n1+n2;    
  System.out.print(" "+n3);    
  n1=n2;    
  n2=n3;    
 }    
  
}}  

You might also like