Number problems

Today we gonna see some interesting number problems program like increament,number reverse, modulus etc.,

ForCondition

package excercise;

public class ForCondition {

public static void main(String[] args) {

// TODO Auto-generated method stub

for(;;) {

System.out.println(“hi”);

break;

}

}

}

RESULT:

hi

ForLoop

package excercise;

public class ForLoop {

public static void main(String[] args) {

int no=5;

for (no=5;no>=1; no–)

{

System.out.println(no);

//break;

}

}

}

RESULT:

5

4

3

2

1

LoopReductionMethod

package excercise;

public class LoopReductionMethod {

public static void main(String[] args) {

int no=1;

while (no<19)

{

System.out.println(no);

no+=2;

if(no==11)

{

no=2;

}

}

}

RESULT:

1

3

5

7

9

2

4

6

8

10

12

14

16

18

Modulus

package excercise;

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

MultipleIncrement

package excercise;

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

NumberCondition

package excercise;

public class NumberCondition {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1=1,no2=10;

while(no1<no2)

{

System.out.println(no1*no2);

no1++;

no2–;

}

}

}

RESULT:

10

18

24

28

30

NumberReverse

package excercise;

public class NumberReverse {

public static void main(String[] args) {

int no = 5;

while (true) {

System.out.println(no);// print the value infinitely if we didnt use no–

no–;// we need to stop the count use break

if (no == 0)

break;

}

}

}

RESULT:

5

4

3

2

1

OrOperator

package excercise;

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

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

SquareRoot

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

SwapingOut

package excercise;

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

5

15

Leave a comment

Design a site like this with WordPress.com
Get started