Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2014~ COMPOSITE MAGIC

Monday, July 28, 2014

ISC COMPUTER SCIENCE PRACTICAL 2014~ COMPOSITE MAGIC

/*
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 which has more than 2 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 2 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.
Test your program with the sample data and some random data:

Example 1:
INPUT:
m=10
n=100
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
10,28,46,55,64,82,91,100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :8

Example 2:INPUT:
m=1200
n=1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE :
1207,1216,1225,1234,1243,1252,1261,1270,1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS :9

Example 3:
INPUT:
m=120
n=99
OUTPUT:
INVALID INPUT
*/

/* ISC COMPUTER SCIENCE PRACTICALS SOLVED, 2014 */
import java.util.*;
public class CompositeMagic {
    public static void main(String[] args) {
        int m,n;
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT THE VALUE OF m & n:");
        m = in.nextInt();
        n = in.nextInt();
        while( m > n || n < 1 || m < 1) {
            System.out.println("INVALID CHOICE:");
            m = in.nextInt();
            n = in.nextInt();
            }
        CheckCompositeMagic cm = new CheckCompositeMagic(m,n);
        System.out.println("THE COMPOSITE MAGIC NUMBERS ARE:");
        cm.checkComposite();
        System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:"+cm.fCount());
        System.out.println();
        }
    }
class CheckCompositeMagic {
    CheckCompositeMagic (int m, int n) {
        M = m; N = n;
        freqCompositeMagic = 0;
        }
    public int fCount() {
        return     freqCompositeMagic;
        }
    public void checkComposite() {
        for ( int i = M; i <= N ;i++) {
            flag = false;
            for ( int j = 2; j <= Math.sqrt(i); j++) {
                if( i%j == 0) {
                    flag = true;
                    break;
                    }
                }
            if(flag == true) {
                 checkMagic(i);
                 if(fg == true)   
                     System.out.println(i);
                 }   
            }
        }    
    private void checkMagic(int mg) {
        fg = false;
        int tmp,rem;
        tmp = mg;
        sum = 0;
        while( tmp > 0) {
            rem = tmp % 10;
            sum += rem;
            tmp = tmp/10;
            }
        if( sum == 1 ) {
            freqCompositeMagic++;
            fg = true;
            }   
        else {
            if(sum < 10)
                fg = false;
            else
                checkMagic(sum);
            }   
           
        }   
    private int M;
    private int N;   
    private int freqCompositeMagic;
    private int sum;
    private boolean flag,fg;
    }   

No comments:

Post a Comment