Java Programs
Java Programs
Harshard Number:
Accept a number from user and print if given number is Harshad
number or not.
A number is said to be the Harshad number if it is divisible by the
sum of its digit.
For example, 156= 1 + 5 + 6 = 12.
Since 156 is divisible by 12. So, 156 is a Harshad number.
Examples: 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112,
114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153,
156, 162, 171, 180, 190, 192, 195, 198, 200
Ans.
import java.util.Scanner;
public class HarshadNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number:");
int n= sc.nextInt();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum + d;
n = n / 10;
}
if ( m%sum == 0)
System.out.println ("yes");
else
System.out.println ("no");
}
}
Accept a number from user and print if given number is spy number
or not.
spy number: A spy number is a number which has sum its digits
equals the product of the digits.
Example: 123. 1+2+3=1*2*3
22, 123, 321, 1241, 1124, 1142
Code:
import java.util.Scanner;
public class SpyNumber
{
public static void main(){
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any number:");
n= sc.nextInt();
int m = n;
int sum = 0, prod = 1;
while (n != 0) {
int d = n%10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
if (sum == prod)
System.out.println ("yes");
else
System.out.println ("no");
}
}
Accept a number from user and print if given number is Neon number
or not.
Neon number: Sum of digits of square of the number is equal to the
number .
For example: 9: 9*9 = 81, 9= 8+1
Code:
import java.util.Scanner;
public class NeonNumber
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any number:");
n= sc.nextInt();
int m = n;
int sum = 0;
n = n * n;
while (n != 0) {
int d = n%10;
sum = sum + d;
n = n / 10;
}
if (sum == m)
System.out.println ("yes");
else
System.out.println ("no");
}
}
Accept a number from user and print if given number is duck number
or not.
Duck number: A Duck number is a number which has zeroes
present in it. e.g 402, 280.
Code:
import java.util.Scanner;
public class DuckNumber
{
public static void main(){
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any number:");
n= sc.nextInt();
int m = n;
int count = 0;
while (n != 0) {
int d = n%10;
if ( d == 0)
count++;
n = n / 10;
}
if ( count > 0)
System.out.println ("yes");
else
System.out.println ("no");
}
}
Accept a number from user and print if given number is prime
number or not?
Code:
import java.util.Scanner;
public class primeNumber
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count=0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
count++;
}
if (count==2)
System.out.println("Yes");
else
System.out.println("No");
}
}
.
Code:
import java.util.Scanner;
public class PronicNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int fact = 0;
for(int i = 1; i < n; i++)
{
if (i * (i + 1) == n)
{
fact = i;
break;
}
}
if (fact != 0)
System.out.println("Yes");
else
System.out.println("No");
}
Accept a number from user and print if given number is Perfect
number or not.
Perfect numbers : the sum of its positive divisors excluding the
number itself is equal to that number.
For example, 28 is a perfect number because, 28 is divisible by 1, 2,
4, 7, 14 and 28 ,and the sum of these values is 1 + 2 + 4 + 7 + 14 =
28.
Code:
import java.util.Scanner;
public class PerfectNumber
{
public static void main(String[] args) {
int n, Sum = 0 ;
Scanner sc = new Scanner(System.in);
System.out.println("\n Please Enter any Number: ");
n = sc.nextInt();
for(int i = 1; i < n; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
System.out.println("Yes");
else
System.out.println("No");
}
}