Question

In: Computer Science

Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...

Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.

Q2: Write a Java program to find the Factorial of a number using a while loop. To Understand what the Factorial of a Number is: Factorial of a number n is denoted as n! and the value of n! is: 1 * 2 * 3 * … (n-1) * n

Solutions

Expert Solution

Solution:

Q1)

Code:

Copyable Code:

//Import package

import java.util.Scanner;

//Main class

class Main {

//Main method

public static void main(String[] args) {

      //Declaration of variables

      int number,temp1=0,temp2=1;

      //Prompt and get from user

      System.out.println("Enter the number:");

      Scanner s=new Scanner(System.in);

      number=s.nextInt();

      //1

System.out.print("1.Fibonacci Series using For loop is:");

      //Using For loop

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

      {

          //output

          System.out.print(temp1 + " , ");

          //Calculation part

          int tot = temp1 + temp2;

          temp1 = temp2;

          temp2 = tot;

      }

      //2

System.out.print("\n2.Fibonacci Series using While loop is:");

      //Inititalization of variables

      int i=1;

      temp1=0;

      temp2=1;

      //Using while loop

      while (i <= number)

      {

            //Output

            System.out.print(temp1 + " , ");

            //Calculation part

            int tot = temp1 + temp2;

            temp1 = temp2;

            temp2 = tot;

            i++;

      }

}

}

Q2)

Code:

Output:

Copyable code:

//Import package

import java.util.Scanner;

//Main class

class Main {

//Main method

public static void main(String[] args) {

//Declaration of variables

int number,i=1;

long res=1;

//Prompt and get from user

System.out.println("Enter the number:");

Scanner s=new Scanner(System.in);

number=s.nextInt();

//Factorial of the number is

while(i<=number)

{

//Calculation

res = res * i;

i++;

}

//Print the result

System.out.println("Factorial of given number "+number+" is: "+res);

}

}


Related Solutions

​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
every code should be in java Program 1: Write a while loop that sums the integers...
every code should be in java Program 1: Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. [Hint: Use logical operators] Program 2: Write a for loop that attempts to display the numbers from 1 to 10, but terminates when the control variable reaches the value 6. Program 3: Write a do...while loop that prints the integers from 10 to 0, inclusive.
QUESTION Write the main while loop for a Java program that processes a set of data...
QUESTION Write the main while loop for a Java program that processes a set of data as follows: Each line of the input consists of 2 numbers representing a quantity and price. Your loop should: 1. Read in the input 2. Calculate the tax. Tax will be 8.875% of price and will only be applicable if price is more than 110. Calculate the new price which is the price plus tax. 3. Calculate the final price by multiplying quantity by...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT