All Posts By

mathanlalsait@gmail.com

Java Code To Check a Given Number Is Odd or Even

March 10, 2023

Program:

public class Odd 
{
	public static void main(String[] args) 
	{
		int x = 16;
		int y = x % 2;
		if (y == 0) 
		{
			System.out.println("The given number is Even ");
		} 
		else 
		{
			System.out.println("The given number is odd ");
		}
	}
}

Compile:

javac Odd.java
java  Odd 

Output:

The given number is Even

Java Code To Check Given Number Is Prime Number or Not

March 10, 2023

Program:

public class Prime 
{
	public static void main(String args[]) 
	{
		int num = 29;
		boolean flag = false;
		for (int i = 2; i <= num / 2; i++) 
		{
			if (num % i == 0) 
			{
				flag = true;
				break;
			}
		}
		if (!flag)
			System.out.println(num + " is a prime number.");
		else
			System.out.println(num + " is not a prime number.");

	}
}

Compile:

javac Prime.java
java  Prime

Output:

29 is a prime number.

Java Code To Check Swap Two Numbers [Without Temp Variable]

March 7, 2023

Program:

import java.util.*;

public class SwapWithout 
{
	public static void main(String[] args) 
	{
		int x = 100;
		int y = 200;
		x = x + y;
		y = x - y;
		x = x - y;
		System.out.println("X=" + x + " and Y=" + y);
	}
}

Compile:

javac SwapWithout.java
java SwapWithout

Output:

X=200 and Y=100

Java Code To Check Swap Two Numbers [with Temp Variable]

March 7, 2023

Program

public class SwapWith 
{
	public static void main(String[] args) 
	{
		int a = 10;
		int b = 8;
		int temp;
		temp = b;
		b = a;
		a = temp;
		System.out.println(a);
		System.out.println(b);
	}
}

Compile:

javac SwapWith.java
java SwapWith

Output:

8
10

Java Code To Check Print the Reverse Of a Given Number

March 4, 2023
public class ReverseNumber 
{
	public static void main(String args[]) 
	{
		int num = 654321, reverse = 0;

		while (num != 0) 
		{
			int remainder = num % 10;
			reverse = reverse * 10 + remainder;
			num = num / 10;
		}
		System.out.println("The reverse of the given number is:" + reverse);
	}
}

Compile:

javac ReverseNumber.java
java ReverseNumber

Output:

The reverse of the given number is : 123456

Java Code To Check Print 1 to 100

March 3, 2023
public class Print 
{
	public static void main(String args[]) 
	{
		int n = 100;
		System.out.println("Numbers from 1 to 100 is " + n + "are:");
		for (int i = 1; i <= n; i++) 
		{
			System.out.println(i + " ");
		}
	}
}

Compile:

javac Print.java
java Print

Output:

Numbers from 1 to 100 is 100 are:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 

Java Code To Check Sum Of First 10 Natural Number

March 3, 2023
public class Natural 
{
	public static void main(String args[]) 
	{
		int i, num = 10, sum = 0;
		for (i = 1; i <= num; i++) 
		{
			sum = sum + i;
		}
		System.out.println(sum);
	}
}

Compile:

javac Natural.java
java Natural

Output:

55

Java Code To Check Fibonacci Series

March 3, 2023
public class Fibonacci
{
  public static void main(String[] args)
   {
		int n1=0,n2=1,n3,i,count=10;
		System.out.print(n1+" "+n2);
		
		for(i=2;i<count;i++)
		{
			n3=n1+n2;
			System.out.print(" "+n3);
			n1=n2;
			n2=n3;
		}
   }
}

Compile:

javac Fibonacci.java
java Fibonacci

Output:

0 1 1 2 3 5 8 13 21 34

Java Code To Check ASCII Value

March 3, 2023
public class ASCII
{
	public static void main(String[] args)
	{
		int i;
		for (i = 0; i <= 127; i++) 
		{
			System.out.println(" The ASCII value of " + (char) i + "=" + i);
		}
	}
}

Compile:

javac ASCII.java
java ASCII

Output:

 The ASCII value of =0
 The ASCII value of =1
 The ASCII value of =2 
 The ASCII value of =3
 The ASCII value of =4
 The ASCII value of =5 
 The ASCII value of =6 
 The ASCII value of =7
 The ASCII value of =8 
 The ASCII value of =9 
 The ASCII value of =10 
 The ASCII value of =11
 The ASCII value of =12 
 The ASCII value of =13
 The ASCII value of =14 
 The ASCII value of =15
 The ASCII value of =16 
 The ASCII value of =17 
 The ASCII value of =18
 The ASCII value of =19
 The ASCII value of .=20
 The ASCII value of =21
 The ASCII value of =22
 The ASCII value of =23
 The ASCII value of =24
 The ASCII value of =25
 The ASCII value of =26
 The ASCII value of =27
 
 The ASCII value of =28
 The ASCII value of =29
 The ASCII value of =30
 The ASCII value of =31
 The ASCII value of  =32
 The ASCII value of !=33
 The ASCII value of "=34
 The ASCII value of #=35
 The ASCII value of $=36
 The ASCII value of %=37
 The ASCII value of &=38
 The ASCII value of '=39
 The ASCII value of (=40
 The ASCII value of )=41
 The ASCII value of *=42
 The ASCII value of +=43
 The ASCII value of ,=44
 The ASCII value of -=45
 The ASCII value of .=46
 The ASCII value of /=47
 The ASCII value of 0=48
 The ASCII value of 1=49
 The ASCII value of 2=50
 The ASCII value of 3=51
 The ASCII value of 4=52
 The ASCII value of 5=53
 The ASCII value of 6=54
 The ASCII value of 7=55
 The ASCII value of 8=56
 The ASCII value of 9=57
 The ASCII value of :=58
 The ASCII value of ;=59
 The ASCII value of <=60
 The ASCII value of ==61
 The ASCII value of >=62
 The ASCII value of ?=63
 The ASCII value of @=64
 The ASCII value of A=65
 The ASCII value of B=66
 The ASCII value of C=67
 The ASCII value of D=68
 The ASCII value of E=69
 The ASCII value of F=70
 The ASCII value of G=71
 The ASCII value of H=72
 The ASCII value of I=73
 The ASCII value of J=74
 The ASCII value of K=75
 The ASCII value of L=76
 The ASCII value of M=77
 The ASCII value of N=78
 The ASCII value of O=79
 The ASCII value of P=80
 The ASCII value of Q=81
 The ASCII value of R=82
 The ASCII value of S=83
 The ASCII value of T=84
 The ASCII value of U=85
 The ASCII value of V=86
 The ASCII value of W=87
 The ASCII value of X=88
 The ASCII value of Y=89
 The ASCII value of Z=90
 The ASCII value of [=91
 The ASCII value of \=92
 The ASCII value of ]=93
 The ASCII value of ^=94
 The ASCII value of _=95
 The ASCII value of `=96
 The ASCII value of a=97
 The ASCII value of b=98
 The ASCII value of c=99
 The ASCII value of d=100
 The ASCII value of e=101
 The ASCII value of f=102
 The ASCII value of g=103
 The ASCII value of h=104
 The ASCII value of i=105
 The ASCII value of j=106
 The ASCII value of k=107
 The ASCII value of l=108
 The ASCII value of m=109
 The ASCII value of n=110
 The ASCII value of o=111
 The ASCII value of p=112
 The ASCII value of q=113
 The ASCII value of r=114
 The ASCII value of s=115
 The ASCII value of t=116
 The ASCII value of u=117
 The ASCII value of v=118
 The ASCII value of w=119
 The ASCII value of x=120
 The ASCII value of y=121
 The ASCII value of z=122
 The ASCII value of {=123
 The ASCII value of |=124
 The ASCII value of }=125
 The ASCII value of ~=126
 The ASCII value of =127

Java Code To Check Amstrong Number

March 3, 2023
public class Armstrong
{
	public static void main(String[] args)
	{
		int i = 1, a, arm, n, temp;
		System.out.println("Armstrong number between 1 to 500 are");

		while (i < 500)
		{
			n = i;
			arm = 0;
			while (n > 0)
			{
				a = n % 10;
				arm = arm + (a * a * a);
				n = n / 10;
			}
			if (arm == i)
				System.out.println(i);
			i++;
		}
	}
}

Compile:

javac Armstrong.java
java Armstrong

Output:

Armstrong number between 1 to 500 are
1
153
370
371
407