[go: up one dir, main page]

0% found this document useful (0 votes)
4 views4 pages

Java Assignment Std10

The document contains ten Java programs that perform various mathematical and pattern operations. These include calculating the sum, factorial, reverse of a number, LCM, HCF, prime checks, twin prime checks, twisted prime checks, and generating two different patterns. Each program includes a main method for user input and displays the result of the respective operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Assignment Std10

The document contains ten Java programs that perform various mathematical and pattern operations. These include calculating the sum, factorial, reverse of a number, LCM, HCF, prime checks, twin prime checks, twisted prime checks, and generating two different patterns. Each program includes a main method for user input and displays the result of the respective operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Sum using Add()

import java.util.Scanner;

public class SumProgram {


public static int Add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt(), b = sc.nextInt();
System.out.println("Sum: " + Add(a, b));
}
}

2. Factorial using Fact()

public class FactorialProgram {


public static int Fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) fact *= i;
return fact;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Factorial: " + Fact(n));
}
}

3. Reverse using Rev()

public class ReverseProgram {


public static int Rev(int n) {
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Reverse: " + Rev(n));
}
}
4. LCM using LCM()

public class LCMProgram {


public static int LCM(int a, int b) {
int max = Math.max(a, b);
while (true) {
if (max % a == 0 && max % b == 0) return max;
max++;
}
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt(), b = sc.nextInt();
System.out.println("LCM: " + LCM(a, b));
}
}

5. HCF using HCF()

public class HCFProgram {


public static int HCF(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt(), b = sc.nextInt();
System.out.println("HCF: " + HCF(a, b));
}
}

6. Prime Check using Isprime()

public class PrimeCheck {


public static boolean Isprime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0) return false;
return true;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println(Isprime(n) ? "Prime" : "Not Prime");
}
}

7. Twin Prime Check using Isprime()

public class TwinPrimeCheck {


public static boolean Isprime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0) return false;
return true;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt(), b = sc.nextInt();
if (Isprime(a) && Isprime(b) && Math.abs(a - b) == 2)
System.out.println("Twin Primes");
else
System.out.println("Not Twin Primes");
}
}

8. Twisted Prime Check using Isprime() and Rev()

public class TwistedPrimeCheck {


public static boolean Isprime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0) return false;
return true;
}

public static int Rev(int n) {


int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (Isprime(n) && Isprime(Rev(n)))
System.out.println("Twisted Prime");
else
System.out.println("Not Twisted Prime");
}
}
9. Pattern1()

public class Pattern1 {


public static void Pattern1() {
for (int i = 0; i < 5; i++) {
for (int j = 1; j <= 5; j++)
System.out.print((j + i - 1) % 5 + 1 + " ");
System.out.println();
}
}

public static void main(String[] args) {


Pattern1();
}
}

10. Pattern2()

public class Pattern2 {


public static void Pattern2() {
for (int i = 0; i < 5; i++) {
for (int s = 0; s < i; s++) System.out.print("\t");
for (int j = 1; j <= 5 - i; j++) System.out.print(j + " ");
for (int j = 0; j < i; j++) System.out.print((5 - i) + " ");
System.out.println();
}
}

public static void main(String[] args) {


Pattern2();
}
}

You might also like