0% found this document useful (0 votes)
7 views

Write a Java program that prompts the user for an integer and then prints out all the prime numbers up to that Integer

The document provides a Java program that prompts the user for an integer and prints all prime numbers less than that integer. It includes an algorithm for checking primality and a detailed explanation of the code structure. Additionally, it outlines a separate aim to perform matrix multiplication of two 3x3 matrices, along with the corresponding algorithm.

Uploaded by

kaminiganesan13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Write a Java program that prompts the user for an integer and then prints out all the prime numbers up to that Integer

The document provides a Java program that prompts the user for an integer and prints all prime numbers less than that integer. It includes an algorithm for checking primality and a detailed explanation of the code structure. Additionally, it outlines a separate aim to perform matrix multiplication of two 3x3 matrices, along with the corresponding algorithm.

Uploaded by

kaminiganesan13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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:

1. Input: Read the number nnn from the user.


2. Loop through numbers from 2 to n−1n-1n−1 to check if each number is prime.
3. Check for primality: If a number is divisible by any number other than 1 and itself, it is
not prime.
4. Output: Print all prime numbers found.

Code with Comments:


java
Copy code
import java.util.Scanner; // Import Scanner class for taking user input

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

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


n = s.nextInt(); // Read the integer input from the user

// Iterate from 2 to n-1 to check for prime numbers


for (int i = 2; i < n; i++) {
p = 0; // Assume that 'i' is prime initially

// Check if 'i' is divisible by any number from 2 to i-1


for (int j = 2; j < i; j++) {
if (i % j == 0) { // If 'i' is divisible by 'j', it's not
prime
p = 1; // Mark 'i' as non-prime
break; // Exit the loop early since no need to check
further divisors
}
}

// If 'p' is still 0, 'i' is a prime number


if (p == 0) {
System.out.println(i); // Print the prime number
}
}
}
}
Example Run:

1. Input:

css
Copy code
Enter a number:
10

2. Output:

Copy code
2
3
5
7

Explanation of Code:

 Scanner: Used to get user input.


 Outer loop: Loops through all numbers from 2 to n−1n-1n−1 (since 1 is not a prime
number).
 Inner loop: Checks if the current number iii is divisible by any number from 2 to i−1i-
1i−1.
o If it is divisible, it sets the flag ppp to 1 and breaks the loop, indicating that iii is
not prime.
o If no divisors are found (i.e., ppp remains 0), it prints the number as a prime
number.

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

You might also like