Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Check whether a given number is palindrome number or not.

Palindrome means, If the reverse of the number is same of original


number
Examples:131,1442,242
import java.util.Scanner;
public class palindrome
{
public static void main()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
int n = scan.nextInt();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum * 10 + d;
n = n / 10;
}
if ( sum == m)
System.out.println ("yes");
else
System.out.println ("no");
}
}

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

Find if number is Deficient Number or not.


Deficient Number: Sum of factors is less than the number itself.
e.g. 21. Factors: 1,3,7 = 11<21.
Examples: 10, 11, 13, 14, 15, 16, 17, 19
Code:
import java.util.Scanner;
public class DeficientNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
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");
}
}

Find if number is a Composite number. its is a number which has


more that one factor(excl. 1,n).
e.g. 8=2,4=2 factors.
Code:
import java.util.Scanner;
public class CompositeNum
{
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 >3)
System.out.println("Yes");
else
System.out.println("No");
}
}

Find if given number is a Abundant number or not.


Abundant number: sum of factor is greater then the number
itself.
e.g. 12. Factors: 1, 2, 3, 4, 6 = 16 >12
12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78,
80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120
Code:
public class AbundantNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
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");
}
}
4. Find if a given number is Pronic number or not.
Pronic Number: it is the product of two consecutive integers,
n(n+1).
e.g 56= 7 x 8 .

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

Floyds binary pattern


import java.util.Scanner;
class Binarypattern4{
public static void main(String args[]){
int i,j,rows;
int count=1;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
rows=scan.nextInt();
for(i=1; i<=rows; i++){
for(j=1; j<=i; j++){
if(j%2==1){
System.out.print("1");
}
else{
System.out.print("0");
}
}
System.out.println();
}
}
}
Enter the number of rows: 8
1
10
101
1010
10101
101010
1010101
10101010

You might also like