Loops in Java (Exercise 1)

preview_player
Показать описание
Java Programming: Programming Question on Loops in Java Programming
Topics Discussed:
1. Printing the even numbers between 1 to 100 with the help of loops.
2. Printing the odd numbers between 1 to 100 with the help of loops.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #Loops
Рекомендации по теме
Комментарии
Автор

Understandable ✅
Clean ✅
Simple code ✅
Thank you 👍

hubbylife
Автор

Thanks for share this information with all the community, I hope in the future to have the opportunity to meet you, regards from Perú.

Renso
Автор

my fellow first year comput sci students!

kylemaritz
Автор

public class Main {
public static void main(String[] args)
{
for (int i=100; i>=1; i--)
{
if (i%2==0)
System.out.print(i+"\t");
}
}
}

AI-krmj
Автор

I did it using a while loop actually! this is my code for ascending

package p1;

import java.util.Scanner;

public class P1 {

public static void main(String[] args) {

int i = 1;
while(i<=100){
i++;
if(i%2==0){
System.out.print(i+" ");
}
}
}
}

And decreasing:
package p1;

import java.util.Scanner;

public class P1 {

public static void main(String[] args) {

int i = 100;
while(i>=1){
i--;
if(i%2==0){
System.out.print(i+" ");
}
}
}
}

valentinaramirez
Автор

Try this easiest way :)
for(int i = 2; i <= 100; i=i+2){
System.out.print( i + " " );
}


DECREASING
for(int i = 100; i >= 2; i=i-2){
System.out.print( i + " " );
}

kennethdagacay
Автор

public class Main {
public static void main(String[] args){


for ( int i = 100 ; i >= 1 ; i--)
if ( i % 2 == 0 )
System.out.print(i+ " ");

for ( int i1 = 1 ; i1 <= 100 ; i1++)
if ( i1 % 2 == 0 )
System.out.print(i1 + " ");


}
}

Bibingka
Автор

decreasing order

for (int i = 100 ; i>=1 ; i--) {
if (i%2==0)
System.out.print(i+" ");

}

sidharths
Автор

Increasing :
class Even_1_100
{
public static void main(String args[])
{
for(int i =1; i<=100;i++){
if(i%2 ==0){
// continue;
System.out.print(i+" ");
} } } }


Decreasing;
class Even_1_100
{
public static void main(String args[])
{
for(int i =100; i>=1;i--){
if(i%2 ==0){
// continue;
System.out.print(i+" ");
}}} }

IMRANsLU
Автор

Solution :-
// Ascending

System.out.println("Ascending :-\n");

for(int i = 1; i<=100; i++){
if(i % 2 != 0){
continue;
}
System.out.println(i);
}
System.out.println();
:-\n");

// Descending

for(int i = 100; i>=1; i--){
if(i % 2 != 0){
continue;
}
System.out.println(i);
}

siddhantkumar
Автор

Thank you, I was wondering why can’t you just use i= i+ 2 in the loop header header.

ishaankapuria
Автор

Decreasing can also be done without changing for loop condition.Change printing by (102-i)👍

dennisps
Автор

for (int i = 1 ; i<=100 ; i++) {
if (i%2==0)
System.out.print(i+" ");
}

sidharths
Автор

I tried to go a step further by asking user input:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Number: ");
int order = input.nextInt();

for (int a = order ; a >= 1; a--){
if (a % 2 ==0)
System.out.println(a);
}
}
}

arnodorian
Автор

public class OddEven {

public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
System.out.println(i);

}

for (int i = 100; i >= 2; i--) {
System.out.println(i);
}

}

}

sathishksuresh
Автор

we can also print using continue keyword

charanmoka
Автор

Your voice seems to be like sleepy bro take some rest by the way amazing video!
Thx ♥

Porcupinebygoldiee
Автор

1) increasing order

//Using While Loop
int a = 0;
while(a <= 100) {
a++;
if(a % 2 == 0) {
System.out.print(a + " ");
}
}
System.out.println();

//Using For Loop
for(int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
System.out.print(i + " ");
}

continue;
}

System.out.println();

//Using do_while Loop
int b = 0;
do {
b++;
if(b % 2 == 0) {
System.out.print(b + " ");
}
} while(b <= 100);

System.out.println();

2) decreasing order

class Check {
public static void main(String[] args) {

//Using While Loop
int a = 101;
while(a > 1) {
a--;
if(a % 2 == 0) {
System.out.print(a + " ");
}
}
System.out.println();

//Using For Loop
for(int i = 100; i > 0; i--) {
if(i % 2 == 0) {
System.out.print(i + " ");
}

continue;
}

System.out.println();

//Using do_while Loop
int b = 101;
do {
b--;
if(b % 2 == 0) {
System.out.print(b + " ");
}
} while(b > 1);

System.out.println();
}
}

suswithcherry
Автор

Sir can u post a all java related videos within a week pls sir

balasuryapitchaimani
Автор

public class Main {

public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
} else {
System.out.print("");
}
}

}
}

mustafaismail