0% found this document useful (0 votes)
37 views7 pages

Ce146 JT Lab 1

Uploaded by

dhyeyshah009
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)
37 views7 pages

Ce146 JT Lab 1

Uploaded by

dhyeyshah009
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
You are on page 1/ 7

Lab 1

Topics: print(), println(), Scanner class, 1-D, 2-D array, jagged array

Q1. Write a Java program to display “Hello World”.


Code :
class Lab1_1 {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World\n");

System.out.println("Hello");
System.out.println("World");
}
}

Output :
Q2. Write a Java program to print numbers between 1 to n which are divisible by 3, 5
and by both(3 and 5) by taking n as an input from the user.
Code :
import java.util.Scanner;
class Lab1_2 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter N : ");
int n = sc.nextInt();
System.out.println("Numbers Divisible By 3 :");
for(int i=1; i<=n; i++){
if(i % 3 == 0){
System.out.println(i);
}
}
System.out.println("Numbers Divisible By 5 :");
for(int i=1; i<=n; i++){
if(i % 5 == 0){
System.out.println(i);
}
}
System.out.println("Numbers Divisible By Both 3 and 5 :");
for(int i=1; i<=n; i++){
if(i % 3 == 0 && i % 5 == 0){
System.out.println(i);
}
}
}
}

Output :
Q3. Write a class named Greeter that prompts the user for his or her name, and then
prints a personalized greeting. As an example, if the user entered “Era”, the program
should respond “Hello Era!”.
Code :
import java.util.Scanner;
class Greeter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name : ");
String name = sc.nextLine();
System.out.println("Hello " + name + "!");
}
}

Output :
Q4. Write a Java program that takes Name, Roll No and marks of 5 subjects as input
and gives a formatted output as:
Name: ABCD
Roll No. : 1
Average: 84
Also display the grade (e.g. A, B, C…etc) using the average.
Code :
import java.util.Scanner;
class Lab1_4{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name : ");
String name = sc.nextLine();
System.out.print("Enter Roll No : ");
int rollno = sc.nextInt();
int marks[] = new int[5];
for(int i=0; i<5; i++){
System.out.print("Marks " + (i+1) + " : ");
marks[i] = sc.nextInt();
}
int sum = 0;
for(int i : marks){
sum += i;
}
int avg = sum/5;
System.out.println("---OUTPUT---");
System.out.println("Name : " + name);
System.out.println("Roll No : " + rollno);
System.out.println("Average : " + avg);
if(avg >= 85){
System.out.println("Grade : A");
} else if(avg >= 75 && avg <85){
System.out.println("Grade : B");
} else if(avg >= 65 && avg <75){
System.out.println("Grade : C");
} else if(avg >= 55 && avg <65){
System.out.println("Grade : D");
} else{
System.out.println("Grade : E");
}
}
}
Output :
Q5. Calculate and return the sum of all the even numbers present in the numbers array
passed to the method calculateSumOfEvenNumbers. Implement the logic inside
calculateSumOfEvenNumbers() method.
Test the functionalities using the main() method of the Tester class.
Sample Input and Output:
Sample Input Sample Output
{68,79,86,99,23,2,41,100} 256
{1,2,3,4,5,6,7,8,9,10} 30
Code :
class Lab1_5{
public static int calculateSumOfEvenNumbers(int arr[])
{
int sum=0;
for(int i : arr){
if(i % 2 == 0){
sum += i;
}
}
return sum;
}
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println(calculateSumOfEvenNumbers(arr));
}
}

Output :
Q6. Write a program to perform matrix addition and matrix multiplication on two
given matrices. Use for-each form of for loop to display the matrices.
Code :
public class Lab1_6 {

public static void displayMatrix(int[][] matrix) {


for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}

public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {


if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) {
System.out.println("Matrices cannot be added.");
return null;
}
int rows = matrix1.length;
int cols = matrix1[0].length;
int[][] result = new int[rows][cols];

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


for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return result;
}

public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {


if (matrix1[0].length != matrix2.length) {
System.out.println("Matrices cannot be multiplied.");
return null;
}
int rows = matrix1.length;
int cols = matrix2[0].length;
int[][] result = new int[rows][cols];

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


for (int j = 0; j < cols; j++) {
result[i][j] = 0;
for (int k = 0; k < matrix1[0].length; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}

public static void main(String[] args) {


int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};

System.out.println("Matrix 1:");
displayMatrix(matrix1);
System.out.println("Matrix 2:");
displayMatrix(matrix2);

int[][] sumMatrix = addMatrices(matrix1, matrix2);


System.out.println("Sum of Matrix 1 and Matrix 2:");
displayMatrix(sumMatrix);

int[][] productMatrix = multiplyMatrices(matrix1, matrix2);


System.out.println("Product of Matrix 1 and Matrix 2:");
displayMatrix(productMatrix);
}
}

Output :

You might also like