Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise #4: The factorial of a nonnegative n written as n! is defined as follow

ID: 3607313 • Letter: E

Question

Exercise #4: The factorial of a nonnegative n written as n! is defined as follows: n'= n"(n-1)*(1-2) * * 1 (for all values of n greater than 0) and 0!-1 For example 5' = 5*4*3*2*1 which is 120. Write a java program that reads a nonnegative integer and computes and prints its factorial. Use While loop statement. Exercise #5: Create a java program to draw the UAE flag in a textual form. Assume that the UAE flag is a rectangular shape flag in which its length is triple its width. Specifically, you need to place the character R (for red) in place of red part of the flag. Similarly, you need place the characters G (for green), W (for white), and B (black) in the right place in the flag. See flag below. The flag should be dynamic with the input width. Assume that width is a multiple of 3 and the minimum width is 3. Input is formatted as follows: Where N is the number of flags and Wi is the width of the ith flag Sample Inp Resultant Output RWHWWw 3 6 9

Explanation / Answer

Solution 4:

Copy the below java code to a file named with Factorial.java and run the below command to compile and run.

Compile command : javac Factorial.java

Run Command : java Factorial

import java.util.Scanner;

public class Factorial {

public static void main(String [] args){
int num;
int result;
Scanner input = new Scanner(System.in);
System.out.print("Enter nonnegative number for it's factorial : ");
if(input.hasNextInt()){
num=input.nextInt();
if (num < 0) {
System.out.println("Please enter a valid input");
} else if (num == 0){
result =1;
System.out.println("The factorial of " + num + " is : " + result);
} else {
int factorial = 1;
int temp = num;
while(temp > 0){
factorial = factorial * temp--;
}
result = factorial;
System.out.println("The factorial of " + num + " is : " + result);
}
} else {
System.out.println("Please enter a valid input");
}
}
}

Solution 5.

Copy the below code in UEAFlag.java and use the below mentioned command.

Sample output :

javac UAEFlag.java
amitkumar:~/programs/java$ java UAEFlag
Enter the total number of flags : 3
Enter the width of each flag(Width in multiple of 3) : 3 6 9


RRRGGGGGG
RRRWWWWWW
RRRBBBBBB


RRRRRRGGGGGGGGGGGG
RRRRRRGGGGGGGGGGGG
RRRRRRWWWWWWWWWWWW
RRRRRRWWWWWWWWWWWW
RRRRRRBBBBBBBBBBBB
RRRRRRBBBBBBBBBBBB


RRRRRRRRRGGGGGGGGGGGGGGGGGG
RRRRRRRRRGGGGGGGGGGGGGGGGGG
RRRRRRRRRGGGGGGGGGGGGGGGGGG
RRRRRRRRRWWWWWWWWWWWWWWWWWW
RRRRRRRRRWWWWWWWWWWWWWWWWWW
RRRRRRRRRWWWWWWWWWWWWWWWWWW
RRRRRRRRRBBBBBBBBBBBBBBBBBB
RRRRRRRRRBBBBBBBBBBBBBBBBBB
RRRRRRRRRBBBBBBBBBBBBBBBBBB

Code Snippet :

import java.util.Scanner;

public class UAEFlag {
public static void main(String [] args){
int noOfFlags;
int [] widths;
Scanner input = new Scanner(System.in);
System.out.print("Enter the total number of flags : ");
if(input.hasNextInt()){
noOfFlags=input.nextInt();
widths = new int[noOfFlags];
System.out.print("Enter the width of each flag(Width in multiple of 3) : " );
int width;
for(int a=0; a<noOfFlags; a++){
width = input.nextInt();
widths[a] = width;
drawFlag(width);
}
} else {
System.out.print("Please enter a valid Input");

}
}
  
public static void drawFlag(int width){
int flagWidth=width;
int flagLength=3*width;
System.out.println(" ");
for(int row=1; row<=flagWidth; row++ ){
for(int column=1; column<=flagLength; column++ ){
if(column<=width){
System.out.print("R");
} else if(row <= flagWidth/3) {
System.out.print("G");
} else if(row <= 2*flagWidth/3) {
System.out.print("W");
} else {
System.out.print("B");
}
}
System.out.print(" ");
}
System.out.println(" ");

}
}