Ex 1:
- Print : 767 495 359 291 257 240 232 228
Idea:
767 495 359 291 257 240 232 228
- Diff between above numbers: 272 136 68 34 17 8 4 2
- 272/2 = 136
- 136/2 = 68
- 68/2 = 34……….
public class Practice6 {
public static void main(String[] args) {
int a = 767;
int b = 272;
while (b > 1) {
System.out.print(a + " ");
a = a - b;
b = b / 2;
}
}
}
Output:
767 495 359 291 257 240 232 228
Ex 2:
- Print: 7 13 25 49 97 193 385 769
Idea:
- 7 13 25 49 97 193 385 769
- Diff between above numbers: 6 12 24 48 96 192 384
- 6 + 6 = 12
- 12 + 12 = 24
- 24 + 24 = 48
- 48 + 48 = 96……..
public class Practice6 {
public static void main(String[] args) {
int a = 7;
int b = 6;
while (a < 800) {
System.out.print(a + " ");
a = a + b;
b = b + b;
}
}
}
Output:
7 13 25 49 97 193 385 769
Ex 3:
- Print: 7 13 25 49 85 133 193
idea:
- 7 13 25 49 85 133 193
- Diff between above numbers: 6 12 24 36 48 60
- 6 is common number
- 2, 4, 6, 8, 10 are multiply with 6.
- 6 is common number
public class Practice6 {
public static void main(String[] args) {
int a = 7;
int b = 6;
int c = 0;
System.out.print(a + " ");
while (c <= 10) {
a = a + b;
System.out.print(a + " ");
c = c + 2;
b = 6 * c;
}
}
}
Output:
7 13 25 49 85 133 193
Ex 4:
- Print: 9 13 22 36 55 79 108
Idea:
- 9 13 22 36 55 79 108
- Diff between above numbers: 4 9 14 19 24 29
- Diff Between 4 9 14 19 24 29 numbers : 5
- Diff between above numbers: 4 9 14 19 24 29
public class Practice6 {
public static void main(String[] args) {
int a = 9;
int b = 4;
while (a <= 110) {
System.out.print(a + " ");
a = a + b;
b = b + 5;
}
}
}
Output:
9 13 22 36 55 79 108
Ex 5:
- 2 4 12 48 240 1440 10080 80640 725760 7257600
Idea:
2*1 = 2
2*2 = 4
4*3 = 12
12*4 = 48
48*5 = 240
etc…….
public class Practice6 {
public static void main(String[] args) {
int a = 2;
int b = 1;
while (b <= 10) {
a = a * b;
System.out.print(a + " ");
b = b + 1;
}
}
}
Output:
2 4 12 48 240 1440 10080 80640 725760 7257600
Ex 6:
- Print: 1 2 6 15 31 56 92 141 205 286 386
Idea:
- 1 2 6 15 31 56 92 141 205 286 386
- Diff between numbers: 1 4 9 16 25 36 49 64 81 100
- Diff between above numbers: 3 5 7 9 11 13 15 17 19
- Diff between 3 5 7 9 11 13 15 17 19 numbers: 2
- Diff between above numbers: 3 5 7 9 11 13 15 17 19
- Diff between numbers: 1 4 9 16 25 36 49 64 81 100
public class Practice6 {
public static void main(String[] args) {
int a = 1;
int b = 1;
int c = 1;
System.out.print(a + " ");
while (b <= 20) {
a = a + c;
System.out.print(a + " ");
b = b + 2;
c = c + b;
}
}
}
Output:
1 2 6 15 31 56 92 141 205 286 386
Ex 7:
- Print Triangular number series: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105
Idea:
- 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105
- Diff between numbers: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
public class Practice6 {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.print(a + " ");
while (b <= 14) {
a = a + b;
System.out.print(a + " ");
b = b + 1;
}
}
}
Output:
1 3 6 10 15 21 28 36 45 55 66 78 91 105
JAVA INFINITE LOOP
- A while loop can be an infinite loop if we skip writing the update statement inside its body or the condition is always true.
Syntax:
while(true condition)
{
// while loop statements
}
BREAK STATEMENT
- Break is useful when we want to stop the loop when a certain condition meets.
Ex:
public class Practice6 {
public static void main(String[] args) {
int num = 0;
while (true)
{
if (num == 7)
{
break;
}
System.out.println("counting..." + num);
num++;
}
}
}
Output:
counting…0
counting…1
counting…2
counting…3
counting…4
counting…5
counting…6
CONTINUE STATEMENT
- Using continue, we can skip the current statement of a loop and jumps to the next statement of the loop immediately.
Ex:
public class Practice6 {
public static void main(String[] args) {
int num = 0;
while (num < 10)
{
if (num == 5)
{
num++;
continue;
}
System.out.println("counting..." + num);
num++;
}
}
}
Output:
counting…0
counting…1
counting…2
counting…3
counting…4
counting…6
counting…7
counting…8
counting…9
Ex 8:
- Print Odd numbers using continue
public class Practice6 {
public static void main(String[] args) {
int num = 0;
while (num < 20)
{
if (num % 2 == 0)
{
num++;
continue;
}
System.out.print(num + " ");
num++;
}
}
}
Output:
1 3 5 7 9 11 13 15 17 19
Ex 9:
- Print which numbers are divided by 10 (using continue statement). After 100 stop the loop.
public class Practice6 {
public static void main(String[] args) {
int num = 1;
while (true)
{
if (num % 10 != 0)
{
num++;
continue;
}
if (num > 100)
{
break;
}
System.out.print(num + " ");
num++;
}
}
}
Output:
10 20 30 40 50 60 70 80 90 100
Ex 10:
- Print reverse number of 89532.
public class Practice6 {
public static void main(String[] args) {
int a = 89532;
int b;
while (a > 0)
{
b = a % 10;
System.out.print(b + " ");
a = a / 10;
}
}
}
Output:
2 3 5 9 8
Ex 11:
- Print Reverse number of 505050
- And also find addition of digits
public class Practice6 {
public static void main(String[] args) {
int a = 505050;
int b;
int sum = 0;
while (a > 0)
{
b = a % 10;
System.out.println(b + " ");
sum = sum + b;
a = a / 10;
}
System.out.println("sum of digits " + sum);
}
}
Output:
0
5
0
5
0
5
sum of digits 15
Ex 12:
- Find Sum of Digits: 978987321
public class Practice6 {
public static void main(String[] args) {
int a = 978987321;
int b;
int sum = 0;
while (a > 0)
{
b = a % 10;
sum = sum + b;
a = a / 10;
}
System.out.println("sum of digits " + sum);
}
}
Output:
sum of digits 54
Ex 13:
- 987654321
- Decrease each digit from right to left
public class Practice6 {
public static void main(String[] args) {
int a = 987654321;
int b;
while (a > 0)
{
System.out.println(a);
a = a / 10;
}
}
}
Output:
987654321
98765432
9876543
987654
98765
9876
987
98
9
Ex 14:
- Print: 2 6 8 12 14 18 20 24
Idea:
- Initially 2.
- Add 4 then Add 2
public class Practice6 {
public static void main(String[] args) {
int a = 2;
int b;
while (a <= 20)
{
System.out.print(a + " ");
b = a + 4;
System.out.print(b + " ");
a = b + 2;
}
}
}
Output:
2 6 8 12 14 18 20 24
Ex 15:
- Addition of even numbers from 0 to 20.
public class Practice6 {
public static void main(String[] args) {
int a = 2;
int sum = 0;
while (a <= 20)
{
sum = sum + a;
a = a + 2;
}
System.out.println("Addition of even numbers: " + sum);
}
}
Output:
Addition of even numbers: 110
Ex 16:
- Print Factorial number of 4!
- factorial of 4 is 4×3×2×1, which is equal to 24
public class Practice6 {
public static void main(String[] args) {
int a = 4;
int fact = 1;
while (a >= 1)
{
fact = fact * a;
a = a - 1;
}
System.out.println("Factorial of number 4: " + fact);
}
}
Output:
Factorial of number 4: 24
public class Practice1 {
public static void main(String[] args) {
int factorial = 1;
int a = 1;
while (a <= 4) {
factorial = factorial * a;
a++;
}
System.out.println("factorial is : " + factorial);
}
}
Output:
factorial is : 24
Ex 17:
Fibonacci:
- Next number is the sum of previous two numbers
- The first two numbers of Fibonacci series are 0 and 1.
public class Practice1 {
public static void main(String[] args) {
int first = -1;
int second = 1;
while ((first + second) <= 55)
{
System.out.print((first + second) + " ");
second = first + second;
first = second - first;
}
}
}
Output:
0 1 1 2 3 5 8 13 21 34 55
Ex 18:
- Print each two digits from right to left: 123456
public class Practice6 {
public static void main(String[] args) {
int a = 123456;
int b;
while (a > 0)
{
b = a % 100;
System.out.println(b + " ");
a = a / 100;
}
}
}
Output:
56
34
12
With havin so much written content do you ever run into any issues of plagorism or copyright infringement? My site has a lot of unique content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement. Do you know any ways to help protect against content from being stolen? I’d certainly appreciate it.
LikeLike
Appreciating the persistence you put into your site and in depth information you offer. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed information. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.
LikeLike