0% found this document useful (0 votes)
21 views25 pages

Computer Program Sanjay Mohanan

Uploaded by

ancheloarun87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views25 pages

Computer Program Sanjay Mohanan

Uploaded by

ancheloarun87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1) Write a java program to input basic electricity bill

unit charge and calculate total electricity bill


according to the given condition.

 For first 100 units Rs 1.50/unit


 For next 250 unit Rs 2.56/unit
 For next 300 unit Rs 3.00/unit
 For next above 300 Rs 4.00/unit
 An additional 30% is added to the bill.

import java.util.*;
class ElectricBillCalculator
{

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.print(System.in);

System.out.print("Enter the electric unit charge "); int units =


sc.nextInt();

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;

System.out.println("Total electric bill: Rs. " + bill);

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

amount above or equal to rupees 10,000 = watchman


amount above or equal to rupees 20,000 = staff
amount between or equal to 30,000 = head
amount more than 50,000 = company owers

import java.util.Scanner;

class WorkerDivision
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the amount obtained by the worker:");


int amount = scanner.nextInt();
if (amount >= 50000) {
System.out.println("Worker is a company owner.");
} else if (amount >= 30000) {
System.out.println("Worker is a head.");
} else if (amount >= 20000) {
System.out.println("Worker is staff.");
} else if (amount >= 10000) {
System.out.println("Worker is a watchman.");
} else {
System.out.println("Worker's division is not specified.");
}

}
}
Using nested ternary

1) Accept three numbers and print the largest number of it. It is


greater than zero. Print the square and cube and print sum of it.

import java.util.*; class

v
{
public static void main(String[] args)

{ Scanner sc = new Scanner(System.in);


System.out.print(System.in); System.out.print("Enter the
first number: "); System.out.print("Enter the second number:
"); System.out.print("Enter the third number: ");

double num1 = sc.nextDouble();


double num2 = sc.nextDouble();
double num3 = sc.nextDouble();

double largest = (num1 > num2) ? ((num1 > num3) ? num1 :


num3) : ((num2 > num3) ? num2 : num3);
System.out.println("The largest number is: " + largest);

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

1) Menu driven program


B@
BL@
BLU@
BLUE
@
BLUEJ
@

ii) ABCD
BC
D
CD
D

iii)
ABCDE
ABCDE
ABCDE
ABCDE

import java.util.Scanner; class


PatternGenerator
{
public static void main(String[] args)
{Scanner sc = new Scanner(System.in);

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

System.out.println("Enter a sentence: "); String


sentence = sc.nextLine;
int vowelCount = 0;

for (int i = 0; i < sentence.length(); i++)


{
char ch = sentence.charAt(i);

if ( ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' || ch == 'u'|| ch

=='A'||ch=='E'||ch
=='I'||ch=='O'||ch=='U')

vowelCount++;
}

System.out.print ( "count of vowel="+ 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);

System.out.println("How many rows you want in this pattern?");


int rows = sc.nextInt();
System.out.println("1 pattern : 2 pattern Here is your pattern.!!!");
System.out.println("Here is your choice!!!"); int
n=sc.nextInt();
switch (n)

{ 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++)
{

for (int j = 2*rows-i; j > i; j--)


{
System.out.print(" ");
}

for (int j = 1; j <= i; j++)


{
System.out.print(j+" ");
}

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
{

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int a=sc.nextInt();

System.out.println("Enter number of terms to be printed");

int n=sc.nextInt();

System.out.println("Enter your choice");

System.out.println("1 for the first sum of series");

System.out.println("2 for the second sum of series");


int choice=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;
}

System.out.println("Sum of series ="+sum2); break;


default:System.out.println("INVALID CHOICE");

}
}
}

Number program using loop


1)Accept a number and check wheather it automophic number or
not .
import java.util.*; public class
jj
{

public static void main(String args[])


{
Scanner sc = new Scanner(System.in); System.out.print("Enter a number
to check: ");

int num = in.nextInt(); int


count=0;
int square = num*num;

int temp = num;

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
{

public static void main(String args[])


{
Scanner sc=new Scanner(System.in); System.out.println("Menu");
System.out.println("1.series=1,9,17,33,49,73,97...n 2.=1,3,5,7,9,11,13..n");
System.out.println("enter your choice"); int
n=sc.nextInt();
switch(n)

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

}
}
}
}

You might also like