Inbound 3600796691372883674

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

UNIVERSIDAD DE DAGUPAN

SCHOOL OF INFORMATION TECHNOLOGY EDUCATION


ITC04 | DATA STRUCTURES & ALGORITHMS
Laboratory Exercise #1

Before coding a program, we must have first an idea how to solve it. Such that,
we have to prepare first a step by step solution also known as ALGORITHM.

Consider the example below:

PROBLEM: Write a Java program to compute for the Factorial of a given


number using a while Loop.

SOLUTION: Algorithm to Find the Factorial of a Given Number using a while


Loop
1. Create an instance of a Scanner class and name it as sc.
2. Declare new variables.
a. Integer variable i. This will act as the loop variable.
b. Integer variable num. This will act as variable for input.
c. Integer variable Fact. This will store the value of the Factorial.
3. Initialize the variables.
a. Set the integer variable i to 1.
b. Set the integer variable num to 0.
c. Set the integer variable Fact to 1.
4. Prompt the user to enter an integer number. For example, 5.
Enter an Integer value [1..10]: 5

5. Traverse using a while loop. The loop will continue to execute every time the value
of (i<=num) is TRUE.
a. Update the factorial in each iteration of the number.
Fact = Fact * i
b. Increment the loop variable.

6. Once (i<=num) is FALSE the while loop is terminated…


7. Print the factorial of a number.
The Factorial of 5 is: 120

Code Implementation of Java Program to Find Factorial of a Number using


while Loop

Create a New Java Program and name it Main_while.java

Consider typing the sample source code below. Replace the Name, Date and Block. The
code was based from the Algorithm stated above:

//Name: ARNALDY D. FORTIN


//Date: September 02, 2024
//ITC04 Block 1

import java.util.*;

Semester: 1st Semester | Academic Year: 2024-2025 1


public class Main_while
{
public static void main(String []args)
{
// Instance of a Scanner class, sc
Scanner sc=new Scanner(System.in);
// Declaration of variables I, num and Fact
int i;
int num;
int Fact;
// Initialization of variables
i = 1;
num=0;
Fact = 1;
// Input
System.out.println("Enter an Integer value [1..10]: ");
num=sc.nextInt();
// Traversing the While Loop statement
while(i<=num) //every time the condition is TRUE, the loop body is executed.
{
fact=fact*i;
i++;
}
// Output
//The output below is executed once the condition (i<=num) becomes FALSE.
System.out.println("The Factorial of “+num+” is: “+Fact);
} //end of Main_while class
} // end of the public static void main(String []args)

Sample Output
Enter an Integer value [1..10]: 5
The Factorial of 5 is: 120

UNIVERSIDAD DE DAGUPAN
SCHOOL OF INFORMATION TECHNOLOGY EDUCATION
ITC04 | DATA STRUCTURES & ALGORITHMS
Semester: 1st Semester | Academic Year: 2024-2025 2
Laboratory Exercise #1 Q&A
Name: Jofrey M. Acosta ______________________________________________
Block 3____
Date Submitted: September 5____, 2024

Run and test the program for values 13, 14 and 15, respectively.

UNIVERSIDAD DE DAGUPAN
SCHOOL OF INFORMATION TECHNOLOGY EDUCATION
ITC04 | DATA STRUCTURES & ALGORITHMS
Laboratory Exercise #1 The Updated Source Code

Name: ______________________________________________ Block ____


Date Submitted: September ____, 2024

After performing Question #4. Copy & Paste all the New Java Code below...

Question 1: What have you observed about the result of Factorial for 13, 14
and 15 respectively? (Paste screenshot and resize it below. Choose one result
only between 13! Or 14! Or 15!)

ANS;

- 13 = 6,227,020,800

- 14 = 87,178,291,200

- 15 = 1,307,674,368,000

Question 2: What do you think might be the effect of not knowing the
maximum value for an integer data type?

ANS;

- Not knowing the maximum value of an integer data type can lead to
issues such as overflow errors, performance inefficiencies, program

Semester: 1st Semester | Academic Year: 2024-2025 3


crashes, security vulnerabilities, memory wastage, and inaccurate
results. Understanding these limits is essential for reliable programming
and preventing unexpected behavior.

Question 3: What would be the appropriate solution to allow correct


calculations for larger values than 12!?

ANS;

- To handle larger factorials like \(13!\) and beyond, use big integer data
types, factorial libraries, or approximation methods like Stirling’s
approximation. These approaches ensure correct calculations without
overflow issues.

Question 4: What will be appropriate solution that will allow the program to
accept only integer numbers from 1 up to 10?

ANS;

Input Validation: Check if the input is an integer and falls within the range of 1
to 10. If it doesn’t, reject the input and prompt the user again.

Looping Structure: Use a loop to repeatedly request valid input until an


integer within the acceptable range is provided.

Error Handling: Implement error handling to catch non-integer inputs (e.g.,


using try-except in Python).

Phyton example;

While True:

Try:

Number = int(input(“Enter an integer between 1 and 10: “))

If 1 <= number <= 10:

Print(“Valid input:”, number)

Break

Else:

Print(“Please enter a number between 1 and 10.”)

Except ValueError:

Semester: 1st Semester | Academic Year: 2024-2025 4


Print(“Invalid input. Please enter an integer.”)

Submit this document in my e-mail address [email protected]

Kindly indicate your Block in the Subject portion of your e-mail composition.

Semester: 1st Semester | Academic Year: 2024-2025 5


Semester: 1st Semester | Academic Year: 2024-2025 6

You might also like