Write a Java program that prompts the user for an integer and then prints out all the prime numbers up to that Integer
Write a Java program that prompts the user for an integer and then prints out all the prime numbers up to that Integer
Write a Java program that prompts the user for an integer and then prints out all the prime
numbers up to that Integer?
Importjava.util.Scanner
;
class PrimeNumbers
{
public static void main(String[] args)
{
int n;
int p;
Scanner s=new Scanner(System.in);
System.out.println("Enter a number: ");
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}
Aim:
The aim of this Java program is to find and display all prime numbers less than a given number
nnn. The user will input a number, and the program will output all prime numbers less than that
number.
Algorithm:
1. Input the value of nnn: The user will provide an integer nnn through the console, which
defines the upper limit for the prime numbers.
2. Iterate from 2 to n−1n-1n−1:
o For each number iii in this range, we check if it is a prime number.
3. Check if a number is prime:
o For each iii, assume that it is prime and set a flag variable ppp to 0 (indicating it's
prime).
o Then, check all numbers from 2 to i−1i-1i−1. If any number divides iii evenly
(i.e., i%j==0i \% j == 0i%j==0), set the flag ppp to 1 (indicating it's not prime).
4. Output the prime numbers:
o If the flag ppp is still 0 after checking all divisors, print iii as a prime number.
Steps in Code:
class PrimeNumbers {
public static void main(String[] args) {
int n; // Variable to store the input number
int p; // Flag variable to check if a number is prime
Scanner s = new Scanner(System.in); // Create a Scanner object to read
input from the user
1. Input:
css
Copy code
Enter a number:
10
2. Output:
Copy code
2
3
5
7
Explanation of Code:
Algorithm:
1. Start
2. Input: Prompt the user to enter a positive integer n.
3. Initialize: Use a loop to iterate through numbers from 2 to n-1 (denoted as i).
4. Check for Primality:
o For each number i, initialize a flag p to 0.
o Use another loop to check divisibility of i by all numbers from 2 to i-1 (denoted
as j).
o If i % j == 0 (i.e., i is divisible by j), set the flag p to 1 and break the inner
loop.
5. Determine Prime:
o If p == 0, print the number i as it is a prime number.
6. Repeat steps 4-5 for all numbers from 2 to n-1.
7. End
Aim:
To perform matrix multiplication of two 3×33 \times 33×3 matrices and display the resultant
matrix.
Algorithm:
1. Start
2. Input:
o Define two 3×33 \times 33×3 matrices, a and b, with predefined values.
3. Initialize:
o Create an empty 3×33 \times 33×3 matrix c to store the resultant matrix after
multiplication.
4. Matrix Multiplication:
o Loop through each row of the first matrix a:
Outer loop (i) iterates through rows of a.
o For each row in a, loop through each column of the second matrix b:
Middle loop (j) iterates through columns of b.
o Initialize the current element c[i][j] to 0.
o For each element in the row-column pair, calculate the dot product:
Inner loop (k) iterates through the elements of the current row of a and
column of b.
Multiply the corresponding elements from a and b and add to c[i][j].
5. Output:
o Print each element of the resultant matrix c row by row.
6. End