Automation Proficient
18. Print Star console using Loop
class Stars {
public static void main(String[] args) {
int row, numberOfStars;
for (row = 1; row <= 10; row++) {
for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
[Link]("*");
}
[Link](); // Go to next line
}
}
}
19. While loop Program in java
import [Link];
class WhileLoop {
public static void main(String[] args) {
int n;
Scanner input = new Scanner([Link]);
[Link]("Input an integer");
while ((n = [Link]()) != 0) {
[Link]("You entered " + n);
[Link]("Input an integer");
}
[Link]("Out of loop");
}
Automation Proficient
20. Print Reverse number in java program
import [Link];
class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;
[Link]("Enter the number to reverse");
Scanner in = new Scanner([Link]);
n = [Link]();
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
[Link]("Reverse of entered number is "+reverse);
}
}
21. While loop using break Program in java
import [Link];
class BreakWhileLoop {
public static void main(String[] args) {
int n;
Scanner input = new Scanner([Link]);
while (true) {
[Link]("Input an integer");
n = [Link]();
if (n == 0) {
break;
}
[Link]("You entered " + n);
}
}
}
Automation Proficient
22. While loop using break and continue
Program in java
import [Link];
class BreakContinueWhileLoop {
public static void main(String[] args) {
int n;
Scanner input = new Scanner([Link]);
while (true) {
[Link]("Input an integer");
n = [Link]();
if (n != 0) {
[Link]("You entered " + n);
continue;
}
else {
break;
}
}
}
}
23. Print all alphabet using for loop Program in
java
class Alphabets
{
public static void main(String args[])
{
char ch;
for( ch = 'a' ; ch <= 'z' ; ch++ )
[Link](ch);
}
}
Automation Proficient
24. Enhance loop in java Program
class EnhancedForLoop {
public static void main(String[] args) {
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int t: primes) {
[Link](t);
}
}
}
//For String
class EnhancedForLoop {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};
for (String sample: languages) {
[Link](sample);
}
}
}
25. Print Multiplication table Program in java
import [Link];
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
[Link]("Enter an integer to print it's multiplication
table");
Scanner in = new Scanner([Link]);
n = [Link]();
[Link]("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
[Link](n+"*"+c+" = "+(n*c));
}
}
//For Any Number
import [Link];
class Tables
{
public static void main(String args[])
{
int a, b, c, d;
Automation Proficient
[Link]("Enter range of numbers to print their multiplication
table");
Scanner in = new Scanner([Link]);
a = [Link]();
b = [Link]();
for (c = a; c <= b; c++) {
[Link]("Multiplication table of "+c);
for (d = 1; d <= 10; d++) {
[Link](c+"*"+d+" = "+(c*d));
}
}
}
}
26. Print prime no Program in java
import [Link].*;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of prime numbers you want");
n = [Link]();
if (n >= 1)
{
[Link]("First "+n+" prime numbers are :-");
[Link](2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= [Link](num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
[Link](num);
count++;
}
Automation Proficient
status = 1;
num++;
}
}
}
27. Check no is Armstrong or not in java
Program
import [Link];
class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;
Scanner in = new Scanner([Link]);
[Link]("Input a number to check if it is an Armstrong
number");
n = [Link]();
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is not an Armstrong number.");
}
static int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;