Computer Program Sanjay Mohanan
Computer Program Sanjay Mohanan
import java.util.*;
class ElectricBillCalculator
{
double bill = 0; if
(units <= 100)
{
bill = units * 1.50;
}
else if (units <= 250)
{
bill = 100 * 1.50;
units -= 100;
bill += units * 2.56;
}
else if (units <=300)
{
bill = 100 * 1.50 + 250 * 2.56;
units -= 250;
bill += units * 3.00;
} else
{
bill = 100 * 1.50 + 250 * 2.56 + 300 * 3.00;
units -= 300;
bill += units * 4.00;
}
bill *= 1.30;
}
}
2) The amount obtained by the worker in six different divisions are
inputted through the keyword. The worker gets the division per
the following. Write a program to calculate the division obtained by
the worker.
import java.util.Scanner;
class WorkerDivision
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
}
}
Using nested ternary
v
{
public static void main(String[] args)
if (largest > 0)
{
double square = largest * largest;
double cube = largest * largest * largest;
double sum = square + cube;
System.out.println("Square of the largest number: " + square);
System.out.println("Cube of the largest number: " + cube);
System.out.println("Sum of square and cube: " + sum);
} else
{
System.out.println("The largest number is not greater than
zero.");
}
}
}
String pattern
ii) ABCD
BC
D
CD
D
iii)
ABCDE
ABCDE
ABCDE
ABCDE
System.out.println("Select a pattern:");
System.out.println("1. pattern");
System.out.println("2. pattern");
System.out.println("3. patten");
System.out.println("4. Exit");
System.out.print("Enter your choice: "); int
choice = sc.nextInt();
String b= "@";
switch (choice) {
case 1:
{
String pattern = "BLUEJ";
for (int i = 0; i < pattern.length(); i++)
{ System.out.println(pattern.substring(0, i )+"@");
}
}
break;
case 2:
{String pattern = "ABCDE";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) { System.out.print(pattern.charAt(j));
}
System.out.println();
}
}
break;
case 3:
for (int i = 3; i >= 0; i--) {
for (int j = i; j >= 0; j--) { System.out.print((char) ('D' -
j));
}
System.out.println(); for (int
k = 3; k > i; k--) {
System.out.print(" ");
}
}
break;
default :System.out.println("wrong choice");
}
}
}
String program
1) Accept a sentence find count of the vowels.it count is
greater than 5 print “ bomblastic” sentence else print not
.
import java.util.*;
class VowelCounter
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
=='A'||ch=='E'||ch
=='I'||ch=='O'||ch=='U')
vowelCount++;
}
if (vowelCount > 5)
{
System.out.println("bombastic sentence");
} else {
System.out.println("not bomlastic sentence");
}
}
}
Number pattern
1
21
321
4321
54321
654321
7654321
1
12
123
1234
12345
123456
1234567
import java.util.*;
class NumberPatternPrograms
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
{ case 1 :
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
break;
case 2 :
{ for (int i = 1; i <= rows; i++)
{
System.out.println();
}
break;
}
}
}
}
Sum of series
1) s= a+a/2!+a/3!+a/4!+….n
S=1+(1+2)/a2+(1+2+3)a4+(1+2+3+4)/a6+….n
import java.util.*;
class S6
{
int a=sc.nextInt();
int n=sc.nextInt();
int b=1;
double s=0.0;
int sum=0;
switch (choice)
{case 1:
for(int i=1;i<=n;i++)
{ for(int j=1;j<=i;j++)
{ b =b * j; }
s = s + (a / b); b
=1;
} System.out.print("s="+s);
break;
case 2:
int po = 0;
for (int i = 1 ;i<=n;i++)
{ for(int j=1;j<=i;j++)
{
sum=sum+ j;
}
}
double a1=Math.pow(a,po); s=s+(sum/a1);
po = 2;
break; default:
System.out.println("wrong choice");
}
}
2) 1/1!/3/2!/5/3!+…n
I) x1+x3+x5+….n
import java.util.Scanner; class
SeriesSum {
public static void main(String[] args) { Scanner scanner =
new Scanner(System.in); System.out.println("Menu:");
System.out.println("1. Sum of the series 1/1! + 3/2! + 5/3! + ... + n");
System.out.println(" 2.Sum of the series X1 + X3 + X5
+......n");
System.out.print("Enter your choice: "); int choice =
scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the number of terms (n): "); int n =
scanner.nextInt();
double sum = 0.0; int
factorial = 1;
for (int i = 1; i <= n; i++) { factorial *= i;
sum += (2 * i - 1) / (double) factorial;
}
System.out.println("Sum of the series: " + sum); break;
case 2:System.out.println("Enter the value of X"); int
x=scanner.nextInt();
System.out.println("Enter the number term n1"); int
n1=scanner.nextInt(); double sum2=0; int a=1; for(int
i=1;i<=n1;i++)
{
sum2=sum2+ Math.pow(x,a); a=a+2;
}
}
}
}
while(temp>0)
{
count++;
temp=temp/10;
}
int lastDigit = (int) (square%(Math.pow(10, count)));
if(num == lastDigit)
System.out.println(num+ " is an automorphic number."); else
System.out.println(num+ " is not an automorphic number.");
}
}
Print series
1,9,17,33,49,73,97….................n
1,3,5,7,9,11,13,…............n
import java.util.*; class series
{
{
case 1:
System.out.println("enter the number of terms"); int
n1=sc.nextInt();
int ith_term=0; for(int
i=1;i<=n1;i++)
{
ith_term=i%2==0?2*i*i+1:2*i*i-1;
System.out.print(ith_term+",");
}
System.out.println(); break;
case 2:System.out.println("enter the number of terms"); int
n2=sc.nextInt(); int a=1;
for(int j=1;j<=n2;j++)
{
System.out.print(a+","); a=a+2;
}
}
}
}