ICSE 10th Computer Programs For Practical File
ICSE 10th Computer Programs For Practical File
Write a Program in Java to input a number and check whether it is a Pronic Number or
Heteromecic Number or not.
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Output:
Enter a number : 110
110 is a Pronic Number.
Enter a number : 73
73 is not 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..
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);
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.
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.
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 */
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;
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;
}
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
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
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).
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());
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