Class 8 Computer

Mid-Term Syllabus

Final-Term Syllabus

Sample Java Programs for Class 8 Students

  1. Write a Java program to input two integers and swap them using a temporary variable. Display the new values of the variables.
    import java.util.Scanner;
    class Swap{
    public static void main(String args[]){
    System.out.print("First integer: ");
    int a = Integer.parseInt(in.nextLine());
    System.out.print("Second integer: ");
    int b = Integer.parseInt(in.nextLine());
    int temp = a;
    a = b;
    b = temp;
    System.out.println("First number = " + a);
    System.out.println("First number = " + a);
    }
    }
  2. Write a Java program to input the radius of a circle and find and display its area and circumference.
    import java.util.Scanner;
    class Circle{
    public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.print("Radius of the circle: ");
    double r = Double.parseDouble(in.nextLine());
    double a = Math.PI * r * r;
    double c = 2 * Math.PI * r;
    System.out.println("Area: " + a);
    System.out.println("Circumference: " + c);
    }
    }

3. Write a Java program to input the year and check and display whether it is a leap year or not.

import java.util.Scanner;
class Leap{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Year: ");
        int y = in.nextInt();
        if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
            System.out.println(y + " is a leap year!");
        else
            System.out.println(y + " is not a leap year.");
    }
}