Import java

22
import java.util.Scanner; class Factorial { public static void main(String args[]) { int n, c, fact = 1; System.out.println("Enter an integer to calculate it's factorial"); Scanner in = new Scanner(System.in); n = in.nextInt(); if (n < 0) { System.out.println("Number should be non-negative."); } else { for (c = 1; c <= n; c++) { fact = fact * c; } System.out.println("Factorial of " + n + " is = " + fact); } } }

Transcript of Import java

Page 1: Import java

import java.util.Scanner;

class Factorial {

public static void main(String args[]) {

int n, c, fact = 1;

System.out.println("Enter an integer to calculate it's factorial");

Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n < 0) {

System.out.println("Number should be non-negative.");

} else {

for (c = 1; c <= n; c++) {

fact = fact * c;

}

System.out.println("Factorial of " + n + " is = " + fact);

}

}

}

//OUT PUTS

//run:

Page 2: Import java

//Enter an integer to calculate it's factorial

//3

//Factorial of 3 is = 6

//BUILD SUCCESSFUL (total time: 13 seconds

import java.util.Scanner;

class sequence1 {

public static void main(String[] args) {

sequence1 fs = new sequence1();

fs.fibonacci();

}

public void fibonacci() {

int numb1 = 1;

int numb2 = 1;

int temp = 0;

@SuppressWarnings("resource")

Scanner input = new Scanner(System.in);

System.out.println("How Many Terms? (Up To n)");

int x = input.nextInt();

x = x - 2;

System.out.println(numb1);

System.out.println(numb2);

Page 3: Import java

for (int i = 0; i < x; i++) {

System.out.println(numb1 + numb2 + " ");

temp = numb1;

numb1 = numb2;

numb2 = temp + numb2;

}

}

}

import java.util.Scanner;

class BinaryGCD {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Please enter the first integer:");

int b = input.nextInt();

System.out.println("Please enter the second integer:");

int d = input.nextInt();

System.out.println("The GCD of " + b + " and " + d + " is " + getGcd(b, d) + ".");

}

public static int getGcd(int b, int d) {

int gcd = 1;

Page 4: Import java

if (b > d) {

for (int i = d; i >= 1; i--) {

if (b % i == 0 && d % i == 0) {

return i;

}

}

} else {

for (int j = b; j >= 1; j--) {

if (b % j == 0 && d % j == 0) {

return j;

}

}

}

return gcd;

}

}

//OUT PUTS

// run:

//Please enter the first integer:

//6

//Please enter the second integer:

//4

//The GCD of 6 and 4 is 2.

Page 5: Import java

//BUILD SUCCESSFUL (total time: 9 seconds)

import java.util.Scanner;

public class ODDEVEN {

}

class OddOrEven {

public static void main(String args[]) {

int x;

System.out.println("Enter an integer to check if it is odd or even ");

Scanner in = new Scanner(System.in);

x = in.nextInt();

if (x % 2 == 0) {

System.out.println("You entered an even number.");

} else {

System.out.println("You entered an odd number.");

}

}

}

//OUTPUT

// run:

//Enter an integer to check if it is odd or even

//3

Page 6: Import java

//You entered an odd number.

//BUILD SUCCESSFUL (total time: 8 seconds

import java.util.Scanner;

class TrignometricFunctionsDemo {

public static void main(String[] args) {

// Create a Scanner object which will read

// values from the console which user enters

Scanner scanner = new Scanner(System.in);

// Getting input from user from the console

System.out.println("Enter value of angle in degrees ");

// Calling nextDouble method of scanner for

// taking a double value from user and storing

// it in degrees variable

double degrees = scanner.nextDouble();

System.out.println("Lets calculate the sine, cosine and tan of angle ...");

// In order to calculate sine , cosine and tan of angle we

// use the Math class three static methods by name as :

// 1. Math.sin(a) -- Sine of a

// 2. Math.cos(a) -- Cosine of a

Page 7: Import java

// 3. Math.tan(a) -- Tangent of a

double sineOfAngle = Math.sin(degrees);

double cosOfAngle = Math.cos(degrees);

double tanOfAngle = Math.tan(degrees);

System.out.println();

System.out.println("The Sine of " + degrees + " degrees is : "

+ sineOfAngle);

System.out.println("The Cosine of " + degrees + " degrees is : "

+ cosOfAngle);

System.out.println("The Tangent of " + degrees + " degrees is : "

+ tanOfAngle);

System.out.println();

System.out.println("Lets calculate the sec, cosec and cot of angle ...");

// In order to calculate sec, cosec and cot of angle we

// just inverse the value of sin , cos and tan calculated above :

// 4. Sec of a -- 1 / Sine of a

// 5. Cosec of a -- 1/ Cosine of a

// 6. Cot of a -- 1 / Tangent of a

double secOfAngle = 1 / Math.sin(degrees);

double cosecOfAngle = 1 / Math.cos(degrees);

double cotOfAngle = 1 / Math.tan(degrees);

Page 8: Import java

System.out.println("\nThe Sec of " + degrees + " degrees is : "

+ secOfAngle);

System.out.println("The Cosec of " + degrees + " degrees is : "

+ cosecOfAngle);

System.out.println("The Cotangent of " + degrees + " degrees is : "

+ cotOfAngle);

}

}

// OUTPUT

//run:

//Enter value of angle in degrees

//30

//Lets calculate the sine, cosine and tan of angle ...

//

//The Sine of 30.0 degrees is : -0.9880316240928618

//The Cosine of 30.0 degrees is : 0.15425144988758405

//The Tangent of 30.0 degrees is : -6.405331196646276

//

//Lets calculate the sec, cosec and cot of angle ...

//

//The Sec of 30.0 degrees is : -1.012113353070178

//The Cosec of 30.0 degrees is : 6.482921234962678

//The Cotangent of 30.0 degrees is : -0.15611995216165922

//BUILD SUCCESSFUL (total time: 9 seconds)

Page 9: Import java

import java.util.Scanner;

class decimal {

}

class DecimalBinaryProgram {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

while (true) {

System.out.println("Enter integer in decimal form (or # to quit):");

String s1 = in.nextLine();

if ("#".equalsIgnoreCase(s1.trim())) {

break;

}

System.out.println(decimalToBinary(s1));

System.out.println("Enter integer in binary form (or # to quit):");

String s2 = in.nextLine();

if ("#".equalsIgnoreCase(s2.trim())) {

break;

}

System.out.println(binaryToDecimal(s2));

}

Page 10: Import java

}

private static String decimalToBinary(String s) {

int n = Integer.parseInt(s, 10);

StringBuilder sb = new StringBuilder();

if (n == 0) {

return "0";

}

int d = 0;

while (n > 0) {

d = n % 2;

n /= 2;

sb.append(d);

}

sb = sb.reverse();

return sb.toString();

}

private static String binaryToDecimal(String s) {

int degree = 1;

int n = 0;

for (int k = s.length() - 1; k >= 0; k--) {

n += degree * (s.charAt(k) - '0');

degree *= 2;

Page 11: Import java

}

return n + "";

}

}

//OUTPUT

//run:

//Enter integer in decimal form (or # to quit):

//2

//10

//Enter integer in binary form (or # to quit):

//3

//3

//Enter integer in decimal form (or # to quit):

//3

//11

//Enter integer in binary form (or # to quit):

//4

//4

//Enter integer in decimal form (or # to quit):

//4

//100

//Enter integer in binary form (or # to quit):

//5

//5

//Enter integer in decimal form (or # to quit):

Page 12: Import java

//5

//101

import java.io.*;

public class betriangle {

}

// CSC108 Chapter 4, Question 19

// Name: Iam Me Student ID: 555555555

// Tutor: Andria Hunter Prof: Ken Jackson

//

// Program Description: This program uses the Triangle class

// to create a triangle object. It then uses the is_right,

// is_scalene, is_isosceles, and is_equilateral methods

// to test what type of triangle it is.

// The Triangle class contains three variables to store the length

// of each side of the triange, and methods that can be used to determine

// determine if a triange is right, scalene, isoscelese, and equilateral.

class Triangle {

// Stores the length of each side of the Triangle object.

private int side1, side2, side3;

// Constructor to initialize the sides of the triangle.

public Triangle(int s1, int s2, int s3) {

Page 13: Import java

side1 = s1;

side2 = s2;

side3 = s3;

}

// Method to test for a right-angled triangle.

public boolean is_right() {

if (((side1 * side1) == ((side2 * side2) + (side3 * side3)))

|| ((side2 * side2) == ((side1 * side1) + (side3 * side3)))

|| ((side3 * side3) == ((side1 * side1) + (side2 * side2)))) {

return true;

} else {

return false;

}

}

// Method to test for a scalene triangle.

public boolean is_scalene() {

if ((side1 != side2) && (side1 != side3) && (side2 != side3)) {

return true;

} else {

return false;

}

}

Page 14: Import java

// Method to test for an isosceles triangle.

public boolean is_isosceles() {

if (((side1 == side2) && (side1 != side3))

|| ((side1 == side3) && (side1 != side2))

|| ((side2 == side3) && (side2 != side1))) {

return true;

} else {

return false;

}

}

// Method to test for an equilateral triangle.

public boolean is_equilateral() {

if ((side1 == side2) && (side1 == side3)) {

return true;

} else {

return false;

}

}

}

// The Test_Triangle class contains one main method where program

// execution starts. It allows the user to create triangle objects

// by entering the dimensions for the three sides of the triangle,

// and it determines the type of each triangle. The user types 'n'

Page 15: Import java

// when they want to quit the program (must enter at least 1 triangle).

class Test_Triangle {

// Main method that allows the user to enter the dimentions for

// three sides of a triangle. These dimensions are then used to

// create a Triangle object, and this object is tested to determine

// what type of triangle it is.

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

// Declare stdin so data can be read from input.

DataInputStream stdin = new DataInputStream(System.in);

// loop exits when the user response is "n"

String user_response = "y";

while (!user_response.equals("n")) {

// Ask user for 3 dimensions of triangle.

System.out.println("\nEnter side1 length: ");

int side1 = Integer.parseInt(stdin.readLine());

System.out.println("Enter side2 length: ");

int side2 = Integer.parseInt(stdin.readLine());

System.out.println("Enter side3 length: ");

int side3 = Integer.parseInt(stdin.readLine());

Page 16: Import java

// Now use these values to create a Triangle object.

Triangle tri = new Triangle(side1, side2, side3);

// Determine what kind of triangle it is.

System.out.println("\nIs triangle right-angle? " + tri.is_right());

System.out.println("Is triangle scalene? " + tri.is_scalene());

System.out.println("Is triangle isosceles? "

+ tri.is_isosceles());

System.out.println("Is triangle equilateral? "

+ tri.is_equilateral());

// Ask user if they want to continue.

System.out.println("\nDo you want to examine more triangles?");

System.out.println("(type 'y' for yes or 'n' for no)");

user_response = stdin.readLine();

}

}

}

//OUTPUT

// run:

//

//Enter side1 length:

//3

//Enter side2 length:

//2

Page 17: Import java

//Enter side3 length:

//1

//

//Is triangle right-angle? false

//Is triangle scalene? true

//Is triangle isosceles? false

//Is triangle equilateral? false

//

//Do you want to examine more triangles?

//(type 'y' for yes or 'n' for no)

//y

//

//Enter side1 length:

//7

//Enter side2 length:

//9

//Enter side3 length:

//8

//

//Is triangle right-angle? false

//Is triangle scalene? true

//Is triangle isosceles? false

//Is triangle equilateral? false

//

//Do you want to examine more triangles?

Page 18: Import java

//(type 'y' for yes or 'n' for no)

/* * Sum five numbers and print the result */public class FiveNumberSum { // Save as "FiveNumberSum.java" public static void main(String[] args) { int number1 = 11; // Declare 5 int variables to hold 5 integers int number2 = 22; int number3 = 33; int number4 = 44; int number5 = 55; int sum; // Declare an int variable called sum to hold the sum sum = number1 + number2 + number3 + number4 + number5; System.out.print("The sum is "); // Print a descriptive string System.out.println(sum); // Print the value stored in sum }

}

public class ARTIMATIC {

}

class ArithmeticTest { // Save as "ArithmeticTest.java"

public static void main(String[] args) {

int number1 = 98; // Declare an int variable number1 and initialize it to 98

int number2 = 5; // Declare an int variable number2 and initialize it to 5

int sum, difference, product, quotient, remainder; // Declare five int variables to hold results

// Perform arithmetic Operations

sum = number1 + number2;

Page 19: Import java

difference = number1 - number2;

product = number1 * number2;

quotient = number1 / number2;

remainder = number1 % number2;

// Print results

System.out.print("The sum, difference, product, quotient and remainder of "); // Print description

System.out.print(number1); // Print the value of the variable

System.out.print(" and ");

System.out.print(number2);

System.out.print(" are ");

System.out.print(sum);

System.out.print(", ");

System.out.print(difference);

System.out.print(", ");

System.out.print(product);

System.out.print(", ");

System.out.print(quotient);

System.out.print(", and ");

System.out.println(remainder);

++number1; // Increment the value stored in the variable "number1" by 1

// Same as "number1 = number1 + 1"

--number2; // Decrement the value stored in the variable "number2" by 1

// Same as "number2 = number2 - 1"

Page 20: Import java

System.out.println("number1 after increment is " + number1); // Print description and variable

System.out.println("number2 after decrement is " + number2);

quotient = number1 / number2;

System.out.println("The new quotient of " + number1 + " and " + number2

+ " is " + quotient);

}

}

//OUT PUT

// run:

//The sum, difference, product, quotient and remainder of 98 and 5

//are 103, 93, 490, 19, and 3

//number1 after increment is 99

//number2 after decrement is 4

//The new quotient of 99 and 4 is 24

//BUILD SUCCESSFUL (total time: 1 second)