6. Write a program that prints all even numbers from 1 to 50 inclusive using the for loop, while loop, and do…while loop. The class name is EvenNum.
Even numbers from 1 to 50...
----------using for loop----------
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
----------using while loop----------
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
----------using do...while loop----------
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
import java.util.Scanner;
public class EvenNum {
public static void main(String[] args) {
Scanner amante=new Scanner(System.in);
int a=1,b=1;
System.out.println("----------using for loop----------");
for(int i=1;i<50;i++){
i=i+1;
System.out.print(i+ " ");
}
System.out.print("\n");
System.out.println("----------using while loop----------");
while (a<50){
a++;
System.out.print(a+ " ");
a=a+1;
}
System.out.print("\n");
System.out.println("----------using do...while loop----------");
do{b++;
System.out.print(b + " ");
b=b+1;
}
while (b<50);
}
}
Walang komento:
Mag-post ng isang Komento