Here we gonna see some simple interesting programs are modulus,multiple increment, narrow casting,number reverse, or operator, palindrome,qube,reverse order, smallest divisor,square root,sum of digits, swaping out,table 3 and their results.
MODULUS
package selva.day1;
public class Modulus {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=1;
while(no<=50)
{
if(no%3==0) {
if(no%5==0)
{
System.out.println(no);
}
}
no++;
}
}
}
RESULT:
15
30
45
MULTIPLE INCREMENT
package selva.day1;
public class MultipleIncrement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=1;
while(no<=5) {
System.out.println(no*(no+1)*(no+2));
no++;
}
}
}
RESULT:
6
24
60
120
210
NARROW CASTING
package selva.day1;
public class NarrowCasting {
public static void main(String[] args) {
// TODO Auto-generated method stub
double myDouble=9.78;
int myint= (int) myDouble;//manual casting; double to int
System.out.println(myDouble);
System.out.println(myint);
}
}
RESULT
9.78
9
NUMBER REVERSE
package selva.day1;
public class NumberReverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no = 5;
while (true) {
System.out.println(no);// print the value infinitely if we didnt use no–
no–;
if (no == 0)
break;// we need to stop the count use break
}
}
}
RESULT
5
4
3
2
1
OR OPERATOR
package selva.day1;
public class OrOperator {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no;
for (no=1;no<=50;no++)
{
if(no%3==0||no%5==0)
{
System.out.println(no);
}
}
}
}
RESULT
3
5
6
9
10
12
15
18
20
21
24
25
27
30
33
35
36
39
40
42
45
48
50
PALLINDROME
package selva.day1;
public class Pallindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no = 121;
int no1 = no;
int rev = 0;
int rem;
while (no > 0) {
rem = no % 10;
rev = (rev * 10) + rem;
no = no / 10;
}
System.out.println(rev);
if (no1 == rev) {
System.out.println(“PALINDROME”);
} else {
System.out.println(“NOT PALINDROME”);
}
}
}
RESULT
121
PALINDROME
QUBE
package selva.day1;
public class Qube {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no = 1, count;
while (no <= 4) {
count = no * no * no;
System.out.println(count);
no = no + 1;
}
}
}
RESULT
1
8
27
64
REVERSE ORDER
package selva.day1;
public class ReverseOrder {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=1234;
int rev=0;
int rem;
while(no>0) {
rem=no%10;
rev=(rev*10)+rem;
no=no/10;
}
System.out.println(rev);
}
}
RESULT
4321
SMALLEST DIVISOR
package selva.day1;
public class SmallestDivisor {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=105;
int div=2;
while(div<=no)
{
if(no%div==0)
{
System.out.println(div);
break;
}
div++;
}
}
}
RESULT
3
SQUARE ROOT
package selva.day1;
public class Squareroot {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=81;
int div=2;
while(div<=no)
{
if(no/div==div) {
System.out.println(div+” is root of ” + no);
break;
}
div++;
}
}
}
RESULT
9 is root of 81
SUM OF DIGITS
package selva.day1;
public class SumOfDigits {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=1234;
int rev=0,sumofdigits=0;
int rem;
while(no>0) {
rem=no%10;
sumofdigits=sumofdigits+rem;
no=no/10;
}
System.out.println(sumofdigits);
}
}
RESULT
10
SWAPINGOUT
package selva.day1;
public class SwapingOut {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a, b, c;
a = 5;
b = 10;
c = 15;
a = (a + b + c);
b = (a – (b + c));
a = (a – (b + c));
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
RESULT
10
TABLE 3
package selva.day1;
public class Table3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int no = 1,no1;
while (no <= 21) {
no1=no*3;
System.out.println(no + “*3=” + no1);
no++;
}
}
}
RESULT
1*3=3
2*3=6
3*3=9
4*3=12
5*3=15
6*3=18
7*3=21
8*3=24
9*3=27
10*3=30
11*3=33
12*3=36
13*3=39
14*3=42
15*3=45
16*3=48
17*3=51
18*3=54
19*3=57
20*3=60
21*3=63
TYPE CASTING
package selva.day1;
public class TypeCasting {
public static void main(String[] args) {
// TODO Auto-generated method stub
int myInt=8;
double myDouble=myInt;// auto convert double to int
System.out.println(myInt);//8
System.out.println(myDouble);//8.0
}
}
RESULT
8
8.0