Ce127 JT Lab 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

LAB 1

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

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


class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
2. 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.
import java.util.Scanner;
class Main{
public static void main(String[] args) {
System.out.println("Enter the number");
Scanner meet=new Scanner(System.in);
int a=0;
int b=0;
int c=0;
int n=meet.nextInt();
// System.out.println(n);
int arr1[]=new int[n];
int arr2[]=new int[n];
int arr3[]=new int[n];
for(int i=1;i<=n;i++)
{
if (i%3==0)
{

arr1[a]=i;
a++;
}
if (i%5==0)
{

arr2[b]=i;
b++;
}
if (i%3==0 && i%5==0)
{

arr3[c]=i;
c++;
}
}

for(int j=0;j<a;j++)
{
System.out.print(arr1[j]+" ");
}System.out.println("is divisable by three");

for(int j=0;j<b;j++)
{
System.out.print(arr2[j]+" ");
}System.out.println(" is divisable by five");

for(int j=0;j<c;j++)
{
System.out.print(arr3[j]+" ");
} System.out.println( " is divisable by three & five");

}
}

3. 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!".
import java.util.Scanner;
public class greeter
{
public static void main(String args[])
{
System.out.println("Enter Your name");
Scanner meet=new Scanner(System.in);
String name=meet.nextLine();
System.out.println("Hello "+name+"!");
}
}

4. 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.

import java.util.Scanner;
public class my {
public static void main (String args[])
{
System.out.println("Name, Roll No and marks of 5 subjects ");
Scanner meet=new Scanner(System.in);
String name= meet.nextLine();
int rno=meet.nextInt();
int arr[]=new int[5];
float sum=0;
for(int i=0;i<=4;i++)
{
arr[i]=meet.nextInt();
sum=sum+arr[i];
}

float avg =sum/5;

System.out.println("Name :" + name);


System.out.println("Roll No. :"+ rno );
System.out.println("Average :" + avg);
}
}
5. 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

import java.util.*;
class Meet
{
public int calculateSumOfEvenNumbers (int a[])
{
int sum = 0;
for (int i:a)
{
if (i % 2 == 0)
{
sum += i;
}
}
return sum;
}

public static void main (String[]args)


{
Scanner sc = new Scanner (System.in);

int n = sc.nextInt ();


int a[] = new int[n];
for (int i = 0; i < n; i++)
{

a[i] = sc.nextInt ();


}
Meet ans = new Meet ();
System.out.println (ans.calculateSumOfEvenNumbers (a));
}
}

java -cp /tmp/ZOOxoMhUm4 Meet


4
1234
6
6. 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.

import java.util.*;
class prog_6
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
System.out.print ("enter no of row and coln in matrix : ");
int n = sc.nextInt ();
int a[][] = new int[n][n];
System.out.println ("enter data for first matrix: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt ();
}
}
int b[][] = new int[n][n];
System.out.println ("enter data for second matrix : ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt ();
}
}
int add[][] = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
add[i][j] = a[i][j] + b[i][j];
}
}

int mul[][] = new int[n][n];


for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
mul[i][j] = 0;
for (int k = 0; k < n; k++)
{
mul[i][j] += (a[i][k] * b[k][j]);
}
}
}
System.out.println ("addition of two matrix: ");
for (int temp1[]:add)
{
for (int temp2:temp1)
{
System.out.print (temp2 + " ");
//System.out.println();
}
System.out.println ();
}
System.out.println ("multiplication of given two matrix: ");
for (int temp1[]:mul)
{
for (int temp2:temp1)
{
System.out.print (temp2 + " ");
}
System.out.println ();
}
}
}

You might also like