0% found this document useful (0 votes)
3 views11 pages

Java Lab Practice Ans

The document contains Java code examples demonstrating various programming concepts including factorial calculation using a while loop, average marks calculation using a do-while loop, multiplication table generation, matrix transposition, type promotion in expressions, and more. Each section provides a complete program with user input and output. The examples cover topics such as control structures, arrays, and basic algorithms.

Uploaded by

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

Java Lab Practice Ans

The document contains Java code examples demonstrating various programming concepts including factorial calculation using a while loop, average marks calculation using a do-while loop, multiplication table generation, matrix transposition, type promotion in expressions, and more. Each section provides a complete program with user input and output. The examples cover topics such as control structures, arrays, and basic algorithms.

Uploaded by

devilswhite625
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q11.

Factorial using While Loop

import [Link];

public class Q11_Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

int fact = 1, i = 1;

while (i <= n) {

fact *= i;

i++;

[Link]("Factorial = " + fact);

Q12. Marks Average using Do-While

import [Link];

public class Q12_AverageMarks {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int sum = 0, count = 0, marks;

do {

[Link]("Enter marks (-1 to stop): ");

marks = [Link]();

if (marks != -1) {

sum += marks;
count++;

} while (marks != -1);

if (count > 0)

[Link]("Average = " + (sum / (double)count));

else

[Link]("No marks entered.");

Q13. Multiplication Table

import [Link];

public class Q13_Table {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number: ");

int n = [Link]();

for (int i = 1; i <= 10; i++) {

[Link](n + " x " + i + " = " + (n * i));

Q14. 2D Array Transpose

import [Link];

public class Q14_Transpose {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

[Link]("Enter rows: ");

int r = [Link]();

[Link]("Enter cols: ");

int c = [Link]();

int[][] mat = new int[r][c];

[Link]("Enter matrix elements:");

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

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

mat[i][j] = [Link]();

[Link]("Transpose:");

for (int i = 0; i < c; i++) {

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

[Link](mat[j][i] + " ");

[Link]();

Q15. Type Promotion in Expressions

public class Q15_TypePromotion {

public static void main(String[] args) {

byte b = 42;

char c = 'A';

short s = 1024;

int i = 50000;

float f = 5.67f;

double d = .1234;
double result = (f * b) + (i / c) - (d * s);

[Link]("Result = " + result);

Q16. Ternary Operator – Max of 3

import [Link];

public class Q16_MaxOfThree {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter three numbers: ");

int a = [Link](), b = [Link](), c = [Link]();

int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

[Link]("Maximum = " + max);

Q17. Prime Numbers with Break & Continue

public class Q17_Primes {

public static void main(String[] args) {

for (int n = 2; n <= 50; n++) {

boolean prime = true;

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

prime = false;

break; // stop inner loop

if (!prime) continue;
[Link](n + " ");

Level III (Complex Logic)

Q18. Number Triangle

public class Q18_Triangle {

public static void main(String[] args) {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= i; j++) {

[Link](j + " ");

[Link]();

Q19. Array Sorting

import [Link];

public class Q19_ArraySort {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter size: ");

int n = [Link]();

int[] arr = new int[n];

[Link]("Enter elements:");

for (int i = 0; i < n; i++) arr[i] = [Link]();


for (int i = 0; i < n - 1; i++)

for (int j = i + 1; j < n; j++)

if (arr[i] > arr[j]) {

int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

[Link]("Sorted: ");

for (int num : arr) [Link](num + " ");

Q20. Count Vowels & Consonants

import [Link];

public class Q20_VowelsConsonants {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter string: ");

String str = [Link]().toLowerCase();

int v = 0, c = 0;

for (char ch : [Link]()) {

if (ch >= 'a' && ch <= 'z') {

if ("aeiou".indexOf(ch) != -1) v++;

else c++;

[Link]("Vowels = " + v + ", Consonants = " + c);

}
Q21. Matrix Addition & Multiplication

import [Link];

public class Q21_MatrixOps {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int r = 2, c = 2; // example fixed size

int[][] A = new int[r][c];

int[][] B = new int[r][c];

[Link]("Enter matrix A:");

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

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

A[i][j] = [Link]();

[Link]("Enter matrix B:");

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

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

B[i][j] = [Link]();

// Addition

[Link]("Sum:");

for (int i = 0; i < r; i++) {

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

[Link]((A[i][j] + B[i][j]) + " ");

[Link]();

// Multiplication

[Link]("Product:");

int[][] prod = new int[r][c];


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

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

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

prod[i][j] += A[i][k] * B[k][j];

for (int[] row : prod) {

for (int val : row) [Link](val + " ");

[Link]();

Q22. Menu-Driven Array Operations

import [Link];

public class Q22_ArrayMenu {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int[] arr = new int[10];

int size = 0, choice;

do {

[Link]("\[Link] [Link] [Link] [Link] [Link]");

choice = [Link]();

switch(choice) {

case 1 -> {

[Link]("Enter value: ");

arr[size++] = [Link]();

case 2 -> {
[Link]("Enter value to delete: ");

int val = [Link]();

boolean found = false;

for (int i = 0; i < size; i++) {

if (arr[i] == val) {

for (int j = i; j < size - 1; j++)

arr[j] = arr[j+1];

size--;

found = true;

break;

if (!found) [Link]("Not found.");

case 3 -> {

[Link]("Enter value to search: ");

int val = [Link]();

boolean present = false;

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

if (arr[i] == val) { present = true; break; }

[Link](present ? "Found" : "Not Found");

case 4 -> {

[Link]("Array: ");

for (int i = 0; i < size; i++) [Link](arr[i] + " ");

[Link]();

} while(choice != 5);

}
Q23. Reverse Number & Palindrome

import [Link];

public class Q23_Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number: ");

int n = [Link](), rev = 0, temp = n;

while (n > 0) {

rev = rev * 10 + (n % 10);

n /= 10;

[Link]("Reversed = " + rev);

[Link](temp == rev ? "Palindrome" : "Not Palindrome");

Q24. Login System

import [Link];

public class Q24_Login {

public static void main(String[] args) {

final String USER = "admin";

final String PASS = "1234";

Scanner sc = new Scanner([Link]);

int attempts = 0;

boolean success = false;


while (attempts < 3) {

[Link]("Username: ");

String u = [Link]();

[Link]("Password: ");

String p = [Link]();

if ([Link](USER) && [Link](PASS)) {

success = true;

break;

} else {

[Link]("Invalid. Attempts left: " + (2 - attempts));

attempts++;

[Link](success ? "Login Successful" : "Account Locked!");

You might also like