Numbers
package numbers;
/* wap to swap two numbers using temporary variable */
public class p1 {
public static void main(String[] args) {
int num1 = 500;
int num2 = 1000;
[Link](num1);
[Link](num2);
[Link]("--------------------------------");
int temp;
temp = num1;
num1 = num2;
num2 = temp;
[Link](num1);
[Link](num2);
package numbers;
/* wap to swap two numbers without using temporary variable */
public class p2 {
public static void main(String[] args) {
int num1 = 500;
int num2 = 1000;
[Link](num1);
[Link](num2);
[Link]("-----------------");
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
[Link](num1);
[Link](num2);
package numbers;
/* wap to print even numbers from 1 to 100 */
public class p3 {
public static void main(String[] args) {
for (int num = 0; num <= 100; num++) {
if (num % 2 == 0) {
[Link](num);
package numbers;
/* wap to print odd numbers from 1 to 100 */
public class p4 {
public static void main(String[] args) {
for (int num = 0; num <= 100; num++) {
if (num % 2 != 0) {
[Link](num);
}
}
package numbers;
/* wap to print numbers from 1 to 100 without using any loop */
public class p5 {
static int num = 1;
public static void main(String[] args) {
[Link](num++);
if (num <= 100) {
main(null);
package numbers;
import [Link];
/* wap to count the number of digits in a number */
public class p6 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link]();
int digits = 0;
while (num > 0) {
num /= 10;
digits++;
[Link]("The number of digits = " + digits);
}
package numbers;
import [Link];
/* wap to find the sum of all the digits in a number */
public class p7 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int sum = 0;
int lastDigit;
while (num > 0) {
lastDigit = num % 10;
num /= 10;
sum += lastDigit;
[Link]("The sum is = " + sum);
package numbers;
/* wap to find the product of all the digits in a number */
import [Link];
public class p8 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int product = 1;
int lastDigit;
while (num > 0) {
lastDigit = num % 10;
num /= 10;
product *= lastDigit;
[Link]("The product is = " + product);
package numbers;
import [Link];
/* wap to reverse a number */
public class p9 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int reverse = 0;
int remainder;
while (temp > 0) {
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
}
[Link](reverse);
package numbers;
import [Link];
/* wap to check whether the number is plaindrom */
public class p10 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int reverse = 0;
int remainder;
while (temp > 0) {
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
if (num == reverse) {
[Link]("The nunumber is palindrom");
} else {
[Link]("The number is not a palindrom");
package numbers;
import [Link];
/* wap to check whether a number is positive or negetive */
public class p11 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
if (num > 0) {
[Link](num + " is a positive number");
} else {
[Link](num + " is a negative number");
package numbers;
import [Link];
/* wap to check whether a number is prime number */
public class p12 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
boolean flag = true;
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
}
if (flag) {
[Link](num + " is a prime number");
} else {
[Link](num + " is not a prime number");
package numbers;
/* wap to check whether a number is prime number using method*/
import [Link];
public class p13 {
public static boolean isPrime(int num) {
boolean flag = true;
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
return flag;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
if (isPrime(num)) {
[Link](num + " is a prime number");
} else {
[Link](num + " is not a prime number");
package numbers;
/* wap to print the prime numbers from 1 to 100 */
public class p14 {
public static boolean isPrime(int num) {
boolean flag = true;
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
return flag;
public static void main(String[] args) {
for (int num = 1; num <= 1000; num++) {
if (isPrime(num)) {
[Link](num);
}
}
package numbers;
import [Link];
/* wap to calculate the factorial of a number */
public class p15 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int factorial = 1;
for (int i = num; i >= 1; i--) {
factorial *= i;
[Link]("The factorial of thr given number is = " + factorial);
package numbers;
import [Link];
/* wap to calculate the factorial of a number using method recurssion */
public class p16 {
public static int factorial(int num) {
if (num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
[Link](factorial(num));
package numbers;
/* wap to print the first 10 numbers in fibonacci series */
public class p17 {
public static void main(String[] args) {
int first_no = 0;
int second_no = 1;
int next_no;
int count = 10;
[Link](first_no + "," + second_no);
while (count - 2 > 0) {
next_no = first_no + second_no;
[Link]("," + next_no);
first_no = second_no;
second_no = next_no;
count--;
package numbers;
import [Link];
/* wap to print the first 10 numbers in fibonacci series using method recurssion*/
public class p18 {
static int first_no = 0;
static int second_no = 1;
static int next_no = 1;
public static void printFibonacci(int count) {
next_no = first_no + second_no;
[Link]("," + next_no);
first_no = second_no;
second_no = next_no;
if (count > 1) {
printFibonacci(count - 1);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int count = [Link]();
[Link](first_no + "," + second_no);
printFibonacci(count - 1);
package numbers;
import [Link];
/* wap to check whether a number is automarphic number */
public class p19 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int sqr = num * num;
boolean flag = true;
while (temp > 0) {
if (sqr % 10 != temp % 10) {
flag = false;
break;
temp /= 10;
sqr /= 10;
if (flag) {
[Link](num + " is a automorphic number");
} else {
[Link](num + " is a not a automorphic number");
package numbers;
import [Link];
/* wap to check whether a number is amstrong number*/
public class p20 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int noofdigits = 0;
int sum = 0;
/* code for counting the no of digits */
while (temp > 0) {
noofdigits++;
temp /= 10;
/* code for calculating the sum of digits raised to power */
temp = num;
while (temp > 0) {
int lastDigit = temp % 10;
int power = 1;
for (int i = 0; i < noofdigits; i++) {
power *= lastDigit;
sum += power;
temp /= 10;
if (num == sum) {
[Link](num + " is a amstrong number");
} else {
[Link](num + " is a not a amstrong number");
}
}
package numbers;
import [Link];
/* wap to check whether a number is a Neon number*/
public class p21 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int sqr = num * num;
int sum = 0;
while (sqr > 0) {
sum += sqr % 10; /* sum = sum + sqr % 10 */
sqr /= 10;
if (num == sum) {
[Link](num + " is a neon number");
} else {
[Link](num + " is not a neon number");
package numbers;
import [Link];
/* wap to check whether a number is peterson number or strong number or krishnamurthy number */
public class p22 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int sum = 0;
int lastDigit;
while (temp > 0) {
lastDigit = temp % 10;
temp /= 10;
int factorial = 1;
for (int i = lastDigit; i >= 1; i--) {
factorial *= i;
sum += factorial;
if (num == sum) {
[Link](num + " is a peterson number");
} else {
[Link](num + " is not peterson number");
package numbers;
import [Link];
/* wap to check whether a number is perfect number */
public class p23 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
if (num == sum) {
[Link](num + " is a perfect number");
} else {
[Link](num + " is not a perfect number");
package numbers;
import [Link];
/* wap to check whether a number is spy number */
public class p24 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
int temp = num;
int sum = 0, product = 1;
while (temp > 0) {
sum += temp % 10;
product *= temp % 10;
temp /= 10;
if (sum == product) {
[Link](num + " is a spy number");
} else {
[Link](num + " is not a spy number");
package numbers;
/* wap to find the square root of a number */
import [Link];
public class p25 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
double num = [Link]();
double sqrt = num / 2;
double temp = 0;
while (sqrt - temp != 0) {
temp = sqrt;
sqrt = (temp + num / temp) / 2;
[Link]("The square root of " + num + " is " + sqrt);
}
package numbers;
/* wap to find the cube root of a number */
import [Link];
public class p26 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
double num = [Link]();
double cbrt = num / 3;
double temp = 0;
while (cbrt - temp != 0) {
temp = cbrt;
cbrt = ((2 * temp) + (num / (temp * temp))) / 3;
[Link]("The cube root of " + num + " is " + cbrt);
package numbers;
import [Link];
/* wap to check whether a number is emirp number */
public class p27 {
/* Method to reverse a number */
public static int reverseNumber(int num) {
int temp = num;
int reverse = 0;
int remainder;
while (temp > 0) {
remainder = temp % 10;
reverse = reverse * 10 + remainder;
temp /= 10;
return reverse;
/* Method to check whether a number is prime */
public static boolean isPrime(int num) {
boolean flag = true;
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
return flag;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
if (isPrime(num)) {
if (isPrime(reverseNumber(num))) {
[Link](num + " is a emirp number");
} else {
[Link](num + " is not a emirp number");
} else {
[Link](num + " is not a emirp number");
package numbers;
import [Link];
/* wap to check whether a number is tech number */
public class p28 {
/* Method to count the number of digits */
public static int numberOfDigits(int num) {
int digits = 0;
while (num > 0) {
digits++;
num /= 10;
return digits;
/* Method to check whether a number is Tech number */
public static boolean isTechNumber(int num) {
boolean flag = false;
int digits = numberOfDigits(num);
int divisior = 1;
for (int i = 0; i < digits / 2; i++) {
divisior *= 10;
}
int SecondHalf = num % divisior;
int firstHalf = num / divisior;
int power = (firstHalf + SecondHalf) * (firstHalf + SecondHalf);
if (power == num) {
flag = true;
return flag;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
if (numberOfDigits(num) % 2 == 0) {
if (isTechNumber(num)) {
[Link](num + " is a Tech number");
} else {
[Link](num + " is not a Tech number");
} else {
[Link](num + " doesn't contain even number of digits");
package numbers;
import [Link];
/* wap to check whether a number is sunny number*/
public class p29 {
public static double squareRoot(double num) {
double sqrt = num / 2;
double temp = 0;
while (sqrt - temp != 0) {
temp = sqrt;
sqrt = (temp + num / temp) / 2;
return sqrt;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
double num = [Link]();
double x = squareRoot(num + 1);
if (x % 1 == 0) {
[Link](num + " is a Sunny number");
} else {
[Link](num + " is not a Sunny number");
package numbers;
import [Link];
/* wap to check whether a number is fascinating number */
public class p30 {
/* Method to count the number of digits */
public static int noOfDigits(int num) {
int digits = 0;
while (num > 0) {
digits++;
num /= 10;
return digits;
/* Method to check whether a number is fascinating number */
public static boolean isFascinatingNumber(int num) {
String s = "" + num + 2 * num + 3 * num;
boolean flag = true;
for (char ch = '1'; ch < '9'; ch++) {
int count = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == ch) {
count++;
if (count != 1) {
flag = false;
break;
return flag;
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int num = [Link]();
if (noOfDigits(num) >= 3) {
if (isFascinatingNumber(num)) {
[Link](num + " is fascinating number");
} else {
[Link](num + " is not a fascinating number");
} else {
[Link](num + " doesn't contain enough digits");
package numbers;
import [Link];
/* wap to check whether a year is leap year */
public class p31 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number ");
int year = [Link]();
boolean flag = true;
if (year % 4 == 0) {
if (year % 100 == 0 && year % 400 != 0) {
flag = false;
}
} else {
flag = false;
if (flag) {
[Link](year + " is a leap year");
} else {
[Link](year + " is not a leap year");
package numbers;
/* wap to find the sum of natural numbers from 1 to 100 */
public class p32 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
[Link](sum);
package numbers;
/* wap to find the sum of even and odd numbers from 1 to 100 */
public class p33 {
public static void main(String[] args) {
int evenSum = 0;
int oddSum = 0;
for (int i = 1; i < 100; i++) {
if (i % 2 == 0) {
evenSum += i;
} else {
oddSum += i;
[Link](evenSum);
[Link](oddSum);