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

Comp - Project Book

The document contains code snippets from Java programs that demonstrate various concepts like interest calculation, time conversion, swapping values, finding maximum/minimum values, calculating average, area and volume calculations, conditional statements, etc. The code snippets are primarily focused on input/output operations and applying basic math functions to solve problems.

Uploaded by

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

Comp - Project Book

The document contains code snippets from Java programs that demonstrate various concepts like interest calculation, time conversion, swapping values, finding maximum/minimum values, calculating average, area and volume calculations, conditional statements, etc. The code snippets are primarily focused on input/output operations and applying basic math functions to solve problems.

Uploaded by

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

Question 5 pg 119

import java.util.*;

public class CI
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter sum of money: ");
double p = sc.nextDouble();
double inter = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " + inter);
p += inter;
inter = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " + inter);
p += inter;
inter = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " + inter);
}
}
Question 7

import java.util.*;

public class time


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
int sec = sc.nextInt();
int hr = sec / 3600;
sec %= 3600;
int mins = sec / 60;
sec %= 60;
System.out.println(hr + " Hours " + mins
+ " Minutes " + sec + " Seconds");
}
}
Question 8

import java.util.*;

public class num


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.println("Enter first number: ");
int a = sc.nextInt();
System.out.println("Enter second number: ");
int b = sc.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + " b = " + b);
}
}
Question 10

import java.util.*;

public class shop


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the selling price: ");
double sp = sc.nextDouble();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double tot = cp1 + cp2;
System.out.println("Total Cost Price = " + tot);
}
}
Pg 145

Question 1

import java.util.*;

public class big


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

Scanner sc = new Scanner(System.in);

System.out.println("Enter First Number: ");


int a = sc.nextInt();
System.out.println("Enter Second Number: ");
int b = sc.nextInt();
System.out.println("Enter Third Number: ");
int c = sc.nextInt();

int g = Math.max(a, b);


g = Math.max(g, c);

int s = Math.min(a, b);


s = Math.min(s, c);

System.out.println("Greatest Number = " + g);


System.out.println("Smallest Number = " + s);
}
}
Question 2

import java.util.*;

public class hypoetnuse


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Perpendicular: ");
double p = sc.nextDouble();
System.out.println("Enter Base: ");
double b = sc.nextDouble();

double h = Math.sqrt(Math.pow(p, 2) + Math.pow(b, 2));

System.out.println("Hypotenuse = " + h);


}
}
Question 3

import java.util.*;

public class math


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: ");
double n = sc.nextDouble();

System.out.println("Natural logarithm = " + Math.log(n));


System.out.println("Absolute value = " + Math.abs(n));
System.out.println("Square root = " + Math.sqrt(n));
System.out.println("Cube root= " + Math.cbrt(n));
System.out.println("Random number = " + Math.random());
}
}
Question 4

import java.util.*;

public class avg


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Marks");
System.out.println("Physics: ");
int p = sc.nextInt();
System.out.println("Chemistry: ");
int c = sc.nextInt();
System.out.println("Biology: ");
int b = sc.nextInt();

double avg = (p + c + b) / 3.0;


double round = Math.round(avg);

System.out.println("Rounded Off Avg Marks = " + round);


}
}
Question 5

import java.util.Scanner;

public class KboatCircleRadius


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Area of Circle: ");
double area = in.nextDouble();
double r = Math.sqrt(7 * area / 22);
System.out.print("Radius of Circle = " + r);
}
}
SPecial question

import java.util.*;
/*
* Extra Question - Write a Program to display Grades of student as follows:
If marks >=90 then A+
If marks >=80 then A
If marks >=70 then B+
If marks >=65 then B
If marks >=60 then C
If marks >=50 then D

*/
public class mark
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Marks");
int m = sc.nextInt();
if(m>0)
{
if(m>=90 && m<=100) System.out.println("Marks = A+");
else if(m>=80 && m<=89) System.out.println("Marks = A");
else if(m>=70 && m<=79) System.out.println("Marks = B+");
else if(m>=65 && m<=69) System.out.println("Marks = B");
else if(m>=60 && m<=64) System.out.println("Marks = C");
else System.out.println("Marks = D");
}
}
}
Question 1 pg 178

import java.util.*;

public class tri$angle


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = sc.nextInt();
System.out.print("Enter second angle: ");
int a2 = sc.nextInt();
System.out.print("Enter third angle: ");
int a3 = sc.nextInt();
int sum = a1 + a2 + a3;

if (sum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {


if (a1 < 90 && a2 < 90 && a3 < 90) System.out.println("Acute-angled Triangle");
else if (a1 == 90 || a2 == 90 || a3 == 90) System.out.println("Right-angled Triangle");
else System.out.println("Obtuse-angled Triangle");
}
else System.out.println("Triangle not possible");

}
}
Question 5

import java.util.*;

public class year


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year to check: ");
int yr = sc.nextInt();

if (yr % 4 == 0 && yr % 100 != 0) System.out.println("It is a Leap Year");


else if (yr % 400 == 0) System.out.println("It is a Century Leap Year");
else if (yr % 100 == 0) System.out.println("It is a Century Year but not a Leap Year");
else System.out.println("It is neither a Century Year nor a Leap Year");
}
}
Question 9

import java.util.*;

public class taximoolah


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Taxi Number: ");
String taxi = sc.nextLine();
System.out.println("Enter distance travelled: ");
int d = sc.nextInt();

