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
You are on page 1/ 15

// 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++){

System.out.println(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++){

System.out.print(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) {

System.out.println(11 + 12); // 23

System.out.println(11 + 11 + 11); // 33

System.out.println("ans : " + 11 + 11 + 11);

System.out.println("ans : " + (11 + 11 + 11));

System.out.println(11 + 11 + 11 + " Ans ");

}
// Online Java Compiler

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

/*

print table

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

int ip;

Scanner sc = new Scanner(System.in);

System.out.println("Enter any Number : ");

ip = sc.nextInt();

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

System.out.println(ip + " * " + i + " = "+ (ip * i));

}
// Online Java Compiler

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

/*

Nested Loops

*****

*****

*****

*****

*****

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print("*");

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

**

***

****

*****

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print("*");

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

12

123

1234

12345

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print(c);

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

22

333

4444

55555

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print(r);

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

12

123

1234

12345

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print(" ");

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

System.out.print(r);

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

12

123

1234

12345

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// outer

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

// inner

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

System.out.print(" ");

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

System.out.print(" "+c);

System.out.println("");

}
// Online Java Compiler

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

/*

Nested Loops

123

12345

1234567

123456789

*/

import java.util.Scanner;

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++){

System.out.print(c);

System.out.println("");

}
// Online Java Compiler

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

/*

while loop

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

int i = 1;

while(i <= 10){

System.out.print(" "+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 java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int ip, ans = 0;

System.out.println("Enter any number : ");

ip = sc.nextInt();

while(ip > 0){

ans++;

ip = ip / 10;

System.out.println(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 java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int ip, ans = 0;

System.out.println("Enter any number : ");

ip = sc.nextInt();

while(ip > 0){

ans += ip % 10;

ip = ip / 10;

System.out.println(ans);

}
// Online Java Compiler

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

/*

Reverse the enetered number

Ex. 12587 = 78521

*/

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int ip, ans = 0;

System.out.println("Enter any number : ");

ip = sc.nextInt();

while(ip > 0){

ans *= 10;

ans += ip % 10;

ip = ip / 10;

System.out.println(ans);

You might also like