For loop task

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

For loop task:

1) Sum of square n natural number:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
sum+=i * i;
}

System.out.println("Sum of squares: " + sum);


}
}

2) Print natural number:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i=1;i<=n;i++){
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}

3) Print even number till n:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i=1;i<=n;i++){
if(i%2==0)
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}
4) Print odd number till n:
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i=1;i<=n;i++){
if(i%2!=0)
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}

5) Print natural number in reverse:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i= n;i>=1;i--){
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}
6) Print natural number:
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i= -10;i<=-1;i++){
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}

7) 7) Print even number till n:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i=n;i>=1;i--){
if(i%2==0)
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}

8) Print even number till n:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
//int sum=0;
for(int i=n;i>=1;i--){
if(i%2!=0)
System.out.print(i+” “);
}

//System.out.println("Sum of squares: " + sum);


}
}

9)sum of n natural number:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}

System.out.println("Sum of squares: " + sum);


}
}

10) sum n even number:


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
if(i%2==0)
sum+=i;
}

System.out.println("Sum of squares: " + sum);


}
}

11)sum of n odd number:


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
if(i%2!=0)
sum+=i;
}

System.out.println("Sum of squares: " + sum);


}
}

12) 2^2+4^2+6^2+...........+n^2

import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
if(i%2==0)
sum+=i * i;
}

System.out.println("Sum of squares: " + sum);


}
}

13) 1^2+3^2+5^2+...........+n^2
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
if(i%2!=0)
sum+=i * i;
}

System.out.println("Sum of squares: " + sum);


}
}

14) print sum of cube number till n.


import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
sum+=i * i * i;
}

System.out.println("Sum of squares: " + sum);


}
}

15) 1/1^2+1/2^2+1/3^2+.......+1/n^2.
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / (i * i);
}
System.out.println("The sum of the series is: " + sum);
}
}

16) 1/1^2+1/3^2+1/5^2+.......+1/n^2.
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = 0.0;
for (int i = 1; i <= n; i++) {
if(i%2!=0)
sum += 1.0 / (i * i);
}
System.out.println("The sum of the series is: " + sum);
}
}

17.1/2^2+1/4^2+1/6^2+.......+1/n^2
import java.util.Scanner;
public class angle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
double sum = 0.0;
for (int i = 1; i <= n; i++) {
if(i%2==0)
sum += 1.0 / (i * i);
}
System.out.println("The sum of the series is: " + sum);
}
}

18) Find factorial values for n.


import java.util.Scanner;
class angle{
public static void main(String args[]){
int i,fact=1;
int number=5;
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

19) 1!+2!+3!+......n!
import java.util.Scanner;
class angle{
public static void main(String args[]){
int i,fact=1;
int number=5;
int sum=0;
for(i=1;i<=number;i++){
fact=fact*i;
sum+=fact;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

20) 1!+3!+5!+......n!
import java.util.Scanner;
class angle{
public static void main(String args[]){
int i,fact=1;
int number=5;
int sum=0;
for(i=1;i<=number;i++){

fact=fact*i;
if(i%2!=0)
sum+=fact;
}
System.out.println("Factorial of "+number+" is: "+fact);

}
}

21 )2!+4!+6!+......n!
import java.util.Scanner;
class angle{
public static void main(String args[]){
int i,fact=1;
int number=5;
int sum=0;
for(i=1;i<=number;i++){

fact=fact*i;
if(i%2==0)
sum+=fact;
}
System.out.println("Factorial of "+number+" is: "+fact);

}
}

22) 1/1!+1/2!+1/3!+.........1/n!
import java.util.Scanner;
class Angle {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();
double sum = 0.0;
int fact = 1;

for (int i = 1; i <= number; i++) {


fact *= i;
sum += 1.0 / fact;
}

System.out.println("Sum of the series 1/1! + 1/2! + ... + 1/" + number


+ "! is: " + sum);
}
}

23) 1/1!+1/3!+1/5!+.........1/n!
import java.util.Scanner;
class Angle {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();

double sum = 0.0;


int fact = 1;

for (int i = 1; i <= number; i++) {


fact *= i;
if(i%2!=0)
sum += 1.0 / fact;
}

System.out.println("Sum of the series 1/1! + 1/2! + ... + 1/" + number


+ "! is: " + sum);
}
}

