0% found this document useful (0 votes)
20 views15 pages

Week2 - Levels 2

The document contains Java code examples for various programming exercises, including identifying odd and even numbers, calculating employee bonuses based on years of service, generating multiplication tables, and implementing the FizzBuzz program. Additional examples include finding the youngest and tallest friends, calculating factors and greatest factors of a number, computing powers of numbers, and listing multiples of a number below 100. Each example includes user input prompts and corresponding outputs.

Uploaded by

bb7431
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)
20 views15 pages

Week2 - Levels 2

The document contains Java code examples for various programming exercises, including identifying odd and even numbers, calculating employee bonuses based on years of service, generating multiplication tables, and implementing the FizzBuzz program. Additional examples include finding the youngest and tallest friends, calculating factors and greatest factors of a number, computing powers of numbers, and listing multiples of a number below 100. Each example includes user input prompts and corresponding outputs.

Uploaded by

bb7431
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/ 15

Week2 – levels 2

1. Odd and Even Numbers Between 1 to the Number Entered by the User

import java.util.Scanner;

public class OddEven {

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

​ Scanner scanner = new Scanner(System.in);

​ System.out.print("Enter a number: ");

​ int number = scanner.nextInt();

​ if (number <= 0) {

System.out.println("Please enter a natural number.");

​ } else {

​ for (int i = 1; i <= number; i++) {

​ if (i % 2 == 0) {

System.out.println(i + " is Even");

​ } else {

System.out.println(i + " is Odd");

​ }

​ }

​ }

scanner.close();
​ }

Output:

Enter a number: 10

1 is Odd

2 is Even

3 is Odd

4 is Even

5 is Odd

6 is Even

7 is Odd

8 is Even

9 is Odd

10 is Even

2. Bonus of Employees Based on Their Years of Service

import java.util.Scanner;

public class EmployeeBonus {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter salary: ");

​ double salary = scanner.nextDouble();


System.out.print("Enter years of service: ");

​ int years = scanner.nextInt();

​ double bonus = 0;

​ if (years > 5) {

​ bonus = salary * 0.05;

​ }

System.out.println("Bonus: " + bonus);

scanner.close();

​ }

Output:

Enter salary: 50000

Enter years of service: 6

Bonus: 2500.0

3. Multiplication Table from 6 to 9

import java.util.Scanner;

public class MultiplicationTable {

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


​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

​ int number = scanner.nextInt();

​ for (int i = 6; i <= 9; i++) {

System.out.println(number + " * " + i + " = " + (number * i));

​ }

scanner.close();

​ }

Output:

Enter a number: 5

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

4. FizzBuzz Program

import java.util.Scanner;

public class FizzBuzz {

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

​ Scanner scanner = new Scanner(System.in);


System.out.print("Enter a number: ");

​ int number = scanner.nextInt();

​ if (number > 0) {

​ for (int i = 1; i <= number; i++) {

​ if (i % 3 == 0 && i % 5 == 0) {

System.out.println("FizzBuzz");

​ } else if (i % 3 == 0) {

System.out.println("Fizz");

​ } else if (i % 5 == 0) {

System.out.println("Buzz");

​ } else {

System.out.println(i);

​ }

​ }

​ }

scanner.close();

​ }

Output:

Enter a number: 15

2
Fizz

Buzz

Fizz

Fizz

Buzz

11

Fizz

13

14

5. FizzBuzz Using While Loop

import java.util.Scanner;

public class FizzBuzzWhile {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

​ int number = scanner.nextInt();


​ int i = 1;

​ while (i <= number) {

​ if (i % 3 == 0 && i % 5 == 0) {

System.out.println("FizzBuzz");

​ } else if (i % 3 == 0) {

​ System.out.println("Fizz");

​ } else if (i % 5 == 0) {

System.out.println("Buzz");

​ } else {

System.out.println(i);

​ }

​ i++;

​ }

scanner.close();

​ }

Output:

Enter a number: 15

Fizz

Buzz

Fizz
7

Fizz

Buzz

11

Fizz

13

14

FizzBuzz

6. Youngest Friend and Tallest Friend

import java.util.Scanner;

public class YoungestTallest {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter age of Amar: ");

​ int amarAge = scanner.nextInt();

System.out.print("Enter height of Amar: ");

​ int amarHeight = scanner.nextInt();

System.out.print("Enter age of Akbar: ");

​ int akbarAge = scanner.nextInt();


System.out.print("Enter height of Akbar: ");

​ int akbarHeight = scanner.nextInt();

System.out.print("Enter age of Anthony: ");

​ int anthonyAge = scanner.nextInt();

System.out.print("Enter height of Anthony: ");

​ int anthonyHeight = scanner.nextInt();

​ int youngestAge = Math.min(Math.min(amarAge, akbarAge), anthonyAge);

​ int tallestHeight = Math.max(Math.max(amarHeight, akbarHeight), anthonyHeight);

​ if (amarAge == youngestAge) {

System.out.println("Youngest friend is Amar");

​ } else if (akbarAge == youngestAge) {

System.out.println("Youngest friend is Akbar");

​ } else {

System.out.println("Youngest friend is Anthony");

​ }

​ if (amarHeight == tallestHeight) {

System.out.println("Tallest friend is Amar");

​ } else if (akbarHeight == tallestHeight) {

System.out.println("Tallest friend is Akbar");

​ } else {
System.out.println("Tallest friend is Anthony");

​ }

scanner.close();

​ }

Output:

Enter age of Amar: 25

Enter height of Amar: 175

Enter age of Akbar: 22

Enter height of Akbar: 180

Enter age of Anthony: 28

Enter height of Anthony: 170

Youngest friend is Akbar

Tallest friend is Akbar

7. Factors of a Number

import java.util.Scanner;

public class Factors {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


​ int number = scanner.nextInt();

​ System.out.println("Factors of " + number + " are:");

​ for (int i = 1; i <= number; i++) {

​ if (number % i == 0) {

System.out.println(i);

​ }

​ }

scanner.close();

​ }

Output:

Enter a number: 12

Factors of 12 are:

12

8. Greatest Factor of a Number (Excluding Itself)

import java.util.Scanner;
public class GreatestFactor {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

​ int number = scanner.nextInt();

​ int greatestFactor = 1;

​ for (int i = number - 1; i >= 1; i--) {

​ if (number % i == 0) {

greatestFactor = i;

​ break;

​ }

​ }

System.out.println("Greatest factor of " + number + " is: " + greatestFactor);

scanner.close();

​ }

Output:

Enter a number: 12

Greatest factor of 12 is: 6


9. Power of a Number

import java.util.Scanner;

public class Power {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter number: ");

​ int number = scanner.nextInt();

System.out.print("Enter power: ");

​ int power = scanner.nextInt();

​ int result = 1;

​ for (int i = 1; i <= power; i++) {

​ result *= number;

​ }

​ System.out.println(number + " raised to the power " + power + " is: " + result);

scanner.close();

​ }

Output:

Enter number: 2
Enter power: 3

2 raised to the power 3 is: 8

10. Multiples of a Number Below 100

import java.util.Scanner;

public class Multiples {

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

​ Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

​ int number = scanner.nextInt();

​ for (int i = 100; i >= 1; i--) {

​ if (i % number == 0) {

System.out.println(i);

​ }

​ }

scanner.close();

​ }

Output:

Enter a number: 5
100

95

90

85

80

75

70

65

60

55

50

45

40

35

30

25

20

15

10

You might also like