Java Programs for beginners

Sample paper for class IX

Java Programs for beginners

Write a program to, compute the railway fare depending on the criteria as given

Distance (in kms)Fare (Rupees per Km)
Below 10Rs. 5
From 10 to 30Rs. 20
From 31 and 50Rs. 30
Above 50Rs. 50
import java.util.*;
class Fare
{   
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the kilometers");
        int km=sc.nextInt();
         int fare=0;
         if(km>=1 && km<10)
             fare=km*5;
         else if(km>=10 && km<=30)
              fare=km*20;
         else if(km>=31 && km<=50)
             fare=km*30;
        else
           fare=km*50;
        System.out.println("The distance in  kilometers");
        System.out.println(km);
       System.out.println("The fare is");
        System.out.println(fare);
}
}

Write a program to take the month number from the user and display the name of the month. Display an error message for wrong input(Use switch case only).

import java.util.*;
class Month
{
    
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the month number");
        int month=sc.nextInt();
        switch(month)
        {
            case 1:
            System.out.println("January");
            break;
            case 2:
            System.out.println("February");
            break;
            case 3:
            System.out.println("March");
            break;
            case 4:
            System.out.println("April");
            break;
            case 5:
            System.out.println("May");
            break;
            case 6:
            System.out.println("June");
            break;
            case 7:
            System.out.println("July");
            break;
            case 8:
            System.out.println("August");
            break;
            case 9:
            System.out.println("September");
            break;
            case 10:
            System.out.println("October");
            break;
            case 11:
            System.out.println("November");
            break;
            case 12:
            System.out.println("December");
            break;
            default:
            System.out.println("Please enter a number in a range 1 to 12");
            break;
        }
        }       
}

Watch java videos

LEAVE A REPLY

Please enter your comment!
Please enter your name here