24) 1/2!+1/4!+1/6!+.........1/n!
import java.util.Scanner;
class Angle {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();

double sum = 0.0;


int fact = 1;

for (int i = 1; i <= number; i++) {


fact *= i;
if(i%2==0)
sum += 1.0 / fact;
}

System.out.println("Sum of the series 1/1! + 1/2! + ... + 1/" + number


+ "! is: " + sum);
}
}

25) print current and previous odd number till n


import java.util.Scanner;
class angle{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
int prevOdd = -1;
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
if (prevOdd != -1) {
System.out.println("Current odd: " + i + ", Previous odd:
" + prevOdd);
} else {
System.out.println("Current odd: " + i + ", No previous
odd");
}
prevOdd = i;
}
}
}
}

26) print current and previous even number till n


import java.util.Scanner;
class angle{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
int prevOdd = -1;
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
if (prevOdd != -1) {
System.out.println("Current odd: " + i + ", Previous odd:
" + prevOdd);
} else {
System.out.println("Current odd: " + i + ", No previous
odd");
}
prevOdd = i;
}
}
}
}

27)multiplication
class angle {
public static void main(String[] args) {
int num = 5;
for (int i = 1; i <=10; i++) {
System.out.println(i +" * " + num +" = "+num * i);
}
}
}

28.Reverse'snumber
public class ReverseNumberExample1

public static void main(String[] args)

int number = 987654;

int reverse = 0;

for (int temp = number; temp != 0; temp = temp / 10) {

int remainder = temp % 10;

reverse = reverse * 10 + remainder;

System.out.println("The reverse of the given number is: " + reverse);

29)print each in string


class angle {
public static void main(String[] args) {
String s = “keerthana”;
for (int i = 1; i <s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}

30) reverse the string:


class angle {
public static void main(String[] args) {
String s = “keerthana”
for (int i = s.length; i>=0; i--) {
System.out.println(s.charAt(i));
}
}
}

31)palindrome
public class ReverseNumberExample1

public static void main(String[] args)

int number = 987654;

int reverse = 0;

for (int temp = number; temp != 0; temp = temp / 10) {

int remainder = temp % 10;

reverse = reverse * 10 + remainder;

System.out.println("The reverse of the given number is: " + reverse);

} if (originalNumber == reversedNumber) {

System.out.println(originalNumber + " is a palindrome.");

} else {

System.out.println(originalNumber + " is not a palindrome.");

}
}

32)Armstrong number:
import java.util.Scanner;

public class ArmstrongCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

int originalNumber = number;


int sum = 0;
int digits = 0;

for (int temp = number; temp != 0; temp /= 10) {


digits++;
}

for (int temp = number; temp != 0; temp /= 10) {


int digit = temp % 10;
sum += Math.pow(digit, digits);
}

if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong
number.");
}

scanner.close();
}
}

33)adam number
import java.util.Scanner;

public class AdamNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

int reversed = 0;
int originalNumber = number;

for (int temp = number; temp != 0; temp /= 10) {


int digit = temp % 10;
reversed = reversed * 10 + digit;
}

int squareOriginal = originalNumber * originalNumber;


int squareReversed = reversed * reversed;
int reversedSquare = 0;

for (int temp = squareReversed; temp != 0; temp /= 10) {


int digit = temp % 10;
reversedSquare = reversedSquare * 10 + digit;
}

if (squareOriginal == reversedSquare) {
System.out.println(originalNumber + " is an Adam number.");
} else {
System.out.println(originalNumber + " is not an Adam number.");
}

}
}

34)perfect number or not


import java.util.Scanner;

public class PerfectNumberCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

int sum = 0;

for (int i = 1; i < number; i++) {


if (number % i == 0) {
sum += i;
}
}

if (sum == number) {
System.out.println(number + " is a perfect number.");
} else {
System.out.println(number + " is not a perfect number.");
}

scanner.close();
}
}