int c = 0;
if (d <= 5)
c = 100;
else if (d <= 15)
c = 100 + (d - 5) * 10;
else if (d <= 25)
c = 100 + 100 + (d - 15) * 8;
else
c = 100 + 100 + 80 + (d - 25) * 5;

System.out.println("Taxi No: " + taxi);


System.out.println("Distance covered: " + d);
System.out.println("Amount: " + c);

}
}
Question 13

import java.util.*;
public class lic
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name: ");
String name = sc.nextLine();
System.out.println("Enter Sum Assured: ");
double sum = sc.nextDouble();
System.out.println("Enter First Premium: ");
double pre = sc.nextDouble();
double disc = 0.0, com = 0.0;
if(sum <= 100000){
disc = pre * 5.0 / 100.0;
com = sum * 2.0 / 100.0;
}
else if(sum <= 200000){
disc = pre * 8.0 / 100.0;
com = sum * 3.0 / 100.0;
}
else if(sum <= 500000){
disc = pre * 10.0 / 100.0;
com = sum * 5.0 / 100.0;
}
else{
disc = pre * 15.0 / 100.0;
com = sum * 7.5 / 100.0;
}
System.out.println("Name of the policy holder: " + name);
System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + pre);
System.out.println("Discount on the first premium: " + disc);
System.out.println("Commission of the agent: " + com);

}
}
Question 16

import java.util.*;
public class volmenu
{
public static void main(String args[]) {
Scanner sc= 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.println("Enter your choice: ");
int ch = sc.nextInt();

switch(ch) {
case 1:
System.out.print("Enter length of cuboid: ");
double l = sc.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = sc.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = sc.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
case 2:
System.out.println("Enter radius of cylinder: ");
double rCy = sc.nextDouble();
System.out.println("Enter height of cylinder: ");
double hCy = sc.nextDouble();
double vCy = (22 / 7.0) * Math.pow(rCy, 2) * hCy;
System.out.println("Volume of cylinder = " + vCy);
break;
case 3:
System.out.print("Enter radius of cone: ");
double rCo = sc.nextDouble();
System.out.println("Enter height of cone: ");
double hCo = sc.nextDouble();
double vCo = (1 / 3.0) * (22 / 7.0) * Math.pow(rCo, 2) * hCo;
System.out.println("Volume of cone = " + vCo);
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}
Question 19

import java.util.*;

public class interest


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sum: ");
double p = sc.nextDouble();
System.out.println("Enter rate: ");
double r = sc.nextDouble();
System.out.println("Enter time: ");
int ti = sc.nextInt();
System.out.println("Enter type of Interest");
System.out.println("('S'- Simple Interest 'C'- Compound Interest): ");
char t = sc.next().charAt(0);
boolean isT = true;

double inter = 0.0;

switch (t) {
case 'S':
inter = p * r * ti / 100;
break;

case 'C':
inter = p * (Math.pow((1 + (r / 100)), ti) - 1);
break;

default:
isT = false;
System.out.println("Incorrect Interest type");
break;
}

if (isT) {
double amt = p + inter;
System.out.println("Sum = " + p);
System.out.println("Interest = " + inter);
System.out.println("Sum + Interest = " + amt);
}
}
}
Page 230 question 5

import java.util.*;
public class psqr
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter m: ");
int m = sc.nextInt();
System.out.println("Enter n: ");
int n = sc.nextInt();
if (m < n && m > 0 && n > 0) {
for (int i = m; i <= n; i++) {
System.out.println("Number = " + i);
double sroot = Math.sqrt(i);
if (sroot == Math.floor(sroot))
System.out.println(i + " is a perfect square");
}
}
else System.out.println("Invalid input");
}
}
Question 6
import java.util.*;
public class buzz
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter p: ");
int p = sc.nextInt();
System.out.println("Enter q: ");
int q = sc.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("p should be less than q");
}
}
Question 10

import java.util.*;
public class rev
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: ");
int org = sc.nextInt();
int copy = org;
int rev = 0;
while(copy != 0) {
int dig = copy % 10;
copy /= 10;
rev = rev * 10 + dig;
}
int diff = rev - org;
System.out.println("Reversed Number = " + rev);
System.out.println("Absolute Difference = " + Math.abs(diff));
}
}
Question 11

import java.util.*;
public class gcd
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
int a = sc.nextInt();
System.out.println("Enter second number: ");
int b = sc.nextInt();
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
System.out.println("GCD=" + a);
}
}
Pg 265

Question 14 a
import java.util.*;
public class series1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter n: ");
int n = sc.nextInt();
double s = 0.0;
for (int i = 1; i <= n; i++)
{
double f = 1;
for (int j = 1; j <= i; j++) f *= j;
s += (1.0 / f);
}
System.out.println("Series=" + s);
}
}

Part d
import java.util.*;
public class series2
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter n");
int n = sc.nextInt();
double s = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) s += j;
}
System.out.println("Series=" + s);
}
}
15)c and e

public class pat


{
public static void main(String args[]) {
for (int j = 1; j <= 5; j++) {
for (int i = 5; i >= j; i--) {
System.out.print(i + " ");
}
System.out.println();
}
for (int j = 4; j >=1; j--) {
for (int i = 5; i >=j; i--) {
System.out.print(i + " ");
}
System.out.println();
}
}
}

You might also like