Java Class8 Loops Conditions Patterns
Java Class8 Loops Conditions Patterns
Contents
A. If-Else / Conditional Statements (Problems 1-5)
B. While Loop (Problems 6-9)
C. For Loop (Problems 10-13)
D. Pattern Printing (Problems 14-18)
E. Number Problems (Problems 19-24)
A. If-Else / Conditional Statements
import [Link];
class LargestOfThree
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers: ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
import [Link];
class GradeCalculator
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter marks: ");
int marks = [Link]();
import [Link];
class LeapYear
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
import [Link];
class VoteEligibility
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter age: ");
int age = [Link]();
import [Link];
class SimpleCalculator
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double a = [Link]();
[Link]("Enter second number: ");
double b = [Link]();
[Link]("Enter operator (+, -, *, /): ");
char op = [Link]().charAt(0);
double result = 0;
if (op == '+')
result = a + b;
else if (op == '-')
result = a - b;
else if (op == '*')
result = a * b;
else if (op == '/')
result = a / b;
else
[Link]("Invalid operator");
import [Link];
class SumUsingWhile
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int i = 1, sum = 0;
while (i <= n)
{
sum = sum + i;
i++;
}
[Link]("Sum = " + sum);
}
}
import [Link];
class CountDigits
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int count = 0;
while (num != 0)
{
num = num / 10;
count++;
}
[Link]("Number of digits = " + count);
}
}
import [Link];
class MultiplicationTable
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int i = 1;
import [Link];
class ReverseNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int reverse = 0;
while (num != 0)
{
int digit = num % 10;
reverse = (reverse * 10) + digit;
num = num / 10;
}
[Link]("Reverse = " + reverse);
}
}
C. For Loop
import [Link];
class SumUsingFor
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int sum = 0;
import [Link];
class Factorial
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
long fact = 1;
import [Link];
class SumOfEvens
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int sum = 0;
class NestedTables
{
public static void main(String args[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
[Link](i + "x" + j + "=" + (i * j) + "\t");
}
[Link]();
}
}
}
D. Pattern Printing
Pattern problems are solved using nested for loops — an outer loop for rows and an inner loop for columns.
import [Link];
class SquarePattern
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
import [Link];
class TrianglePattern
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
import [Link];
class InvertedTriangle
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
import [Link];
class NumberPyramid
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](i);
}
[Link]();
}
}
}
import [Link];
class FloydsTriangle
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int n = [Link]();
int num = 1;
import [Link];
class PrimeCheck
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true;
if (num <= 1)
isPrime = false;
if (isPrime)
[Link](num + " is a Prime Number");
else
[Link](num + " is not a Prime Number");
}
}
import [Link];
class PalindromeCheck
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int original = num;
int reverse = 0;
while (num != 0)
{
int digit = num % 10;
reverse = (reverse * 10) + digit;
num = num / 10;
}
if (original == reverse)
[Link](original + " is a Palindrome");
else
[Link](original + " is not a Palindrome");
}
}
import [Link];
class SumOfDigits
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
while (num != 0)
{
int digit = num % 10;
sum = sum + digit;
num = num / 10;
}
[Link]("Sum of digits = " + sum);
}
}
import [Link];
class ArmstrongCheck
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a 3-digit number: ");
int num = [Link]();
int original = num;
int sum = 0;
while (num != 0)
{
int digit = num % 10;
sum = sum + (digit * digit * digit);
num = num / 10;
}
if (sum == original)
[Link](original + " is an Armstrong Number");
else
[Link](original + " is not an Armstrong Number");
}
}
import [Link];
class FibonacciSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int first = 0, second = 1;
import [Link];
class GCDCalculator
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int gcd = 1;
Tip for students: for pattern problems, always identify the outer loop (rows) and inner loop (columns/characters per row)
separately before coding.