35. Convert Decimal to Binary


import java.io.*;
class binary {
public static void main(String[] args) {
int n = 17;
int[] binaryNum = new int[32];
int i;

for (i = 0; n > 0; i++) {


binaryNum[i] = n % 2;
n = n / 2;
}

for (int j = i - 1; j >= 0; j--)


System.out.print(binaryNum[j]);
}
}

Decimal to Octal using for loop

import java.io.*;

class hexa {
public static void main(String[] args) {
int n = 37;
String octal = "";

for (; n > 0; n = n / 8) {
octal = (n % 8) + octal;
}

System.out.println(octal);
}
}

Convert Decimal to Hexadecimal


import java.io.*;

class hexa{
public static void main(String[] args) {
int n = 37;
String hex = "";

for (; n > 0; n = n / 16) {


int remainder = n % 16;
hex = (remainder < 10 ? remainder : (char) ('A' + remainder - 10))
+ hex;
}

System.out.println(hex);
}
}

3. Convert Binary to Decimal


import java.io.*;

class hexa {
public static void main(String[] args) {
String binary = "10101";
int decimal = 0;
int base = 1;

for (int i = binary.length() - 1; i >= 0; i--) {


if (binary.charAt(i) == '1') {
decimal += base;
}
base *= 2;
}
System.out.println(decimal);
}
}

4. Convert Binary to Octal

import java.io.*;

class hexa{
public static void main(String[] args) {
String binary = "10101";
int decimal = 0;
int base = 1;

for (int i = binary.length() - 1; i >= 0; i--) {


if (binary.charAt(i) == '1') {
decimal += base;
}
base *= 2;
}

String octal = "";


for (; decimal > 0; decimal = decimal / 8) {
octal = (decimal % 8) + octal;
}
System.out.println(octal);
}
}

5. Convert Binary to Hexadecimal

import java.io.*;

class hexa{
public static void main(String[] args) {
String binary = "10101";
int decimal = 0;
int base = 1;

for (int i = binary.length() - 1; i >= 0; i--) {


if (binary.charAt(i) == '1') {
decimal += base;
}
base *= 2;
}

String hex = "";


for (; decimal > 0; decimal = decimal / 16) {
int remainder = decimal % 16;
hex = (remainder < 10 ? remainder : (char) ('A' + remainder - 10))
+ hex;
}
System.out.println(hex);
}
}

6. Convert Octal to Decimal

import java.io.*;

class hexa{
public static void main(String[] args) {
String octal = "25";
int decimal = 0;
int base = 1;

for (int i = octal.length() - 1; i >= 0; i--) {


decimal += (octal.charAt(i) - '0') * base;
base *= 8;
}
System.out.println(decimal);
}
}

7. Convert Octal to Binary

import java.io.*;

class hexa {
public static void main(String[] args) {
String octal = "25";
int decimal = 0;
int base = 1;

for (int i = octal.length() - 1; i >= 0; i--) {


decimal += (octal.charAt(i) - '0') * base;
base *= 8;
}

String binary = "";


for (; decimal > 0; decimal = decimal / 2) {
binary = (decimal % 2) + binary;
}
System.out.println(binary);
}
}
8. Convert Octal to Hexadecimal

import java.io.*;

class hexa {
public static void main(String[] args) {
String octal = "25";
int decimal = 0;
int base = 1;

for (int i = octal.length() - 1; i >= 0; i--) {


decimal += (octal.charAt(i) - '0') * base;
base *= 8;
}

String hex = "";


for (; decimal > 0; decimal = decimal / 16) {
int remainder = decimal % 16;
hex = (remainder < 10 ? remainder : (char) ('A' + remainder - 10))
+ hex;
}
System.out.println(hex);
}
}

9. Convert Hexadecimal to Decimal

import java.io.*;

class hexa {
public static void main(String[] args) {
String hex = "25";
int decimal = 0;
int base = 1;

for (int i = hex.length() - 1; i >= 0; i--) {


int digit = hex.charAt(i) >= 'A' ? hex.charAt(i) - 'A' + 10 :
hex.charAt(i) - '0';
decimal += digit * base;
base *= 16;
}
System.out.println(decimal);
}
}

