Saturday, 28 December 2024

CLASS X TYPES OF NUMBERS

 CLASS X  TYPES OF NUMBERS

List of types of Numbers

1. Arm Strong Number

2. Co-prime Number

3. Duck Number [I.C.S.E 2024]

4. Magic Number 

5. Niven Number [I.C.S.E 2016]

6. Perfect Number

7. Prime Number

8. Spy Number [I.C.S.E 2017]

9. Special Number [I.C.S.E 2014]

10. 


1. Arms Strong Number

import java.util.*;

public class Armstrong_Number

{

public static void main(String args[])

{

   Scanner sc=new Scanner(System.in);

   int no;

   System.out.println("Enter a Number:");

   no=sc.nextInt();

   int actno=no;

   while(no>0)

   {

       int p=no%10;

       sum=sum+p*p*p;

       no=no/10;

   }

   if(sum==actno)

   {

         System.out.println("It is an Arm Strong Number:");

    }

    else

    {

         System.out.println("It is not an Arm Strong Number:");

    }

}

}


2. Co-prime Number 


Co prime numbers are those numbers that have only one common factor, namely 1. That means a pair of numbers are said to be co prime when they have their highest common factor as 1. 


import java.util.Scanner;

public class CoPrimeNumbers

{

    public static void main(String[] args)

    {

        int a, b, gcd = 1;

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a=");

        a = sc.nextInt();

        System.out.print("Enter b=");

        b = sc.nextInt();

        int min, max;

        min = a;

        if (min > b)

        {

            min = b;

            max = a;

        }

        else

        {

            min = a;

            max = b;

        }

        while (max > min)

        {

            int r = max % min;

            if (r == 0)

            {

                gcd = min;

                break;

            }

            else

            {

                max = min;

                min = r;

            }

        }

        if (gcd == 1)

        {

            System.out.println("Co Prime Numbers");

        }

        else

        {

            System.out.println("Not Co Prime Numbers");

        }

    }

}

3. Duck Number [I.C.S.E 2024]


A Duck Number is a positive non-zero number containing at least one zero in its numeric representation. The zero should be present as a leading zero, i.e. the digit zero can be present at any position except at the start of the number.

import java.io.*;

class GFG {

 

    // Function to check whether

    // the given number is duck number or not.

    static boolean check_duck(String num)

    {

        // Ignore leading 0s

        int i = 0, n = num.length();

        while (i < n && num.charAt(i) == '0')

            i++;

 

        // Check remaining digits

        while (i < n) {

            if (num.charAt(i) == '0')

                return true;

            i++;

        }

 

        return false;

    }

 

    // Driver Method

    public static void main(String args[]) throws IOException

    {

        String num = "1023";

        if (check_duck(num))

            System.out.println("It is a duck number");

        else

            System.out.println("It is not a duck number");

    }

}



4. Magic Number

magic number is a number whose sum of digits is 1 after recursive addition

For example- 
Number= 50113 
=> 5+0+1+1+3=10 
=> 1+0=1 
This is a Magic Number 

For example- 
Number= 1234 
=> 1+2+3+4=10 
=> 1+0=1 
This is a Magic Number


import java.io.*; 

public class GFG 

   public static boolean isMagic(int n) 

   { 

       int sum = 0; 

       

       // Note that the loop continues  

       // if n is 0 and sum is non-zero. 

       // It stops when n becomes 0 and 

       // sum becomes single digit. 

       while (n > 0 || sum > 9) 

       { 

           if (n == 0) 

           { 

               n = sum; 

               sum = 0; 

           } 

           sum += n % 10; 

           n /= 10; 

       } 

       

       // Return true if sum becomes 1. 

       return (sum == 1); 

   } 

    

   // Driver code 

   public static void main(String args[]) 

    { 

     int n = 1234; 

     if (isMagic(n)) 

        System.out.println("Magic Number"); 

           

     else

        System.out.println("Not a magic Number"); 

    } 



5. Niven Number

