Java Programs for beginners
Write a program to, compute the railway fare depending on the criteria as given
Java Programs for beginners
Sample paper for class IX
Write a program to, compute the railway fare depending on the criteria as given
<tbody>
Distance (in kms)
Fare (Rupees per Km)
Below 10
Rs. 5
From 10 to 30
Rs. 20
From 31 and 50
Rs. 30
Above 50
Rs. 50
</tbody>
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
Related Posts
Continue learning with these related articles

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.

Math Library Methods in Java for ICSE Class 9 (with Outputs)
ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.