10. Convert Hexadecimal to Binary

import java.io.*;

class hexa {
public static void main(String[] args) {
String hex = "25";
int decimal = 0;
int base = 1;
for (int i = hex.length() - 1; i >= 0; i--) {
int digit = hex.charAt(i) >= 'A' ? hex.charAt(i) - 'A' + 10 :
hex.charAt(i) - '0';
decimal += digit * base;
base *= 16;
}

String binary = "";


for (; decimal > 0; decimal = decimal / 2) {
binary = (decimal % 2) + binary;
}
System.out.println(binary);
}
}

11. Convert Hexadecimal to Octal

import java.io.*;

class hexa {
public static void main(String[] args) {
String hex = "25";
int decimal = 0;
int base = 1;

for (int i = hex.length() - 1; i >= 0; i--) {


int digit = hex.charAt(i) >= 'A' ? hex.charAt(i) - 'A' + 10 :
hex.charAt(i) - '0';
decimal += digit * base;
base *= 16;
}

String octal = "";


for (; decimal > 0; decimal = decimal / 8) {
octal = (decimal % 8) + octal;
}
System.out.println(octal);
}
}

1. Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10; // Number of terms
int a = 0, b = 1;

System.out.print("Fibonacci Series: ");


System.out.print(a + " " + b + " ");

for (int i = 2; i < n; i++) {


int next = a + b;
System.out.print(next + " ");
a = b;
b = next;
}
}
}

2. Pell Series
public class PellSeries {
public static void main(String[] args) {
int n = 10; // Number of terms
int p0 = 0, p1 = 1;

System.out.print("Pell Series: ");


System.out.print(p0 + " " + p1 + " ");

for (int i = 2; i < n; i++) {


int next = p0 + 2 * p1;
System.out.print(next + " ");
p0 = p1;
p1 = next;
}
}
}

3. Convert Time

i. Hour to Seconds

public class TimeConversion {


public static void main(String[] args) {
int hours = 2; // Example hours
int seconds;

for (seconds = hours * 3600; hours > 0; hours--) {


// Loop will exit when hours becomes 0
}

System.out.println("Hours to seconds: " + seconds);


}
}

ii. Minutes to Hours

java
Copy code
public class TimeConversion {
public static void main(String[] args) {
int minutes = 120; // Example minutes
int hours = 0;

for (; minutes >= 60; minutes -= 60) {


hours++;
}
System.out.println("Minutes to hours: " + hours);
}
}

iii. Seconds to Minutes

java
Copy code
public class TimeConversion {
public static void main(String[] args) {
int seconds = 3600; // Example seconds
int minutes = 0;

for (; seconds >= 60; seconds -= 60) {


minutes++;
}

System.out.println("Seconds to minutes: " + minutes);


}
}

1. Count Number of Digits


public class CountDigits {
public static void main(String[] args) {
int number = 12345; // Example number
int count = 0;

for (int temp = number; temp > 0; temp /= 10) {


count++;
}

System.out.println("Number of digits: " + count);


}
}

2. Calculate Sum of Digits


public class SumOfDigits {
public static void main(String[] args) {
int number = 12345; // Example number
int sum = 0;

for (int temp = number; temp > 0; temp /= 10) {


sum += temp % 10;
}

System.out.println("Sum of digits: " + sum);


}
}
3. Calculate Product of Digits
public class ProductOfDigits {
public static void main(String[] args) {
int number = 123; // Example number
int product = 1;

for (int temp = number; temp > 0; temp /= 10) {


product *= temp % 10;
}

System.out.println("Product of digits: " + product);


}
}
4) swap two number: first and last
import java.util.Scanner;
import static java.lang.Math.pow;

