1.
Even Odd Number
package com.practise;
public class EvenOddNumber {
public static void main(String[] args) {
int num =123;
if(num%2==0)
{
System.out.println(num+ " : The Number is Even");
}
else
{
System.out.println(num+ " : The Number is Odd");
}
}
}
Output is : 123 : The Number is Odd
2. Factorial Number
package com.practise;
public class FactorialNumber {
public static void main(String[] args) {
int a = 5;
int fact = 1;
for(int i=1;i<=a;i++)
{
fact = fact * i;
}
System.err.println("Factorial of "+ a +" is : "+fact);
}
}
Output is: Factorial of 5 is : 120
3. Palindrome Number
package com.practise;
public class PalindromeNumber {
public static void main(String[] args) {
int num = 121;
int temp = num;
int rev = 0 , rem;
while(temp!=0)
{
rem = temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(num==rev)
{
System.out.println(num +" : is Palindrome");
}
else
{
System.out.println(num +": is Not Palindrome");
}
}
}
Output is: 121 : is Palindrome
4. Prime Number
package com.practise;
public class PrimeNumber {
public static void main(String[] args) {
int num = 7;
int temp = 0;
for(int i=2;i<=num-1;i++)
{
if(num%i==0)
{
temp=temp+1;
}
}
if(temp==0)
{
System.out.println(num +" : Number is prime");
}
else
{
System.out.println(num +" : Number is not prime");
}
}
}
Output is: 7 : Number is prime
5. Prime Number Upto 100
package com.practise;
public class PrimeNumber100 {
public static void main(String[] args) {
for(int num=0;num<=100;num++)
{
int temp = 0;
for(int i=2;i<=num-1;i++)
{
if(num%i==0)
{
temp=temp+1;
}
}
if(temp==0)
{
System.out.println(num +" : Number is prime");
}
}
}
Output is:
0 : Number is prime
1 : Number is prime
2 : Number is prime
3 : Number is prime
5 : Number is prime
7 : Number is prime
11 : Number is prime
13 : Number is prime
17 : Number is prime
19 : Number is prime
23 : Number is prime
29 : Number is prime
31 : Number is prime
37 : Number is prime
41 : Number is prime
43 : Number is prime
47 : Number is prime
53 : Number is prime
59 : Number is prime
61 : Number is prime
67 : Number is prime
71 : Number is prime
73 : Number is prime
79 : Number is prime
83 : Number is prime
89 : Number is prime
97 : Number is prime
6. Reverse of Each letter in String
package com.practise;
public class ReverseEachLetter {
public static void main(String[] args) {
String s =" Java tech";
String[] words = s.split(" ");
String rev = "";
for(int i=0;i<words.length;i++)
{
String word = words[i];
String revword="";
for(int j=word.length()-1;j>=0;j--)
{
revword=revword+word.charAt(j);
}
System.out.print(revword+" ");
}
}
}
Output is : avaJ hcet
7. Reverse Number
package com.practise;
public class ReverseNumber {
public static void main(String[] args) {
int num = 123, rev = 0,rem;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
System.out.println("The Reverse Number is : "+rev);
}
Output is: The Reverse Number is : 321
8. Reverse String
package com.practise;
public class ReverseString {
public static void main(String[] args) {
String s = "yash";
String rev = "";
int length = s.length();
for(int i=length-1;i>=0;i--)
{
rev=rev+s.charAt(i);
}
System.out.println("The Reverse of the string : "+ s +" : is = "+rev);
}
}
Output is: The Reverse of the string : yash : is = hsay
9. Reverse of Sentence
package com.practise;
public class ReverseSentence {
public static void main(String[] args) {
String s = "My name is yash";
String[] split = s.split(" ");
String result=" ";
for(int i=split.length-1;i>=0;i--)
{
if(i==0)
{
result=result+split[i];
}
else
{
result=result+split[i]+" ";
}
}
System.out.println("The Reverse of the String : "+s+" = "+result);
}
}
Output is: The Reverse of the String : My name is yash = yash is name My
10. Swap With third variable
package com.practise;
public class SwapWithThirdVariable {
public static void main(String[] args) {
int a=10,b=20;
System.out.println("The number before the swapping is, first Numbr is :
"+a+" and Second Number is : "+b );
int temp=a;
a=b;
b=temp;
System.out.println("The number After the swapping is, first Numbr is :
"+a+" and Second Number is : "+b );
}
}
Output is: The number before the swapping is, first Numbr is : 10 and Second Number
is : 20
The number After the swapping is, first Numbr is : 20 and Second Number is : 10
11. Swap without third variable
package com.practise;
public class SwapWithoutThirdVariable {
public static void main(String[] args) {
int a=10,b=20;
System.out.println("The number before the swapping is, first Numbr is :
"+a+" and Second Number is : "+b );
a=a+b;
b=a-b;
a=a-b;
System.out.println("The number After the swapping is, first Numbr is :
"+a+" and Second Number is : "+b );
}
}
Output is: The number before the swapping is, first Numbr is : 10 and Second Number
is : 20
The number After the swapping is, first Numbr is : 20 and Second Number is : 10