हिंदी

Apply all the following in one program using different functions and sub-functions. 1. Count the total factors of a number int; fnTotalFactors(int n) 2. Verify whether a number is prime or not;

Advertisements
Advertisements

प्रश्न

Apply all the following in one program using different functions and sub-functions.

  1. Count the total factors of a number int;             fnTotalFactors(int n)
  2. Verify whether a number is prime or not;            boolean fnIsPrime(int n)
  3. Create the reverse of a number and verify whether it is prime.
  4. Count the number of prime factors of a number.
  5. Add the digits of a number till the sum comes in a single digit number.
कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clDigits
{
    int fnTotalFactors(int n)
    {
        int c = 2;//every number has 1 and itself as factors
        for(int f = 2; f <= n/2; f++)//other than n, n/2 is the next
        largest factor
        {
            if(n % f == 0) // is a factor of n
            {
                c++;
            }
        }
        return c;
    }
    boolean fnIsPrime(int n)
    {
        for(int f = 2; f <= n/2; f++)
        {
            if(n % f == 0)//f is a factor of n, it is not prime
            {
                return false;//process stops by returning
                false
            }
        }
        return true;//no factor was found, so it is prime
    }
    void fnRevPrime(int n)
    {
        int cn = n, rev = 0;
        while(cn > 0)
        {
            rev = rev * 10 + cn % 10;
            cn = cn / 10;
        }
        boolean isp = fnIsPrime(rev);
        System.out.println(" Reverse of " + n + " is " + rev);
        System.out.println(" Prime status of " + rev + " is " + isp);
    }
    int fnCntPrimeFactors(int n)
    {
        int c = 0;
        System.out.println("\n Prime factors of " + n + " : ");
        for(int f = 2; f <= n; f++)
        {
            if(n % f == 0)//f is a factor of n, it is not prime
            {
                if(fnIsPrime(f) == true) 
                {
                    System.out.print(" " + f);
                    c++;
                }
            }
        }
        return c;
    }
    int fnDigitSum(int n)
    {
        int cn2 = n;
        System.out.println("\n Continuous Sum of digits of : " + n);
        while (cn2 > 9)
        {
            int cn = cn2, sum = 0, d = 0;
            while(cn > 0)
            {
                d = cn % 10;
                sum = sum + d;
                cn = cn / 10;
            }
            System.out.println(cn2 + " sum of its digits " + sum);
            cn2 = sum;
        }
        return cn2;
    }
    void main()
    {
    public static void main(String args[])
    {
        clDigits obd = new clDigits();
        int R1 = obd.fnTotalFactors(28);
        System.out.println("\nTotal Factors of 28 : " + R1);
        boolean R2 = obd.fnIsPrime(28);
        System.out.println("\nPrime status 28 : " + R2);
        obd.fnRevPrime(92);
        int R4 = obd.fnCntPrimeFactors(210);
        System.out.println("\nTotal Prime Factors of 210 : " + R4);
        int R5 = obd.fnDigitSum(8269);
        System.out.println("\nFinal Sum of digits 8269 : "+ R5);
    }
}

Output:

Total Factors of 28 : 6 
Prime status 28 : false
Reverse of 92 is 29
Prime status of 29 is true
Prime Factors of 210:
2 3 5 7 
Total Prime Factors of 210 : 4 
Continuous Sum of digits of : 8269
8269 sum of its digits 25
25 sum of its digits 7
Final Sum of digits 8269 : 7

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - Methods [पृष्ठ २५३]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
Methods | Q 1. | पृष्ठ २५३
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×