public class SwapDigits {


public static void main(String[] args) {
System.out.print("Enter any number of two or more digits: ");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int z = x;
int k = 0;
int num1, num2, num3, num4;

// Calculate the number of digits using a for loop


for (int temp = z; temp > 0; temp /= 10) {
k++;
}

// Extract the last digit


int lastDigit = x % 10;

// Compute the power of 10


int powerOfTen = 1;
for (int i = 1; i < k - 1; i++) {
powerOfTen *= 10;
}

// Calculate the number with the first digit in the last place and the last digit in the first place
num1 = lastDigit * powerOfTen;
num2 = x / 10; // Remove the last digit
num3 = num2 / 10 * 10; // Remove the first digit and add it at the end
num3 = num1 + num3;
num4 = x % powerOfTen; // Get the middle part of the number

int swappedNumber = num3 + num4;


System.out.print("Number after swapping first and last digits: " + swappedNumber);
}
}

5. Find Positive, Negative, and Zero


public class NumberType {
public static void main(String[] args) {
int n = -5; // Example number

if (n > 0) {
System.out.println("The number is positive.");
} else if (n < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}

6. Find Biggest Number for N Time's Value


public class BiggestNumber {
public static void main(String[] args) {
int n = 5; // Number of values
int biggest = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {


int num = i * 10; // Example values
if (num > biggest) {
biggest = num;
}
}

System.out.println("Biggest number: " + biggest);


}
}

7. Find Smallest Number for N Time's Value


public class SmallestNumber {
public static void main(String[] args) {
int n = 5; // Number of values
int smallest = Integer.MAX_VALUE;

for (int i = 0; i < n; i++) {


int num = i * 10; // Example values
if (num < smallest) {
smallest = num;
}
}
System.out.println("Smallest number: " + smallest);
}
}

8. Sum of Numbers for N Time Value


public class SumOfNumbers {
public static void main(String[] args) {
int n = 5; // Number of values
int sum = 0;

for (int i = 1; i <= n; i++) {


sum += i; // Example sum of first N natural numbers
}

System.out.println("Sum of numbers: " + sum);


}
}

9. Sum of Odd Numbers for Times Value


public class SumOfOddNumbers {
public static void main(String[] args) {
int n = 5; // Number of odd numbers
int sum = 0;

for (int i = 1; i <= n * 2; i += 2) {


sum += i;
}

System.out.println("Sum of odd numbers: " + sum);


}
}

10. Sum of Even Numbers for Times Value


public class SumOfEvenNumbers {
public static void main(String[] args) {
int n = 5; // Number of even numbers
int sum = 0;

for (int i = 2; i <= n * 2; i += 2) {


sum += i;
}

System.out.println("Sum of even numbers: " + sum);


}
}

11. Find Sum and Average for N Time's Number


public class SumAndAverage {
public static void main(String[] args) {
int n = 5; // Number of values
int sum = 0;

for (int i = 1; i <= n; i++) {


sum += i; // Example sum of first N natural numbers
}

double average = (double) sum / n;

System.out.println("Sum: " + sum);


System.out.println("Average: " + average);
}
}

12. Find Highest Common Factor (HCF) for 2 Numbers


public class HCF {
public static void main(String[] args) {
int a = 36;
int b = 60;
int hcf = 1;

for (int i = 1; i <= Math.min(a, b); i++) {


if (a % i == 0 && b % i == 0) {
hcf = i;
}
}

System.out.println("HCF: " + hcf);


}
}

13. Find Least Common Multiple (LCM) for 2 Numbers


public class LCM {
public static void main(String[] args) {
int a = 36;
int b = 60;
int lcm = a;

for (; lcm % b != 0; lcm += a) {


// Loop until lcm is divisible by b
}

System.out.println("LCM: " + lcm);


}
}

14. Check Whether Given Number is Prime


public class PrimeCheck {
public static void main(String[] args) {
int number = 29; // Example number
boolean isPrime = true;
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

System.out.println("The number is " + (isPrime ? "prime" : "not


prime"));
}
}

15. Check Whether Given Number is Automorphic


public class AutomorphicCheck {
public static void main(String[] args) {
int number = 25; // Example number
int square = number * number;
int length = 0;
int temp = number;

for (temp = number; temp > 0; temp /= 10) {


length++;
}

// Extract the last digits from the square


int lastDigits = square % (int) Math.pow(10, length);

System.out.println("The number is " + (lastDigits == number ?


"automorphic" : "not automorphic"));
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy