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
Multiples of 2 and 3: write a c++ program Using a while loop, write a program...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
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.
*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)
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...
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...
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.
1 for each of the problems listed below write the c++ program while using WHILE loop...
1 for each of the problems listed below write the c++ program while using WHILE loop structures A program will display each term in the following sequence 1 -10 100 -1000 10000 -100000 1000000 A program will calculate and display the corresponding celsius temperatures for the given Farenheit ones from 0f to 212 f (hint c=(f-32/1.8)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT