From 09f535891f34edf105e3b74311a2f17f0fa898d7 Mon Sep 17 00:00:00 2001 From: Kilokem Date: Thu, 12 Sep 2024 19:50:18 +0200 Subject: [PATCH] FinishedFirst --- Basics/src/main/java/com/example/Basics.java | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Basics/src/main/java/com/example/Basics.java b/Basics/src/main/java/com/example/Basics.java index 11a7647..2fc77f5 100644 --- a/Basics/src/main/java/com/example/Basics.java +++ b/Basics/src/main/java/com/example/Basics.java @@ -100,6 +100,56 @@ public class Basics { System.out.println("Something else!"); break; } + + /* + Given the age of a person as an input, output their age group. + Here are the age groups you need to handle: + Child: 0 to 11 + Teen: 12 to 17 + Adult: 18 to 64 + Sample Input + 42 + Sample Output + Adult + */ + int ages = sc.nextInt(); + if (ages > 0 && ages <= 11) { + System.out.println("Child"); + }else if(ages <= 17){ + System.out.println("Teen"); + } else if (ages <= 64) { + System.out.println("Adult"); + } + + /* + Your math teacher asked you to calculate the sum of the numbers 1 to N, where N is a given number. + Task: Take an integer N from input and output the sum of the numbers 1 to N, inclusive. + Sample Input: + 10 + Sample Output: + 55 + */ + + int number = sc.nextInt(); + int sum = 0; + while (number > 0) { + sum += number; + number--; + } + System.out.println(sum); + + /* + The factorial of a number N is equal to 1 * 2 * 3 * ... * N + For example, the factorial of 5 is 1* 2 * 3 * 4 * 5 = 120. + Create a program that takes a number from input and output the factorial of that number. + */ + long fact = 1; + int num = sc.nextInt(); + for (int i = 2; i <= num; i++) { + fact *= i; + } + System.out.println(fact); + sc.close(); sci.close(); }