Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
3
Indexable
Never
Author: Mehrab
Task1:

import java.util.Scanner;

public class Area {
   public static void main(String args[]){
      int radius;
      double area;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the radius of the circle ::");
      radius = sc.nextInt();
      area = (radius*radius)*Math.PI;
      System.out.println("Area of the circle is ::"+area);
   }
}

Task2:

import java.util.Scanner;

public class EvenOdd {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        if(num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
    }
}


Task3:

import java.util.Scanner;

public class Factorial {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i;
		int fact = 1;
		int number;
		System.out.println("Enter a number: ");
		Scanner sc = new Scanner(System.in);
		number = sc.nextInt();
		for(i=1; i<=number; i++)
		{
			fact = fact * i;
		}
		
		 System.out.println("Factorial of "+number+" is: "+fact);
	}

}


Task4:

import java.util.Scanner;

public class PalindromeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        
        if (isPalindrome(input)) {
            System.out.println("The given string is a palindrome.");
        } else {
            System.out.println("The given string is not a palindrome.");
        }
        
        scanner.close();
    }

    public static boolean isPalindrome(String str) {

        String cleanStr = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        
        String reversedStr = new StringBuilder(cleanStr).reverse().toString();
        
        return cleanStr.equals(reversedStr);
    }
}


Task 5:
import java.util.Scanner;

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

        if (number <= 1) {
            isPrime = false;
        } else {

            for (int i = 2; i <= Math.sqrt(number); i++) {
                if (number % i == 0) {
                    isPrime = false; 
                    break;
                }
            }
        }

        if (isPrime) {
            System.out.println(number + " is a prime number.");
        } else {
            System.out.println(number + " is not a prime number.");
        }

        scanner.close();
    }
}
Leave a Comment