Fall 2016 Homework 71 Due Date 21 October 2016 Use nested lo
Fall 2016 Homework 7_1 Due Date: 21 October 2016 Use nested loop to write these programs. 1. Write a JAVA program to display following pattern. You first need to read number of rows and columns and based on it your patter should be. For example, if you read 5 as row value and 4 as column value then, your pattern should look like: * - * - * - * - * - * - * - * - * - * - If you read 10 as row value and 5 as column value then, your pattern should look like: * - * - * * - * - * * - * - * * - * - * * - * - * * - * - * * - * - * * - * - * * - * - * * - * - * 2. Write a JAVA program to read a number from user and then find out all prime numbers up to that number. For Example, if user enters 10, then your program should show: The prime number up to 10 are: 2, 3, 5, 7 if user enters 20, then your program should show: The prime number up to 10 are: 2, 3, 5, 7, 11, 13, 17, 19
Solution
Please follow the code and comments for description :
1)
CODE :
import java.util.Scanner;
public class PatternGenerator { // class to run the program
public static void main(String[] args) { // main driver program
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
System.out.println(\"Please Enter the Rows Deisred : \"); // propmt to enter the data
int rows = sc.nextInt(); // getting the data for rows
System.out.println(\"Please Enter the Columns Desired : \"); // prompt to enter the data
int columns = sc.nextInt(); // getting the data for columns
for (int i = 1; i <= rows; i++) { // iterating over the size of rows
for (int j = 1; j <= columns; j++) { // iterating over the size of columns
if (j % 2 == 0) { // checking for the column value
System.out.print(\" - \"); // print the desired character
} else {
System.out.print(\" * \"); // print the desired character
}
}
System.out.println(\"\"); // new line printer
}
}
}
OUTPUT :
Case 1 :
Please Enter the Rows Deisred :
10
Please Enter the Columns Desired :
5
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
* - * - *
Case 2 :
Please Enter the Rows Deisred :
5
Please Enter the Columns Desired :
4
* - * -
* - * -
* - * -
* - * -
* - * -
2)
CODE :
import java.util.Scanner;
public class PrimeNumbersGen { //class to run the code
public static void main(String[] args) { // driver method
Scanner scanner = new Scanner(System.in); // scanner class to get the data from the user
int i = 0; // required initialisations
int num = 0;
String primeNum = \"\"; //empty String
System.out.println(\"Enter the Value till where you need the Prime Numbers : \"); // prompt to enter the last value
int n = scanner.nextInt(); // getting the value
for (i = 1; i <= n; i++) { // iterating over the value
int count = 0; // local variable
for (num = i; num >= 1; num--) { // inner loop to decrement the count
if (i % num == 0) { // checking for the prime condition
count = count + 1; // incrementing the count value
}
}
if (count == 2) { //appending the Prime number to the String
primeNum = primeNum + i + \", \"; // formatter
}
}
System.out.println(\"Prime numbers from 1 to \" + n + \" are : \"); // printing the output
primeNum = primeNum.substring(0, primeNum.length() - 2); // removing the leading commas
System.out.println(primeNum); // print to console
}
}
OUTPUT :
Case 1 :
Enter the Value till where you need the Prime Numbers :
10
Prime numbers from 1 to 10 are :
2, 3, 5, 7
Case 2 :
Enter the Value till where you need the Prime Numbers :
20
Prime numbers from 1 to 20 are :
2, 3, 5, 7, 11, 13, 17, 19
Hope this is helpful.

