0% found this document useful (0 votes)
3 views16 pages

Java Class8 Loops Conditions Patterns

JAVA Class 8

Uploaded by

madhubelakoba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Java Class8 Loops Conditions Patterns

JAVA Class 8

Uploaded by

madhubelakoba
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA: CONDITIONS, LOOPS & PATTERNS

Problems with Solutions · Class 8 · ICSE Board


This set covers if-else conditions, while loops, for loops, pattern printing, and common number-based problems. Each
problem includes a statement, a sample run, and a complete Java solution. Encourage students to try each problem first, then
compare with the solution.

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

Problem 1: Largest of Three Numbers


Accept three numbers and find the largest among them using if-else.
Sample: Input a=12, b=45, c=7 → Output Largest number = 45
Solution:

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]();

if (a >= b && a >= c)


[Link]("Largest number = " + a);
else if (b >= a && b >= c)
[Link]("Largest number = " + b);
else
[Link]("Largest number = " + c);
}
}

Problem 2: Grade Calculator


Accept marks (out of 100) and print the grade: 90+ 'A', 75-89 'B', 60-74 'C', 40-59 'D', below 40 'Fail'.
Sample: Input marks = 82 → Output Grade = B
Solution:

import [Link];
class GradeCalculator
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter marks: ");
int marks = [Link]();

if (marks >= 90)


[Link]("Grade = A");
else if (marks >= 75)
[Link]("Grade = B");
else if (marks >= 60)
[Link]("Grade = C");
else if (marks >= 40)
[Link]("Grade = D");
else
[Link]("Grade = Fail");
}
}

Problem 3: Leap Year Check


Accept a year and check whether it is a leap year. (A year is a leap year if it is divisible by 4, but centuries must also be
divisible by 400.)
Sample: Input year = 2024 → Output 2024 is a Leap Year
Solution:

import [Link];
class LeapYear
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


[Link](year + " is a Leap Year");
else
[Link](year + " is not a Leap Year");
}
}

Problem 4: Eligibility to Vote


Accept a person's age and check if they are eligible to vote (age 18 or above).
Sample: Input age = 16 → Output Not eligible to vote
Solution:

import [Link];
class VoteEligibility
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter age: ");
int age = [Link]();

if (age >= 18)


[Link]("Eligible to vote");
else
[Link]("Not eligible to vote");
}
}

Problem 5: Simple Calculator (Menu-based)


Accept two numbers and an operator (+, -, *, /). Perform the operation using if-else and display the result.
Sample: Input a=10, b=5, op='*' → Output Result = 50.0
Solution:

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");

[Link]("Result = " + result);


}
}
B. While Loop

Problem 6: Sum of First N Natural Numbers (while loop)


Accept a number N and find the sum of all natural numbers from 1 to N using a while loop.
Sample: Input N = 5 → Output Sum = 15
Solution:

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);
}
}

Problem 7: Count Digits in a Number (while loop)


Accept a number and count how many digits it has using a while loop.
Sample: Input num = 24567 → Output Number of digits = 5
Solution:

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);
}
}

Problem 8: Table of a Number (while loop)


Accept a number and print its multiplication table up to 10 using a while loop.
Sample: Input num = 4 → Output 4 x 1 = 4 ... 4 x 10 = 40
Solution:

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;

while (i <= 10)


{
[Link](num + " x " + i + " = " + (num * i));
i++;
}
}
}

Problem 9: Reverse of a Number (while loop)


Accept a number and print its reverse using a while loop.
Sample: Input num = 3456 → Output Reverse = 6543
Solution:

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

Problem 10: Sum of First N Natural Numbers (for loop)


Accept a number N and find the sum of natural numbers from 1 to N using a for loop.
Sample: Input N = 10 → Output Sum = 55
Solution:

import [Link];
class SumUsingFor
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int sum = 0;

for (int i = 1; i <= n; i++)


{
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}

Problem 11: Factorial of a Number (for loop)


Accept a number and find its factorial using a for loop.
Sample: Input num = 5 → Output Factorial = 120
Solution:

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;

for (int i = 1; i <= num; i++)


{
fact = fact * i;
}
[Link]("Factorial = " + fact);
}
}

Problem 12: Sum of Even Numbers Between 1 and N (for loop)


