1. Write a Code to generate Random numbers
public class GenerateRandomNumbers {
public static void main(String[] args) {
System.out.println(getRandomNumber());
}
public static double getRandomNumber() {
double x = Math.random();
return x;
}
}
2. Write code to verify a number is a perfect number or not.
This is a Java Program to Check if a given Number is Perfect Number. A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself.
public class NumberPerfect {
public static void main(String[] args) {
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer you want to check:");
n = s.nextInt();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == n) {
System.out.println("Given number is Perfect");
} else {
System.out.println("Given number is not Perfect");
}
}
int divisor(int x) {
return x;
}
}
3. Fibonacci series from 1 to 10.
The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
public class Fibonacci {
public static void main(String[] args) {
int count = 7, num1 = 0, num2 = 1;
System.out.print("Fibonacci Series of " + count + " numbers:");
for (int i = 1; i <= count; ++i) {
System.out.print(num1 + " ");
/*
* On each iteration, we are assigning second number to the first
* number and assigning the sum of last two numbers to the second
* number
*/
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
}
}
}
4. Write a program to find a Factorial of a number.
public static void main(String[] args) {
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else {
for (c = 1; c <= n; c++)
fact = fact * c;
System.out.println("Factorial of " + n + " is = " + fact);
}
}
5. Swap two numbers without using the third variable.
public class SwapWithoutThirdVariable {
public static void main(String[] args) {
System.out.println("Before swapping");
int x = 10;
int y = 20;
System.out.println("value of x:" + x);
System.out.println("value of y:" + y);
System.out.println("After swapping");
x = x + y;
y = x - y;
x = x - y;
System.out.println("value of x:" + x);
System.out.println("value of y:" + y);
}
}
6. Program to find greatest of three numbers.
public class GreatestNumber {
public static void main(String[] args) {
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
x = s.nextInt();
System.out.print("Enter the second number:");
y = s.nextInt();
System.out.print("Enter the third number:");
z = s.nextInt();
if (x > y && x > z) {
System.out.println("Largest number is:" + x);
} else if (y > z) {
System.out.println("Largest number is:" + y);
} else {
System.out.println("Largest number is:" + z);
}
}
}
7. An Array of numbers given. .Find the largest two number and print it
public class LargestNumbers {
public static void main(String[] args) {
int n, l1, l2, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
if (n > 1) {
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Largest two numbers are:" + a[0] + " and " + a[1]);
} else {
System.out.println("Enter number greater than 1");
}
}
}
8. Reverse a number.
public class ReverseNumber {
public static void main(String[] args) {
int n, m, a = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while (n > 0) {
x = n % 10;
a = a * 10 + x;
n = n / 10;
}
System.out.println("Reverse number: " + a);
}
}
9. Verify if a given number is a palindrome or not.(same concept of reversing a number)
public class Palindrome {
public static void main(String[] args) {
int n, m, a = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while (n > 0) {
x = n % 10;
a = a * 10 + x;
n = n / 10;
}
if (a == m) {
System.out.println("Given number " + m + " is Palindrome");
} else {
System.out.println("Given number " + m + " is Not Palindrome");
}
}
}
10. Armstrong number program.
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 370, number, temp, total = 0;
number = num;
while (number != 0) {
temp = number % 10;
total = total + temp * temp * temp;
number /= 10;
}
if (total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
public class GenerateRandomNumbers {
public static void main(String[] args) {
System.out.println(getRandomNumber());
}
public static double getRandomNumber() {
double x = Math.random();
return x;
}
}
2. Write code to verify a number is a perfect number or not.
This is a Java Program to Check if a given Number is Perfect Number. A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself.
public class NumberPerfect {
public static void main(String[] args) {
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer you want to check:");
n = s.nextInt();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == n) {
System.out.println("Given number is Perfect");
} else {
System.out.println("Given number is not Perfect");
}
}
int divisor(int x) {
return x;
}
}
3. Fibonacci series from 1 to 10.
The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
public class Fibonacci {
public static void main(String[] args) {
int count = 7, num1 = 0, num2 = 1;
System.out.print("Fibonacci Series of " + count + " numbers:");
for (int i = 1; i <= count; ++i) {
System.out.print(num1 + " ");
/*
* On each iteration, we are assigning second number to the first
* number and assigning the sum of last two numbers to the second
* number
*/
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
}
}
}
4. Write a program to find a Factorial of a number.
public static void main(String[] args) {
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else {
for (c = 1; c <= n; c++)
fact = fact * c;
System.out.println("Factorial of " + n + " is = " + fact);
}
}
5. Swap two numbers without using the third variable.
public class SwapWithoutThirdVariable {
public static void main(String[] args) {
System.out.println("Before swapping");
int x = 10;
int y = 20;
System.out.println("value of x:" + x);
System.out.println("value of y:" + y);
System.out.println("After swapping");
x = x + y;
y = x - y;
x = x - y;
System.out.println("value of x:" + x);
System.out.println("value of y:" + y);
}
}
6. Program to find greatest of three numbers.
public class GreatestNumber {
public static void main(String[] args) {
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
x = s.nextInt();
System.out.print("Enter the second number:");
y = s.nextInt();
System.out.print("Enter the third number:");
z = s.nextInt();
if (x > y && x > z) {
System.out.println("Largest number is:" + x);
} else if (y > z) {
System.out.println("Largest number is:" + y);
} else {
System.out.println("Largest number is:" + z);
}
}
}
7. An Array of numbers given. .Find the largest two number and print it
public class LargestNumbers {
public static void main(String[] args) {
int n, l1, l2, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
if (n > 1) {
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Largest two numbers are:" + a[0] + " and " + a[1]);
} else {
System.out.println("Enter number greater than 1");
}
}
}
8. Reverse a number.
public class ReverseNumber {
public static void main(String[] args) {
int n, m, a = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while (n > 0) {
x = n % 10;
a = a * 10 + x;
n = n / 10;
}
System.out.println("Reverse number: " + a);
}
}
9. Verify if a given number is a palindrome or not.(same concept of reversing a number)
public class Palindrome {
public static void main(String[] args) {
int n, m, a = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while (n > 0) {
x = n % 10;
a = a * 10 + x;
n = n / 10;
}
if (a == m) {
System.out.println("Given number " + m + " is Palindrome");
} else {
System.out.println("Given number " + m + " is Not Palindrome");
}
}
}
10. Armstrong number program.
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 370, number, temp, total = 0;
number = num;
while (number != 0) {
temp = number % 10;
total = total + temp * temp * temp;
number /= 10;
}
if (total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}