0% found this document useful (0 votes)
2 views3 pages

Simple Java programs

The document contains Java programs that perform various number-related checks and operations. It includes functionalities to determine if a number is even or odd, check for primality, reverse a number, check if a number is a palindrome, and calculate the sum of its digits. Each program prompts the user for input and outputs the result based on the specified operation.

Uploaded by

binduann
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)
2 views3 pages

Simple Java programs

The document contains Java programs that perform various number-related checks and operations. It includes functionalities to determine if a number is even or odd, check for primality, reverse a number, check if a number is a palindrome, and calculate the sum of its digits. Each program prompts the user for input and outputs the result based on the specified operation.

Uploaded by

binduann
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/ 3

✅ Check if a number is Even or Odd

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("Even Number");
else
System.out.println("Odd Number");

sc.close();
}
}

✅ Check whether the number is prime number or not


import java.util.Scanner;

public class SimplePrimeCheck {


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

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


if (num % i == 0)
count++;
}

if (count == 2)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");

sc.close();
}
}

✅ Reverse of a number
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int rev = 0;

while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

System.out.println("Reversed Number: " + rev);


sc.close();
}
}

✅ Check whether the number is palindrome or not


import java.util.Scanner;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num, rev = 0;

while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

if (original == rev)
System.out.println("It is a Palindrome Number.");
else
System.out.println("It is NOT a Palindrome Number.");

sc.close();
}
}

✅ Sum of digits
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

int sum = 0;

while (num > 0) {


int digit = num % 10; // Get the last digit
sum += digit; // Add it to sum
num = num / 10; // Remove the last digit
}

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


sc.close();
}
}

You might also like