Accept a number N and find the sum of all even numbers between 1 and N (inclusive) using a for loop.
Sample: Input N = 10 → Output Sum of even numbers = 30
Solution:

import [Link];
class SumOfEvens
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int sum = 0;

for (int i = 2; i <= n; i = i + 2)


{
sum = sum + i;
}
[Link]("Sum of even numbers = " + sum);
}
}

Problem 13: Print Multiplication Tables from 1 to 5 (nested for loop)


Print the multiplication tables of numbers 1 to 5, each up to 5, using a nested for loop.
Sample: → Output 1x1=1 1x2=2 ... 5x5=25
Solution:

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.

Problem 14: Square Pattern of Stars


Accept a number N and print a square pattern of N rows and N columns using '*'.
Sample: Input N = 4 → Output **** / **** / **** / ****
Solution:

import [Link];
class SquarePattern
{
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 <= n; j++)
{
[Link]("*");
}
[Link]();
}
}
}

Problem 15: Right-Angled Triangle of Stars


Accept a number N and print a right-angled triangle pattern with N rows using '*'.
Sample: Input N = 5 → Output * / ** / *** / **** / *****
Solution:

import [Link];
class TrianglePattern
{
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]("*");
}
[Link]();
}
}
}

Problem 16: Inverted Triangle of Stars


Accept a number N and print an inverted right-angled triangle pattern using '*'.
Sample: Input N = 5 → Output ***** / **** / *** / ** / *
Solution:

import [Link];
class InvertedTriangle
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();

for (int i = n; i >= 1; i--)


{
for (int j = 1; j <= i; j++)
{
[Link]("*");
}
[Link]();
}
}
}

Problem 17: Number Pyramid Pattern


Accept a number N and print a pyramid pattern where each row shows the row number repeated.
Sample: Input N = 4 → Output 1 / 22 / 333 / 4444
Solution:

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]();
}
}
}

Problem 18: Floyd's Triangle


Accept a number N (number of rows) and print Floyd's Triangle — consecutive natural numbers arranged in a triangle.
Sample: Input N = 4 → Output 1 / 2 3 / 4 5 6 / 7 8 9 10
Solution:

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;

for (int i = 1; i <= n; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](num + " ");
num++;
}
[Link]();
}
}
}
E. Number Problems

Problem 19: Check Prime Number


Accept a number and check whether it is a prime number.
Sample: Input num = 17 → Output 17 is a Prime Number
Solution:

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;

for (int i = 2; i <= num / 2; i++)


{
if (num % i == 0)
{
isPrime = false;
break;
}
}

if (isPrime)
[Link](num + " is a Prime Number");
else
[Link](num + " is not a Prime Number");
}
}

Problem 20: Check Palindrome Number


Accept a number and check whether it is a palindrome (reads the same forwards and backwards).
Sample: Input num = 1221 → Output 1221 is a Palindrome
Solution:

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");
}
}

Problem 21: Sum of Digits of a Number


Accept a number and find the sum of its digits.
Sample: Input num = 4523 → Output Sum of digits = 14
Solution:

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);
}
}

Problem 22: Check Armstrong Number


Accept a 3-digit number and check whether it is an Armstrong number (sum of the cubes of its digits equals the number
itself).
Sample: Input num = 153 → Output 153 is an Armstrong Number
Solution:

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");
}
}

Problem 23: Print Fibonacci Series


Accept a number N and print the first N terms of the Fibonacci series (each number is the sum of the previous two).
Sample: Input N = 7 → Output 0 1 1 2 3 5 8
Solution:

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;

for (int i = 1; i <= n; i++)


{
[Link](first + " ");
int next = first + second;
first = second;
second = next;
}
}
}

Problem 24: Greatest Common Divisor (GCD) of Two Numbers


Accept two numbers and find their GCD (greatest common divisor / HCF) using a loop.
Sample: Input a=36, b=24 → Output GCD = 12
Solution:

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;

for (int i = 1; i <= a && i <= b; i++)


{
if (a % i == 0 && b % i == 0)
{
gcd = i;
}
}
[Link]("GCD = " + gcd);
}
}

Tip for students: for pattern problems, always identify the outer loop (rows) and inner loop (columns/characters per row)
separately before coding.

You might also like