[go: up one dir, main page]

0% found this document useful (0 votes)
82 views12 pages

CSE 1007 - Java Programming

The document contains the code and output for 8 questions on Java programming assignments. It includes code to print patterns, find HCF and LCM, calculate student scores, sort arrays, remove duplicate elements, check for identity matrices, calculate row sums, and add matrices. The code uses concepts like loops, arrays, sorting, conditional statements, and matrix operations. Testing is done by taking user input and printing results.

Uploaded by

Naman Sood
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)
82 views12 pages

CSE 1007 - Java Programming

The document contains the code and output for 8 questions on Java programming assignments. It includes code to print patterns, find HCF and LCM, calculate student scores, sort arrays, remove duplicate elements, check for identity matrices, calculate row sums, and add matrices. The code uses concepts like loops, arrays, sorting, conditional statements, and matrix operations. Testing is done by taking user input and printing results.

Uploaded by

Naman Sood
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/ 12

CSE 1007 – Java Programming

Name – Naman Sood


Registration Number – 18BCI0168
Slot - L53+L54
Faculty – Prof. JABANJALIN HILDA J
LAB Assignment – 2
Q1. (a)
Code:
import java.util.Scanner;

public class pattern1 {


public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to print: ");
n = sc.nextInt();
for(int i=0;i<=n;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output:
Q1. (b)
Code:
import java.util.Scanner;

public class pattern2 {


public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows of pattern: ");
n = sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j==1||j==n||i==n/2 && j!=n/2+1 || i==n/2+1 || i==n/2+1
|| i==n/2+2 && j!=n/2+1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Output:
Q1. (c)
Code:
import java.util.Scanner;

public class pattern3 {


public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the max number for pattern: ");
n = sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(j+1+" ");
}
System.out.println();
}
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<=i-1;j++)
{
System.out.print(j+1+" ");
}
System.out.println();
}
}
}

Output:
Q2.
Code
import java.sql.SQLOutput;
import java.util.Scanner;

public class hcflcm {


public static void main(String args[])
{
int a, b, res=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
a = sc.nextInt();
System.out.println("Enter second number: ");
b = sc.nextInt();
//HCF code
for(int i=1;i<=a || i<=b;i++)
{
if(a%i==0 && b%i==0)
{
res = i;
}
}
//LCM code
int lcm = (a*b)/res;
System.out.println("HCF of the numbers is "+res);
System.out.println("LCM of the numbers is "+lcm);
}
}

Output:
Q3.
Code
import java.util.Scanner;
public class marks {
public static void main(String args[]){
int math, physics, chem, eng, cs;
Scanner sc = new Scanner(System.in);
System.out.println("Enter math marks: ");
math = sc.nextInt();
System.out.println("Enter physics marks: ");
physics = sc.nextInt();
System.out.println("Enter chemistry marks: ");
chem = sc.nextInt();
System.out.println("Enter english marks: ");
eng = sc.nextInt();
System.out.println("Enter computer science marks: ");
cs = sc.nextInt();
float overall_avg = (math+physics+chem+eng+cs)/5;
float eng_av = (math*math)+physics+(chem/4);
float csa = cs;

if(overall_avg>75 && eng_av>csa)


{
System.out.println("Probable Mech, Civil, EEE, ECE candidate");
}
else if(overall_avg>75 && csa>eng_av)
{
System.out.println("Probable CSE, IT, IS candidate");
}
else if(overall_avg<75 && csa>eng_av)
{
System.out.println("Probable BCA candidate");
}
else if(overall_avg<75 && csa<eng_av)
{
System.out.println("Probable BSc candidate");}}}
Output:
Q4.
Code
import java.util.Scanner;

public class arr_sort {


static void bub_sort(int[] arr, int n)
{
int f = 0;
for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(arr[j-1]>arr[j])
{
f = arr[j-1];
arr[j-1] = arr[j];
arr[j] = f;
}
}
}
}
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of elements in array: ");
n=sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
bub_sort(arr, n);
System.out.println("Sorted array: ");
for(int i=0;i<n;i++)
{
System.out.print(arr[i] + " ");
}
}
}
Output:
Q5.
Code:
import java.util.Arrays;
import java.util.Scanner;

public class duplicate_arr {


public static void main(String args[])
{
System.out.println("Enter the number of elements: ");
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int j=0;
for(int i=0;i<n-1;i++)
{ if(arr[i]!=arr[i+1])
{
arr[j++]=arr[i];
}
}
arr[j++] = arr[n-1];
System.out.println("Array elements are: ");
for(int i=0;i<j;i++)
{
System.out.print(arr[i]+" ");
}
}
}

Output:
Q6.
Code
import java.util.Scanner;

public class identity {


public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of square matrix: ");
int n = sc.nextInt();
int a[][] = new int[n][n];
System.out.println("Enter the elements of the identity matrix:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The matrix entered: ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int counter=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((i==j && a[i][j]!=1) || (i!=j && a[i][j]!=0))
{
counter=1;
break;
}
}
}
if(counter==1)
{
System.out.println("The matrix entered is not an identity
matrix");
}
else
{
System.out.println("The matrix entered is an identity matrix");
}
}
}
Output:

Q7.
Code:
import java.util.Scanner;

public class row_sum {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int m = sc.nextInt();
System.out.println("Enter the number of columns: ");
int n = sc.nextInt();
int a[][] = new int[m][n];
System.out.println("Enter elements for matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Matrix:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int result;
for(int i=0;i<m;i++)
{
result = 0;
for(int j=0;j<n;j++)
{
result = result+a[i][j];
}
System.out.println("Sum of "+"row "+(i+1)+" is: "+result);
}
}
}

Output:
Q8.
Code:
import java.util.Scanner;
public class matrix_addition {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int m = sc.nextInt();
System.out.println("Enter the number of columns: ");
int n = sc.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println("Enter elements for matrix A ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements for matrix B ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("Matrix A: ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix B: ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println("The addition of the matrices give us: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j] = a[i][j]+b[i][j];
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}}
Output:

You might also like