Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

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

The document contains three Java programs: one that prompts the user for an integer and prints all prime numbers up to that integer, another that multiplies two matrices and displays the result, and a third that counts the number of characters, lines, and words in user-inputted text. Each program includes a detailed algorithm outlining the steps to be followed. The code snippets for each program are provided for implementation.

Uploaded by

kaminiganesan13
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

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

The document contains three Java programs: one that prompts the user for an integer and prints all prime numbers up to that integer, another that multiplies two matrices and displays the result, and a third that counts the number of characters, lines, and words in user-inputted text. Each program includes a detailed algorithm outlining the steps to be followed. The code snippets for each program are provided for implementation.

Uploaded by

kaminiganesan13
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a Java program that prompts the user for an integer and then prints out all the

prime
numbers up to that Integer

Aim:

To write a Java program that accepts a number from the user and prints all prime numbers less
than the given number.

Algorithm:

1. Start the program


2. Input the Number :Prompt the user to enter a number n. using scanner class
3. Iterate through all numbers from 2 to n−1 (outer loop)and For each number I, initialize a flag
variable p to 0.
4. Use a nested loop (inner loop) to iterate from 2 to i−1
5. If i is divisible by any number j in this range, set p to 1 (not prime) and exit the inner
loop.
6. After the inner loop, if p remains 0, i is prime. Print i.
7. Continue checking for all numbers less than n.
8. End the program

import java.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);

Write a Java program to multiply two given matrices

To write a Java program to perform matrix multiplication for two given matrices and display the
resultant matrix.

import java.util.Scanner;

public class MatrixMultiplication

public static void main(String[] args)

int row1, row2, col1, col2;

Scanner scanner = new Scanner(System.in);

// Input for matrix dimensions

System.out.print("Enter number of rows for matrix A: ");

row1 = scanner.nextInt();

System.out.print("Enter number of columns for matrix A: ");


col1 = scanner.nextInt();

System.out.print("Enter number of rows for matrix B: ");

row2 = scanner.nextInt();

System.out.print("Enter number of columns for matrix B: ");

col2 = scanner.nextInt();

// Matrix multiplication is only possible if col1 == row2

if (col1 != row2) {

System.out.println("Matrix multiplication is not possible");

return;

// Initialize matrices

int[][] matrixA = new int[row1][col1];

int[][] matrixB = new int[row2][col2];

int[][] matrixC = new int[row1][col2];

// Input elements of matrix A

System.out.println("Enter elements of matrix A:");

for (int i = 0; i < row1; i++)

for (int j = 0; j < col1; j++)

matrixA[i][j] = scanner.nextInt();
}

// Input elements of matrix B

System.out.println("Enter elements of matrix B:");

for (int i = 0; i < row2; i++)

for (int j = 0; j < col2; j++)

matrixB[i][j] = scanner.nextInt();

// Matrix multiplication

System.out.println("Matrix multiplication result:");

for (int i = 0; i < row1; i++)

for (int j = 0; j < col2; j++)

matrixC[i][j] = 0; // Initialize cell

for (int k = 0; k < col1; k++)

{ // Use col1 or row2 as they are equal

matrixC[i][j] += matrixA[i][k] * matrixB[k][j];

}
System.out.print(matrixC[i][j] + " ");

System.out.println();

scanner.close();

Algorithm:

1. Input Dimensions:
o Prompt the user to enter the number of rows and columns for matrices A and B.
o Ensure that the number of columns in matrix A equals the number of rows in
matrix B. If not, output an error and exit.
2. Input Matrices:
o Create two 2D arrays for matrix A and matrix B based on the provided
dimensions.
o Prompt the user to fill these arrays with integer values.
3. Initialize Result Matrix:
o Create a 2D array for the result matrix, with dimensions row1 x col2.
4. Matrix Multiplication Logic:
o Use three nested loops:
 Outer loop for rows of matrix A.
 Middle loop for columns of matrix B.
 Inner loop to multiply elements of rows from matrix A and columns from
matrix B, and accumulate the sum in the result matrix.
5. Display Result:
o Print the elements of the resultant matrix in matrix form.

Write a Java program that displays the number of characters, lines and words in a text?
import java.util.Scanner;

public class Text {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter text (type 'EXIT' on a new line to stop):");

int lineCount = 0;
int wordCount = 0;
int charCount = 0;

while (true) {
String line = scanner.nextLine();
// Stop input if the user types "EXIT"
if (line.equalsIgnoreCase("EXIT")) {
break;
}
// Increment line count
lineCount++;
// Increment character count (including spaces)
charCount += line.length();
// Increment word count
String[] words = line.trim().split("\\s+");
wordCount += words.length;
}

// Output the statistics


System.out.println("\nText Statistics:");
System.out.println("Number of lines: " + lineCount);
System.out.println("Number of words: " + wordCount);
System.out.println("Number of characters (including spaces): " + charCount);

scanner.close();
}
}

You might also like