Harshad (or Nivennumber is and how to check if a number is a Harshad number in different bases. 

An integer number in base 10 which is divisible by the sum of its digits is said to be a Harshad Number. An n-Harshad number is an integer number divisible by the sum of its digit in base n.

Below are the first few Harshad Numbers represented in base 10:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………

Given a number in base 10, our task is to check if it is a Harshad Number or not.


Examples : 

Input: 3

Output: 3 is a Harshad Number


Input: 18

Output: 18 is a Harshad Number


Input: 15

Output: 15 is not a Harshad Number

public class NivenNo { 

    // method to check Niven Number 

    static boolean checkNiven(int n) 

    { 

        // calculate sum of digits 

        int sum = 0; 

        for (int temp = n; temp > 0; temp /= 10) 

            sum += temp % 10; 

  

        // Return true if sum of digits is multiple 

        // of n 

        return (n % sum == 0); 

    } 

  

    // Driver program to test above functions 

    public static void main(String[] args) 

    { 

        System.out.println(checkNinenNo(12) ? "Yes" : "No"); 

        System.out.println(checkNinenNo(15) ? "Yes" : "No"); 

    } 


6. Perfect Number

A perfect number is a positive integer equal to the total of its positive divisors, except the number itself in number theory. For example, 6 is a perfect number since 1 + 2 + 3 equals 6.

Some of the first perfect numbers are 6, 28, 496, and 8128. Perfect numbers are also known as "Complete Numbers" and "Proper Numbers".

import java.util.*;

public class Perfect_Number

{

public static void main(String args[])

{

   Scanner sc=new Scanner(System.in);

   int no, sum=0,prod=1;

   System.out.println("Enter a Number:");

   no=sc.nextInt();

   int actno=no;

   for(i=1;i<=no;i++)

   {

       if(no%i==0)

       sum=sum+i;

       prod=prod*i;

   }

   if(sum==prod)

   {

         System.out.println("It is a Perfect Number:");

    }

    else

    {

         System.out.println("It is not a Perfect Number:");

    }

}

}



7.  Prime Number

import java.util.*;

public class Prime_Number

{

public static void main(String args[])

{

   Scanner sc=new Scanner(System.in);

   int no, sum=0,prod=1;

   System.out.println("Enter a Number:");

   no=sc.nextInt();

   int actno=no;

   for(i=1;i<=no;i++)

   {

       if(no%i==0)

        {

           count++;

        }

    }

   if(count<=2)

   {

         System.out.println("It is a Prime Number:");

    }

    else

    {

         System.out.println("It is not a Prime Number:");

    }

}

}


8. Spy Number

Write a program to accept a number and check and display whether it is a spy number or not. (A number is spy if the sum of its digits equals the product of its digits.)


Example: consider the number 1124.

Sum of the digits = 1 + 1 + 2 + 4 = 8

Product of the digits = 1 x 1 x 2 x 4 = 8

Solution:-

import java.util.Scanner;


public class KboatSpyNumber

{

    public static void main(String args[]) {

        

        Scanner in = new Scanner(System.in);

        

        System.out.print("Enter Number: ");

        int num = in.nextInt();

        

        int digit, sum = 0;

        int orgNum = num;

        int prod = 1;

        

        while (num > 0) {

            digit = num % 10;

            

            sum += digit;

            prod *= digit;

            num /= 10;

        }

        

        if (sum == prod)

            System.out.println(orgNum + " is Spy Number");

        else

            System.out.println(orgNum + " is not Spy Number");

        

    }

}


9. Special Number [ICSE 2014]

A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.


Example: Consider the number 59.

Sum of digits = 5 + 9 = 14

Product of digits = 5 * 9 = 45

Sum of the sum of digits and product of digits = 14 + 45 = 59


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, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".


Solution:-


import java.util.Scanner;

public class SpecialNumber

{

    public void checkNumber() {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter a 2 digit number: ");

        int orgNum = in.nextInt();

        int num = orgNum;

        int count = 0, digitSum = 0, digitProduct = 1;

        while (num != 0) {

            int digit = num % 10;

            num /= 10;

            digitSum += digit;

            digitProduct *= digit;

            count++;

        }

        

        if (count != 2)

            System.out.println("Invalid input, please enter a 2-digit number");

        else if ((digitSum + digitProduct) == orgNum)

            System.out.println("Special 2-digit number");

        else

            System.out.println("Not a special 2-digit number");

    }

}

No comments:

Post a Comment

CLASS X TYPES OF NUMBERS

 CLASS X  TYPES OF NUMBERS List of types of Numbers 1. Arm Strong Number 2.  Co-prime Number 3. Duck Number [I.C.S.E 2024] 4. Magic Number  ...