0% found this document useful (0 votes)
4 views15 pages

Java Loops Example

Uploaded by

Vidya Bhushan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Java Loops Example

Uploaded by

Vidya Bhushan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Loops :

For, While, do..while

*/

class Main {

public static void main(String[] args) {

for(int i = 1; i <= 10; i++){

[Link](i);

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Loops :

For, While, do..while

*/

class Main {

public static void main(String[] args) {

for(int i = 1; i <= 10; i++){

[Link](i+" ");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Loops :

For, While, do..while

*/

class Main {

public static void main(String[] args) {

[Link](11 + 12); // 23

[Link](11 + 11 + 11); // 33

[Link]("ans : " + 11 + 11 + 11);

[Link]("ans : " + (11 + 11 + 11));

[Link](11 + 11 + 11 + " Ans ");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

print table

*/

import [Link];

class Main {

public static void main(String[] args) {

int ip;

Scanner sc = new Scanner([Link]);

[Link]("Enter any Number : ");

ip = [Link]();

for(int i = 1; i <=10; i++){

[Link](ip + " * " + i + " = "+ (ip * i));

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

*****

*****

*****

*****

*****

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int c = 1; c <= 5; c++){

[Link]("*");

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

**

***

****

*****

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int c = 1; c <= r; c++){

[Link]("*");

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

12

123

1234

12345

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int c = 1; c <= r; c++){

[Link](c);

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

22

333

4444

55555

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int c = 1; c <= r; c++){

[Link](r);

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

12

123

1234

12345

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int s = 4; s >= r; s--){

[Link](" ");

for(int c = 1; c <= r; c++){

[Link](r);

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

12

123

1234

12345

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int s = 4; s >= r; s--){

[Link](" ");

for(int c = 1; c <= r; c++){

[Link](" "+c);

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Nested Loops

123

12345

1234567

123456789

*/

import [Link];

class Main {

public static void main(String[] args) {

// outer

for(int r = 1; r <= 5; r++){

// inner

for(int c = 1; c < r*2; c++){

[Link](c);

[Link]("");

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

while loop

*/

import [Link];

class Main {

public static void main(String[] args) {

int i = 1;

while(i <= 10){

[Link](" "+i);

i++;

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

count how many digits in entered number

Ex. 54856 = 5

*/

import [Link];

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int ip, ans = 0;

[Link]("Enter any number : ");

ip = [Link]();

while(ip > 0){

ans++;

ip = ip / 10;

[Link](ans);

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

count sum of digits in entered number

Ex. 54856 = 28

*/

import [Link];

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int ip, ans = 0;

[Link]("Enter any number : ");

ip = [Link]();

while(ip > 0){

ans += ip % 10;

ip = ip / 10;

[Link](ans);

}
// Online Java Compiler

// Use this editor to write, compile and run your Java code online

/*

Reverse the enetered number

Ex. 12587 = 78521

*/

import [Link];

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int ip, ans = 0;

[Link]("Enter any number : ");

ip = [Link]();

while(ip > 0){

ans *= 10;

ans += ip % 10;

ip = ip / 10;

[Link](ans);

You might also like