Level 1 Tasks-(Java)
🟢
Level 1 – (Basics: Conditions & Loops)
1. Bus Ticket Counter
Story:
A conductor issues tokens to passengers. Each passenger must get a number in
sequence.
Why: Helps students learn loops.
Logic:
- Input: number of passengers.
- Use for loop to print numbers 1 to n.
- Extension: use if inside loop to print only even tokens.
Code :
import java.util.Scanner;
public class BusTokens
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of passengers: ");
int n = sc.nextInt();
System.out.println("All Tokens:");
for (int i = 1; i <= n; i++)
{
System.out.print(i + " ");
}
System.out.println("\nEven Tokens:");
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
System.out.print(i + " ");
}
}
}
}
Output :
2. Shop Discount Calculator
Story:
A shop applies discounts depending on bill amount.
Why: Introduces if-else conditions.
Logic:
- Input: bill amount.
- If bill < 1000 → apply 10%.
- Else → apply 20%.
- Final price = bill - discount.
Code :
import java.util.Scanner;
class bill {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Bill value");
int bill = sc.nextInt();
double discount;
if(bill < 1000) discount = bill*0.10;
else discount = bill*0.20;
double finalPrice = bill - discount;
System.out.println("Bill=" + bill);
System.out.println("Discount=" + discount);
System.out.println("Final Price=" + finalPrice);
}
}
Output :
3. School Attendance
Story:
Class teacher records attendance as "P"
for present, "A" for absent.
Why: Helps in counting characters in a string.
Logic:
- Input: string like "PAPAPPPAA".
- Loop through string → count P and A.
- Print "Present
= X, Absent = Y".
Code :
import java.util.Scanner;
class attendance
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your attendance (Ex:Present=P,Absent=A)\t:\t");
String s = sc.next();
int p=0,a=0;
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if(c=='P') p++;
else if(c=='A') a++;
}
System.out.println("Present=" + p);
System.out.println("Absent=" + a);
}
}
Output :
4. Village Water Tank
Story:
A water tank has limited supply. Villagers want to know how long it lasts.
Why: Good for division and loop thinking.
Logic:
- Input: capacity (100 liters), daily usage.
- Days = capacity
/ dailyUsage.
- Remainder = leftover water.
Code :
import java.util.Scanner;
public class WaterTank
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Tank capacity (liters): ");
int capacity = sc.nextInt();
System.out.print("Daily usage (liters/day): ");
int usage = sc.nextInt();
if (usage <= 0)
{
System.out.println("Daily usage must be > 0");
} else {
int days = capacity / usage;
int remainder = capacity % usage;
System.out.println("Tank will last for " + days + " full days.");
System.out.println("Leftover water = " + remainder + " liters.");
}
sc.close();
}
}
Output :
5. Multiplication Game
Story:
A student practices multiplication tables.
Why: Introduces simple loops.
Logic:
- Input: number (say 7).
- Loop from 1 to 10 → print 7 x i = result.
Code :
import java.util.Scanner;
public class MultiplicationTable
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int n = sc.nextInt();
for (int i = 1; i <= 16; i++)
{
System.out.println(i + " x " + n + " = " + (n * i));
}
sc.close();
}
}
Output :
Comments
Post a Comment