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

Assignment 2

The document contains multiple Java programs that demonstrate various programming concepts, including temperature conversion, prime number generation, bubble sort, finding the largest and smallest elements in an array, Fibonacci series generation, string reversal, digit sum calculation, palindrome checking, matrix multiplication, and diamond pattern printing. Each program includes user input and outputs the results accordingly. The programs are designed for educational purposes to illustrate fundamental programming techniques.

Uploaded by

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

Assignment 2

The document contains multiple Java programs that demonstrate various programming concepts, including temperature conversion, prime number generation, bubble sort, finding the largest and smallest elements in an array, Fibonacci series generation, string reversal, digit sum calculation, palindrome checking, matrix multiplication, and diamond pattern printing. Each program includes user input and outputs the results accordingly. The programs are designed for educational purposes to illustrate fundamental programming techniques.

Uploaded by

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

Assignment 2

1.Temperature Conversion Program. Name: - Atharv Kailas Jadhav


import java.util.Scanner; Roll No: - CO2138 Div: - B
public class TemperatureConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter temperature: ");
double temp = sc.nextDouble();
System.out.println("Convert to (C/F): ");
char type = sc.next().charAt(0);
if (type == 'C' || type == 'c') {
double celsius = (temp - 32) * 5 / 9;
System.out.println("Temperature in Celsius: " + celsius);
} else if (type == 'F' || type == 'f') {
double fahrenheit = (temp * 9 / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
} else {
System.out.println("Invalid choice.");
} sc.close();
}}

2.
import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number N: ");
int n = sc.nextInt();
System.out.println("Prime numbers up to " + n + ":");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}}
sc.close();
}
public static boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}

3.Sorting an Array using Bubble Sort


import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} }}
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
sc.close();
}}

4. Find Largest and Smallest Element in an Array


import java.util.Scanner;

public class MinMaxArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];

System.out.println("Enter " + n + " numbers:");


for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

int min = arr[0], max = arr[0];


for (int num : arr) {
if (num < min) min = num;
if (num > max) max = num;
}
System.out.println("Smallest: " + min);
System.out.println("Largest: " + max);
sc.close();
}}
5.Fibonacci Series
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();

int a = 0, b = 1;
System.out.print("Fibonacci series: " + a + " " + b + " ");
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(next + " ");
a = b;
b = next;
}
sc.close();
}
}

6. Reverse a String Without Built-in Methods


import java.util.Scanner;

public class ReverseString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.next();
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println("Reversed string: " + rev);
sc.close();
}
}

7. Sum of Digits of a Large Number


import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
String num = sc.next();
int sum = 0;
for (char digit : num.toCharArray()) {
sum += Character.getNumericValue(digit);
}
System.out.println("Sum of digits: " + sum);
sc.close();
}
}

8. Palindrome Check
import java.util.Scanner;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string/number: ");
String str = sc.next();
String rev = new StringBuilder(str).reverse().toString();

if (str.equals(rev)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
sc.close();
}}

9.Matrix multiplication
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter rows and columns of first matrix (M x N): ");


int m = sc.nextInt();
int n = sc.nextInt();

System.out.print("Enter rows and columns of second matrix (N x P): ");


int n2 = sc.nextInt();
int p = sc.nextInt();

if (n != n2) {
System.out.println("Matrix multiplication not possible! Columns of first matrix must equal rows of second
matrix.");
return;
}

int[][] mat1 = new int[m][n];


int[][] mat2 = new int[n][p];
int[][] result = new int[m][p];

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


for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();

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


for (int i = 0; i < n; i++)
for (int j = 0; j < p; j++)
mat2[i][j] = sc.nextInt();
// Matrix Multiplication
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

System.out.println("Resultant Matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}

10.Dimond pattern Printing


import java.util.Scanner;

public class DiamondPattern {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows for half diamond: ");
int rows = sc.nextInt();

// Upper half
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++)
System.out.print(" ");
for (int j = 1; j <= (2 * i - 1); j++)
System.out.print("*");
System.out.println();
}

// Lower half
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--)
System.out.print(" ");
for (int j = 1; j <= (2 * i - 1); j++)
System.out.print("*");
System.out.println();
}
sc.close();
}
}

You might also like