Important Programs
Important Programs
Page 1 of 7
2. An Electricity Board charges for electricity per month from their consumers
according to the units consumed. The tariff is given below:
Units Consumed Charges
Up to 200 units ₹3.80/unit
More than 200 units and up to 300 units ₹4.40/unit
More than 300 units and up to 400 units ₹5.10/unit
More than 400 units ₹5.80/unit
Write a program to calculate the electricity bill taking consumer's name and units consumed
as inputs. Display the output.
import java.util.Scanner;
import java.util.Scanner;
public class StudentMarks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int studentCount = in.nextInt();
double totalMarks = 0.0;
Page 2 of 7
String name = in.nextLine();
System.out.print("Marks in English: ");
int engMarks = in.nextInt();
System.out.print("Marks in Science: ");
int sciMarks = in.nextInt();
System.out.print("Marks in Maths: ");
int mathsMarks = in.nextInt();
double avgMarks = (engMarks + sciMarks + mathsMarks) / 3.0;
totalMarks += avgMarks;
System.out.println("Average marks of " + name + " = " + avgMarks);
}
}
}
5. Write a program to input a number and count the number of digits. The program
further checks whether the number contains odd number of digits or even number of
digits.
Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.
import java.util.Scanner;
Page 3 of 7
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
int dc = 0;
while (n != 0) {
dc++;
n /= 10;
}
System.out.println("Number of digits = " + dc);
if (dc % 2 == 0)
System.out.println("The number contains even number of digits");
else
System.out.println("The number contains odd number of digits");
}
}
Write a program to input a number. Check and display whether it is a Niven number or not.
(A number is said to be Niven which is divisible by the sum of its digits).
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
import java.util.Scanner;
public class NivenNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int orgNum = num;
int digitSum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number");
else
System.out.println(orgNum + " is not a Niven number");
}
}
6. Write a program to generate a triangle or an inverted triangle based upon user's
choice of triangle to be displayed.
Example 1: Example 2:
Input: Type 1 for a triangle Input: Type 2 for an inverted triangle
Enter your choice: 1 Enter your choice: 2
Sample Output: Sample Output:
1 55555
22 4444
333 333
4444 22
55555 1
Page 4 of 7
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
7. Write a program to accept a number from the user and check whether it is a
Palindrome number or not. A number is a Palindrome which when reads in reverse
order is same as in the right order.
Sample Input: 242
Sample Output: A Palindrome number
Sample Input: 467
Sample Output: Not a Palindrome number
import java.util.Scanner;
public class PalindromeNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = in.nextInt();
int copyNum = num;
int revNum = 0;
Page 5 of 7
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
System.out.println("A Palindrome number");
else
System.out.println("Not a Palindrome number");
}
}
8. Using a switch case statement, write a menu driven program to convert a given
temperature from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an
appropriate message should be displayed.
Hint: c = 5/9*(f-32) and f=1.8*c+32
import java.util.Scanner;
public class Temperature
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to convert from Fahrenheit to Celsius");
System.out.println("Type 2 to convert from Celsius to Fahrenheit");
int choice = in.nextInt();
double ft = 0.0, ct = 0.0;
switch (choice) {
case 1:
System.out.print("Enter temperature in Fahrenheit: ");
ft = in.nextDouble();
ct = 5 / 9.0 * (ft - 32);
System.out.println("Temperature in Celsius: " + ct);
break;
case 2:
System.out.print("Enter temperature in Celsius: ");
ct = in.nextDouble();
ft = 1.8 * ct + 32;
System.out.println("Temperature in Fahrenheit: " + ft);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
9. Consider a String:
THE COLD WATER AND THE HOT WATER GLASSES ARE KEPT ON THE
TABLE
Write a program by using scanner class to enter the string and display the new string
after removing repeating token 'THE'. The new string is:
COLD WATER AND HOT WATER GLASSES ARE KEPT ON TABLE
import java.util.Scanner;
public class RemoveThe
{
Page 6 of 7
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence ending with a space & terminating with (.):");
while(in.hasNext()) {
String word = in.next();
if (word.equals("."))
break;
else if (!word.toUpperCase().equals("THE"))
System.out.print(word + " ");
}
}
}
10. Consider the following statement:
"26 January is celebrated as the Republic Day of India"
Write a program (using scanner class) to change 26 to 15; January to August; Republic to
Independence and finally print as:
"15 August is celebrated as the Independence Day of India"
import java.util.Scanner;
public class WordReplace
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence ending with a space & terminating with (.):");
while (true) {
if (in.hasNextInt()) {
int n = in.nextInt();
if (n == 26)
System.out.print("15 ");
else
System.out.print(n + " ");
}
String word = in.next();
if (word.equals("."))
break;
else if (word.equals("January"))
System.out.print("August ");
else if (word.equals("Republic"))
System.out.print("Independence ");
else
System.out.print(word + " ");
}
}
}
Page 7 of 7