basic conditions and 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.
Program:

import java.util.Scanner;
public class tokens{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number of passengers:");
        int n=sc.nextInt();
        for(int i=1;i<=n;i+=2){
            System.out.println("Only print even numbers:");
        }
    }
}

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.


Program:

import java.util.Scanner;
public class shopbill{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the bill amount:");
int billamount=sc.nextInt();
float discount=0;
if(billamount<1000){
discount=billamount*10/100;
}
else{
discount=billamount*20/100;
}
float price=billamount-discount;
System.out.println("Final price:" +price);
}
}


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".
Program:

import java.util.Scanner;
class records{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter attendece record:");
        String record=sc.nextLine();
        int present=0,absent=0;
        for(int i=0;i<record.length();i++){
            if(record.charAt(i)=='P'){
                present++;
            }
            else if(record.charAt(i)=='A'){
                absent++;
            }
        }
        System.out.println("Present=" +present);
        System.out.println("Absent=" +absent);
}
}

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.
Program:

import java.util.Scanner;
public class watertank{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the daily usage of water:");
int dailyusage=sc.nextInt();
int days=0;
int capacity=100;
days=capacity/dailyusage;
System.out.println("The water will use for the days:" + days);
int remainder=capacity%dailyusage;
System.out.println("left over after the water:" +remainder);
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.

Program:

import java.util.Scanner;
public class multiplication5{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int n=sc.nextInt();
for(int i=1;i<=10;i++){
int result=n*i;
System.out.println(n +"X"+i+"="+result);
}
sc.close();
}
}

OUTPUT:




Popular posts from this blog

core java challenges

for and while challenges

html task