Ex1. Write a program (Ex1.java) to display the following patterns. (Only nested
ID: 3600401 • Letter: E
Question
Ex1. Write a program (Ex1.java) to display the following patterns. (Only nested loops allowed)
#
# #
# # #
# # # #
# # # # #
Ex2. Write a program (Ex2.java) to display the following patterns. (Only nested loops allowed)
# # # # #
# # # #
# # #
# #
#
Ex3. Write a program (Ex3.java) to display the following patterns. (Only nested loops allowed)
# # # # #
# # # #
# # #
# #
#
Ex4. Write a program (Ex4.java) to display following tables. (Only nested loops allowed)
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Ex5. Write a program (Ex5.java) to play the game of "Guessing Number" again. Firstly, use Random generator to generate a number(0-9), then let the user input a guess, if they are the same, print "Congratulations! You win!". Otherwise, print "You lose." The user has at most three chances per game! (Note that do not change the generated random number before the user hits it)
Ex6. Write a program (Ex6.java) to read 10 values from the user and print the lowest and highest value entered.
Example:
Inputs: 1 3 5 7 9 10 8 6 4 2
Output:
The lowest value: 1
The highest value: 10
Ex7. Write a program (Ex7.java) to find the Nth number in Fibonacci sequence.
A Fibonacci sequence is defined as follow:
- The first two numbers are 0 and 1.
- The i th number is the sum of i-1 th number and i-2 th number.
The first ten numbers in Fibonacci sequence is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
Explanation / Answer
Answer:-
//Ex1.java
import java.util.*;
import java.lang.*;
import java.io.*;
public class Ex1 {
public static void main (String[] args) throws java.lang.Exception
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("# ");
}
System.out.println();
}
}
}
////////////////////////////////////////
Ex2.java
import java.util.*;
import java.lang.*;
import java.io.*;
public class Ex2 {
public static void main (String[] args) throws java.lang.Exception
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("# ");
}
System.out.println();
}
}
}
/////////////////////////////
Ex3.java
import java.util.*;
import java.lang.*;
import java.io.*;
public class Ex3 {
public static void main (String[] args) throws java.lang.Exception
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print("# ");
}
System.out.println();
}
}
}
///////////////////////
Ex4.java
import java.util.*;
import java.lang.*;
import java.io.*;
public class Ex4 {
public static void main (String[] args) throws java.lang.Exception
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
System.out.print(j*i+" ");
}
System.out.println("");
}
}
}
/////////////////////////////////////
Ex5.java
import java.util.Random;
import java.util.Scanner;
public class Ex5 {
public static void main(String[] args) {
Random rand = new Random();
int n = rand.nextInt(9);
Scanner input = new Scanner( System.in );
int flag=0;
for(int i=0;i<3;i++)
{
int num=input.nextInt();
if(n==num && flag==0)
{
System.out.println("Congratulations! You win!");
flag=1;
break;
}
else
{
flag=0;
System.out.println("You lose");
}
}
}
}
////////////////////////////
Ex6.java
import java.util.Scanner;
public class Ex6 {
public static void main (String[] args) throws java.lang.Exception
{
int smallest = Integer.MAX_VALUE;
int large = 0;
int num;
System.out.println("enter the number");//how many number you want to enter
Scanner input = new Scanner(System.in);
num = input.nextInt();
smallest = num; //assume first entered number as small one
// i starts from 2 because we already took one num value
for (int i = 0; i < 10; i++) {
num = input.nextInt();
//comparing each time entered number with large one
if (num > large) {
large = num;
}
//comparing each time entered number with smallest one
if (num < smallest) {
smallest = num;
}
}
System.out.println("The lowest value:" + smallest);
System.out.println("The highest value " + large);
}
}
////////////////////
Ex7.java:-
import java.util.Scanner;
public class Ex7 {
public static void main(String[] args) {
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.