0% found this document useful (0 votes)
464 views

ICSE 10th Computer Programs For Practical File

The document contains examples of Java programs to check different types of numbers: pronic numbers, harshad numbers, greatest common divisor, fibonacci numbers, automorphic numbers, and more. It also includes programs to find digit frequency in a number, check for duck numbers, and find prime factors of a number.

Uploaded by

Harry Kalsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
464 views

ICSE 10th Computer Programs For Practical File

The document contains examples of Java programs to check different types of numbers: pronic numbers, harshad numbers, greatest common divisor, fibonacci numbers, automorphic numbers, and more. It also includes programs to find digit frequency in a number, check for duck numbers, and find prime factors of a number.

Uploaded by

Harry Kalsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Write a Program in Java to input a number and check whether it is a Pronic Number or
Heteromecic Number or not.

Pronic Number : A pronic number, oblong number, rectangular number or heteromecic


number, is a number which is the product of two consecutive integers, that is, n (n + 1).

import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

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


int n = sc.nextInt();
int k = (int)(Math.sqrt(n));
if(k*(k+1)==n)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}

Output:
Enter a number : 110
110 is a Pronic Number.

Enter a number : 73
73 is not a Pronic Number.

Enter a number : 342


342 is a Pronic Number.

Enter a number : 15
15 is not a Pronic Number

2.
Write a Program in Java to input a number and check whether it is a Harshad Number or
Niven Number or not..

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is


an integer (in base 10) that is divisible by the sum of its digits.
Let’s understand the concept of Harshad Number through the following example:

The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is
9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)

import java.util.*;
class HarshadNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

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


int n = sc.nextInt();
int c = n, d, sum = 0;

//finding sum of digits


while(c>0)
{
d = c%10;
sum = sum + d;
c = c/10;
}

if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
else
System.out.println(n+" is not a Harshad Number.");
}
}
Output:
Enter a number : 195
195 is a Harshad Number.

Enter a number : 194


194 is not a Harshad Number.

Enter a number : 190


190 is a Harshad Number.

Enter a number : 111


111 is a Harshad Number.

3.
Write a Program in Java to input 2 numbers and find their Greatest Common Divisor
(GCD).

Note: If the 2 numbers are 54 and 24, then the divisors (factors) of 54 are: 1, 2, 3, 6, 9, 18,
27, 54.

Similarly the divisors (factors) of 24 are: 1, 2, 3, 4, 6, 8, 12, 24.

The numbers that these two lists share in common are the common divisors (factors) of
54 and 24: 1, 2, 3, 6.

The greatest (highest) of these is 6. That is the greatest common divisor or the highest
common factor of 54 and 24.

import java.util.*;
class Gcd
{
public static void main(String args[])throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the First no : ");
int n1=sc.nextInt();
System.out.print("Enter the Second no : ");
int n2=sc.nextInt();
int r;

while(n2 != 0)
{
r = n1 % n2;
n1 = n2;
n2 = r;
}
System.out.print("GCD = "+n1);
}
}

Output:
Enter the First no : 54
Enter the Second no : 24
GCD = 6

4.
Write a program in Java to accept a number and check whether it belongs to the
Fibonacci Series (sequence) or not.
Fibonacci Series:
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

The first two numbers in the series is ‘0’ and ‘1’ and every next number is found by
adding up the two numbers before it.

import java.util.*;
class Is_Fibonacci
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : "); // Inputting a number
int n = sc.nextInt();

if(n<0)
System.out.println("Kindly enter a positive number.");
else
{
int a=0, b=1 ,c=0;
/* 'a' is the 1st term, 'b' is the 2nd term and 'c' is the 3rd term
* 'c' stores the last generated term of the Fibonacci series */

while(c<n) // Loop goes on till the 3rd term is less than the given number
{
c = a + b; // Generating the terms of Fibonacci Series
a = b;
b = c;
}

/* When the control comes out of the while loop, either the
* 3rd term is equal to the number or greater than it */

if(c==n) // If the last term = number, then it belongs to Fibonacci Series


System.out.println("Output : The number belongs to Fibonacci Series.");
else
System.out.println("Output : The number does not belong to Fibonacci Series.");
}
}
}
Output:
1. Enter a number : 377
Output : The number belongs to Fibonacci Series.
2. Enter a 2 digit number : 100
Output : The number does not belong to Fibonacci Series.

5.
Write a Program in Java to input a number and check whether it is an Automorphic
Number or not.

Note: An automorphic number is a number which is present in the last digit(s) of its
square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last
digits.

import java.util.*;
class Automorphic
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number : "); // Inputting the number
int n = sc.nextInt();
int sq = n*n; // Finding the square
int c = 0, copy = n;

// While loop for counting the number of digits in the number


while(copy > 0)
{
c++;
copy = copy/10;
}

/* Finding the end digits of the square.


* If the number has 2 digits, then we need to find last 2 digits of square
* i.e. do a 'sq % 100' operation
*/
int end = sq % (int)Math.pow(10,c);

if(n == end) // If the square ends with the number then it is Automorphic
System.out.print(n+" is an Automorphic Number.");
else
System.out.print(n+" is not an Automorphic Number.");
}
}
6.

Write a program to input a number. Count and print the frequency of each digit present in
that number.

import java.util.*;
class Digit_Freq
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number : ");
int n = sc.nextInt();
int freq[] = new int[10]; //array for storing frequency of all digits
for(int i=0; i<10; i++)
{
freq[i]=0; //intializing the count of every digit with '0'
}
/*Note: Frequency of digit '0' is stored in freq[0], frequency of '1' in freq[1] and so on*/
System.out.println("Output:");
System.out.println("===================="); //this is just for styling the look of the
output
System.out.println("Digit\tFrequency");
System.out.println("====================");
int d;
while(n>0)
{
d=n%10; //extracting digit from the end
freq[d]++; //increasing the frequency of that digit.
n=n/10;
}

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


{
if(freq[i]!=0) //printing only those digits whose count is not '0'
System.out.println(" "+i+"\t "+freq[i]);
}
}
}
Output:
Enter any number : 937825770

Output:
=====================
Digit Frequency
=====================
0 1
2 1
3 1
5 1
7 3
8 1
9 1

7.
Write a Program in Java to input a number and check whether it is a Duck Number or not.

Note: A Duck number is a number which has zeroes present in it, but there should be no
zero present in the beginning of the number. For example 3210, 7056, 8430709 are all
duck numbers whereas 08237, 04309 are not.

import java.io.*;
class Duck_No
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
String n=br.readLine(); //inputting the number and storing it in a String

int l=n.length(); //finding the length (number of digit) of the number


int c=0; //variable for counting number of zero digits
char ch;

for(int i=1;i<l;i++)
{
ch=n.charAt(i); //extracting each digit and checking whether it is a '0' or not
if(ch=='0')
c++;
}

char f=n.charAt(0); //taking out the first digit of the inputted number

if(c>0 && f!='0')


System.out.println("It is a duck number");
else
System.out.println("It is not a duck number");
}
}
Output:
Enter any number : 4013
It is a duck number

Enter any number : 0134


It is not a duck number

Enter any number : 67300987


It is a duck number

8.
Write a program in JAVA to find the Prime factors of a number.

Prime factors of a number are those factors which are prime in nature and by which the
number itself is completely divisible (1 will not be taken as prime number).

Few such numbers are:


Prime Factors of 24 are 2, 2, 2, 3
Prime Factors of 6 are 2, 3

import java.io.*;
class PrimeFactors
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Enter a Number : ");
n=Integer.parseInt(br.readLine());

System.out.print("The Prime Factors of "+n+" are : ");

int i=2;
while(n>1)
{
if(n%i == 0)
{
System.out.print(i+" ");
n=n/i;
}
else
i++;
}
}
}
Output:
Enter a Number : 378
The Prime Factors of 378 are: 2 3 3 3 7

You might also like