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");

    }

}

Wednesday, 25 December 2024

CLASS - X : PROGRAM BASED ON ARRAYS

 CLASS - X : PROGRAM BASED ON ARRAYS

Question 1:- Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.                                                                                                              [ICSE 2024]

110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

Solution:-

public class Pincode

{

    public static void main(String args[]) {

        int[] arr = {110061, 110001, 

                     110029, 110023, 

                     110055, 110006, 

                     110019, 110033};

                          

        for (int i = 0; i < 7; i++) 

        {

            int idx = i;

            for (int j = i + 1; j < 8; j++) 

            {

                if (arr[j] < arr[idx])

                    idx = j;

            }

            

            int t = arr[i];

            arr[i] = arr[idx];

            arr[idx] = t; 

        }

        

        System.out.println("Sorted Array:");

        for (int i = 0; i < 8; i++) 

        {

            System.out.print(arr[i] + " ");

        }

    }

}

}

Question 2:- Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special array if the sum of the even elements = sum of the odd elements.

                                                                                                                   [ICSE Speciman 2024]

Example:

A[ ][ ]={{ 4 ,5, 6}, { 5 ,3, 2}, { 4, 2, 5}};

Sum of even elements = 4 + 6 + 2 + 4 + 2 = 18

Sum of odd elements = 5 + 5 + 3 + 5 = 18

Solution:-

import java.util.Scanner;


public class SpecialArray

{

    public static void main(String args[]) 

    {

        Scanner in = new Scanner(System.in);

        int arr[][] = new int[3][3];

        long evenSum = 0, oddSum = 0;

        System.out.println("Enter the elements of 3 x 3 DDA: ");

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                arr[i][j] = in.nextInt();

            }

        }

        

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                if (arr[i][j] % 2 == 0)

                    evenSum += arr[i][j];

                else

                    oddSum += arr[i][j];

            }

        }

        

        System.out.println("Sum of even elements = " + evenSum);

        System.out.println("Sum of odd elements = " + oddSum);

        if (evenSum == oddSum)

            System.out.println("Special Array");

        else

            System.out.println("Not a Special Array");

    }

}


Question 3:Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its position where it is present in the array. Otherwise display message "not found".

Solution:-

import java.util.Scanner;


public class ExLinearSearch

{

    public static void main(String args[]) 

    {

        Scanner in = new Scanner(System.in);

        

        double arr[] = new double[20];

        int l = arr.length;

        int i = 0;

        

        System.out.println("Enter array elements: ");

        for (i = 0; i < l; i++) 

        {

            arr[i] = in.nextDouble();    

        }

        

        System.out.print("Enter the number to search: ");

        double n = in.nextDouble();

        

        for (i = 0; i < l; i++) 

        {

            if (arr[i] == n) 

            {

                break;

            }

        }

        

        if (i == l) 

        {

            System.out.println("Not found");

        }

        else 

        {

            System.out.println(n + " found at index " + i);

        }

    }

}

Question 4: Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.

EXAMPLE :

Input : Hari, Anita, Akash, Amrita, Alina, Devi Rishab, John, Farha, AMITHA

Output: Anita

Amrita

Alina

AMITHA

Soluton:-

import java.util.Scanner;


public class KboatWords

{

    public static void main(String args[]) 

    {

        Scanner in = new Scanner(System.in);

        String names[] = new String[10];

        int l = names.length;

        System.out.println("Enter 10 names : ");

        

        for (int i = 0; i < l; i++) 

        {

            names[i] = in.nextLine();

        }

        

        System.out.println("Names that begin and end with letter A are:");


        for(int i = 0; i < l; i++)

        {

            String str = names[i];

            int len = str.length();

            char begin = Character.toUpperCase(str.charAt(0));

            char end = Character.toUpperCase(str.charAt(len - 1));

            if (begin == 'A' && end == 'A') {

                System.out.println(str);

            }

        }

    }

} 


Question 5 :-  Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.

Solution:-

import java.util.Scanner;

public class ExBubbleSort

{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        int n = 15;

        int arr[] = new int[n];

        

        System.out.println("Enter the elements of the array:");

        for (int i = 0; i < n; i++) {

            arr[i] = in.nextInt();

        }

        

        //Bubble Sort

        for (int i = 0; i < n - 1; i++) {

            for (int j = i+1; j < n - i - 1; j++) {

                if (arr[j] > arr[j + 1]) {

                    int t = arr[j];

                    arr[j] = arr[j+1];

                    arr[j+1] = t;

                }

            } 

        }

        

        System.out.println("Sorted Array:");

        for (int i = 0; i < n; i++) {

            System.out.print(arr[i] + " ");

        }

    }

} 



Question 6 :-  Write a program to accept name and total marks of N number of students in two single subscript array name[] and totalmarks[].

Calculate and print:

The average of the total marks obtained by N number of students.

[average = (sum of total marks of all the students)/N]

Deviation of each student’s total marks with the average.

[deviation = total marks of a student – average]

Solution:-

import java.util.Scanner;


public class StudentMarksPercentage

{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter number of students: ");

        int n = in.nextInt();

        

        String name[] = new String[n];

        int totalmarks[] = new int[n];

        int grandTotal = 0;

        

        for (int i = 0; i < n; i++) {

            in.nextLine();

            System.out.print("Enter name of student " + (i+1) + ": ");

            name[i] = in.nextLine();

            System.out.print("Enter total marks of student " + (i+1) + ": ");

            totalmarks[i] = in.nextInt();

            grandTotal += totalmarks[i];

        }

        

        double avg = grandTotal / (double)n;

        System.out.println("Average = " + avg);

        

        for (int i = 0; i < n; i++) {

            System.out.println("Deviation for " + name[i] + " = " 

            + (totalmarks[i] - avg));

        }

    }

}


Question 7 :-  Write a program to input integer elements into an array of size 20 and perform the following operations:

Display largest number from the array.

Display smallest number from the array.

Display sum of all the elements of the array

Solution:-

import java.util.Scanner;

public class MinimumMaximumSum

{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        int arr[] = new int[20];

        System.out.println("Enter 20 numbers:");

        for (int i = 0; i < 20; i++) {

            arr[i] = in.nextInt();

        }

        int min = arr[0], max = arr[0], sum = 0;

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] < min)

                min = arr[i];

                

            if (arr[i] > max)

                max = arr[i];

                

            sum += arr[i];

        }

        

        System.out.println("Largest Number = " + max);

        System.out.println("Smallest Number = " + min);

        System.out.println("Sum = " + sum);

    }

}



Question 8 :-Write a program to initialize the seven Wonders of the World along with their locations in two different arrays. Search for a name of the country input by the user. If found, display the name of the country along with its Wonder, otherwise display "Sorry not found!".

Seven Wonders:

CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM

Locations:

MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALYExamples:

Country name: INDIA

Output: TAJ MAHAL

Country name: USA

Output: Sorry Not found!

Solution:-

import java.util.Scanner;

public class SevenWonders

{

    public static void main(String args[]) {

        

        String wonders[] = {"CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJMAHAL", 

            "GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"};

        

        String locations[] = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN",

        "ITALY"};

        

        Scanner in = new Scanner(System.in);

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

        String c = in.nextLine();

        int i;

        

        for (i = 0; i < locations.length; i++) {

            if (locations[i].equals(c)) {

                System.out.println(locations[i] + " - " + wonders[i]);

                break;

            }

        }

        

        if (i == locations.length)

            System.out.println("Sorry Not Found!");

    }

}


Question 9 :-  Write a program to input twenty names in an array. Arrange these names in descending order of letters, using the bubble sort technique.

Solution:-

import java.util.Scanner;


public class KboatArrangeNames

{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        String names[] = new String[20];

        System.out.println("Enter 20 names:");

        for (int i = 0;  i < names.length; i++) {

            names[i] = in.nextLine();

        }


        //Bubble Sort

        for (int i = 0; i < names.length - 1; i++) {

            for (int j = 0; j < names.length - 1 - i; j++) {

                if (names[j].compareToIgnoreCase(names[j + 1]) < 0) {

                    String temp = names[j + 1];

                    names[j + 1] = names[j];

                    names[j] = temp;

                }

            }

        }

        

        System.out.println("\nSorted Names");

        for (int i = 0;  i < names.length; i++) {

            System.out.println(names[i]);

        }

    }

} 


Question 10 :-Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".

Sample Input:

n[0]n[1]n[2]n[3]n[4]n[5]n[6]n[7]n[8]n[9]
1982198719931996199920032006200720092010

  Solution:-

import java.util.Scanner;


public class ExampleGraduationYear

{

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};

        

        System.out.print("Enter graduation year to search: ");

        int year = in.nextInt();

        

        int l = 0, h = n.length - 1, idx = -1;

        while (l <= h) {

            int m = (l + h) / 2;

            if (n[m] == year) {

                idx = m;

                break;

            }

            else if (n[m] < year) {

                l = m + 1;

            }

            else {

                h = m - 1;

            }

        }

        

        if (idx == -1)

            System.out.println("Record does not exist");

        else

            System.out.println("Record exists");

    }

}




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  ...