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
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.