In: Computer Science
Write a program (your choice!) that fulfills all the requirements below. The final product should include the pseudocode and flowchart for the entire program.
Explanation:
Program is also provided at the end.The program was developed using eclipse Mars Release (4.5.0).
I have written the program to demonstrate the following concepts:
Program Description:
Program is completely coded in Java
The program requires user to enter their name which is stored in a String variable.Next the user is asked to select the type of operation that needs to be performed.
Choice of operations are represented by integer number 1 being the lowest and 4 being the highest input.
The type of Operations and their corresponding Integer numbers are given below:
1.Print Fibonacci series 2.Calculate Factorial 3.Print Odd Even numbers upto 100 4.Exit
After the user selects the type of operations different functions are called based on the option selected by the user.
Three Functions have been defined in the following way:
public static int factorial(int n){
int i,fact=1;
for(i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}
public static void fibonnaci(int limit){
long[] series = new
long[limit];
//create first 2 series
elements
series[0] = 0;
series[1] = 1;
//create the Fibonacci series and
store it in an array
for(int i=2; i < limit;
i++){
series[i] =
series[i-1] + series[i-2];
}
//print the Fibonacci series
numbers
System.out.println("Fibonacci
Series upto " + limit);
for(int i=0; i< limit;
i++){
System.out.print(series[i] + " ");
}
}
public static void printOddEven(){
for(Integer
i=1;i<=100;i++){
if(i%2==0){
System.out.println("Number is even"+i);
}else{
System.out.println("Number is odd"+i);
}
}
}
Pseudo code : -
The Pseudo code for the above program consist of the following steps
Step 1 : Take name as an input from the user and store it in as a String variable
Step 2 : Provide the user with the type of operations that can be performed and their corresponding options such as
1.Print Fibonacci series 2.Calculate Factorial 3.Print Odd Even numbers upto 100 4.Exit
Step 3 : Take the input from the user and store it in a variable named nextInt
IF nextInt == 1 then
Ask the user to input the number till which fibonnaci series needs to be generated and store it in a variable named fib such as
fib=sc.next();
Pass the number provided as an input to the function fibonnaci(fib);
IF nextInt==2 then
Ask the user to input the number till whose factorial needs to be calculated and store it in a vairable named fin
Print the fibonnaci series
fin=sc.next();
Pass the number provided as an input to the function factorial(fin);
Print the factorial of the given number
IF nextInt==3 then
Print Odd Even numbers upto 100.
ELSE
Exit the Program.
Step 4: Provide user with the prompt Do you wish to continue.Type y or n
IF user choose N
Exit the Program.
ELSE IF user choose Y
Repeat Steps 2 and 3.
ELSE
Exit the Program.
Flow Chart for the above Program:
Flow Chart depicts the flow of the program.Program Explanation has been given in the beginning and flowchart is self -explanatory
Actual Program: Save the Program by the name OddEven.java
import java.util.Scanner;
public class OddEven {
private static Scanner sc;
//Function that demonstrates use of For Loop and calculates the factorial
public static int factorial(int n){
int i,fact=1;
for(i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}
//Function that uses array and also uses Real
Number
public static void fibonnaci(int limit){
long[] series = new
long[limit];
//create first 2 series
elements
series[0] = 0;
series[1] = 1;
//create the Fibonacci series and
store it in an array
for(int i=2; i < limit;
i++){
series[i] =
series[i-1] + series[i-2];
}
//print the Fibonacci series
numbers
System.out.println("Fibonacci
Series upto " + limit);
for(int i=0; i< limit;
i++){
System.out.print(series[i] + " ");
}
}
//Function that uses Integer datatype
public static void printOddEven(){
for(Integer
i=1;i<=100;i++){
if(i%2==0){
System.out.println("Number is even"+i);
}else{
System.out.println("Number is odd"+i);
}
}
}
public static void main(String[] args) {
String name;//String Variable
Boolean choice = true;//Boolean
Variable
sc = new Scanner(System.in);
System.out.println("Enter your
name:");
name = sc.next();
System.out.println("Name entered by
the user is:"+name);
//while loop usage
while(choice){
System.out.println("Please enter which operation you want to
perform?");
System.out.print("1.Print Fibonacci series 2.Calculate Factorial
3.Print Odd Even numbers upto 100 4.Exit");
int nextInt =
sc.nextInt();
//IF ELSE IF Ladder
if(nextInt==1){
System.out.print("Enter the number upto which
fibonacci series needs to be generated");
int fib=sc.nextInt();
fibonnaci(fib);
}else
if(nextInt==2){
System.out.print("Enter the number whose
factorial is to be calculated");
int fin=sc.nextInt();
int factorial = factorial(fin);
System.out.println("Factorial of the
number"+fin+"is:"+factorial);
}else
if(nextInt==3){
printOddEven();
}else{
System.exit(0);
}
System.out.println("Do you wish to continue.Type y or n");
String next =
sc.next();
if(next.equalsIgnoreCase("n")||next.equalsIgnoreCase("N")){
System.exit(0);
}else
if(next.equalsIgnoreCase("y")||next.equalsIgnoreCase("NY")){
choice=true;
}else{
System.out.println("Invalid Input");
System.exit(0);
}
}
}
}