0% found this document useful (0 votes)
37 views

Important Programs

The document contains examples of Java programs to calculate the volume of different solids, calculate electricity bills based on tariff slabs, calculate average marks of students, display 'Buzz Numbers' between ranges, count digits of a number and check for odd/even count, check if a number is a Niven number, generate triangles or inverted triangles based on user input, and check if a number is a palindrome.

Uploaded by

Divyashree R H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Important Programs

The document contains examples of Java programs to calculate the volume of different solids, calculate electricity bills based on tariff slabs, calculate average marks of students, display 'Buzz Numbers' between ranges, count digits of a number and check for odd/even count, check if a number is a Niven number, generate triangles or inverted triangles based on user input, and check if a number is a palindrome.

Uploaded by

Divyashree R H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. The volume of solids, viz.

cuboid, cylinder and cone can be calculated by the


formula:
Volume of a cuboid (v = l*b*h)
Volume of a cylinder (v = π*r 2*h)
Volume of a cone (v = (1/3)*π*r 2*h)
Using a switch case statement, write a program to find the volume of different solids
by taking suitable variables and data types.
import java.util.Scanner;
public class MenuVolume
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Volume of cuboid");
System.out.println("2. Volume of cylinder");
System.out.println("3. Volume of cone");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch(choice) {
case 1:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
case 2:
System.out.print("Enter radius of cylinder: ");
double rCylinder = in.nextDouble();
System.out.print("Enter height of cylinder: ");
double hCylinder = in.nextDouble();
double vCylinder = (22 / 7.0) * Math.pow(rCylinder, 2) * hCylinder;
System.out.println("Volume of cylinder = " + vCylinder);
break;
case 3:
System.out.print("Enter radius of cone: ");
double rCone = in.nextDouble();
System.out.print("Enter height of cone: ");
double hCone = in.nextDouble();
double vCone = (1 / 3.0) * (22 / 7.0) * Math.pow(rCone, 2) * hCone;
System.out.println("Volume of cone = " + vCone);
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}

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;

public class ElectricBill


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Consumer Name: ");
String name = in.nextLine();
System.out.print("Enter Unit's Consumed: ");
int units = in.nextInt();
double amt = 0.0;
if (units <= 200)
amt = units * 3.8;
else if (units <= 300)
amt = (200 * 3.8) + ((units - 200) * 4.4);
else if (units <= 400)
amt = (200 * 3.8) + (100 * 4.4) + ((units - 300) * 5.1);
else
amt = (200 * 3.8) + (100 * 4.4) + (100 * 5.1) + ((units - 400) * 5.8);

System.out.println("Consumer Name: " + name);


System.out.println("Units Consumed: " + units);
System.out.println("Bill Amount: " + amt);
}
}
3. In an entrance examination, students have answered English, Maths and Science
papers. Write a program to calculate and display average marks obtained by all the
students. Take number of students appeared and marks obtained in all three subjects
by every student along with the name as inputs.

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;

for (int i = 1; i <= studentCount; i++) {


System.out.println("Enter details of student " + i);
System.out.print("Name: ");
in.nextLine();

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);
}

double classAvg = totalMarks / studentCount;


System.out.println("Class Average = " + classAvg);
}
}
4. Write a program to display all the 'Buzz Numbers' between p and q (where p<q). A
'Buzz Number' is the number which ends with 7 or is divisible by 7.
import java.util.Scanner;
public class BuzzNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter p: ");
int p = in.nextInt();
System.out.print("Enter q: ");
int q = in.nextInt();
if (p < q) {
System.out.println("Buzz Numbers between "
+ p + " and " + q);
for (int i = p; i <= q; i++) {
if (i % 10 == 7 || i % 7 == 0)
System.out.println(i);
}
}
else {
System.out.println("Invalid Inputs!!!");
System.out.println("p should be less than q");
}

}
}
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;

public class DigitCount


{

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;

public class Pattern


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");

System.out.print("Enter your choice: ");


int ch = in.nextInt();

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

You might also like