Skip to document

Java Labcycle 3 - Simple(Basic) Java Programs with outputs for each.

Simple(Basic) Java Programs with outputs for each.
Course

Bachelor of Computer Applications (BCA2020)

999+ Documents
Students shared 2571 documents in this course
Academic year: 2020/2021
Uploaded by:
0followers
84Uploads
105upvotes

Comments

Please sign in or register to post comments.

Preview text

LAB CYCLE 3

Question 1 Write a java program to accept the marks of a student into a 1D array and find total marks and percentage. Aim Find a java program to accept marks of astudent and calculate total mark and percentage using 1D array Program

import java.util; public class Array{ public static void main(String[] args)throws Exception{ int marks[]=new int[6]; float total_mark=0; final int grand_total=600; float percentage; Scanner in=new Scanner(System); System.out("Enter your marks: "); for(int i=0;i<6;i++){ marks[i]=in(); total_mark=total_mark+marks[i]; } percentage=total_mark/grand_total*100; System.out("\nObtained Marks = "); for(int i=0;i<6;i++){ System.out(marks[i]+" "); } System.out("\n\nTotal mark out of "+grand_total+" = " + total_mark); System.out("\nPercentage= "+percentage); } }

Output

Output

Question 3 Write a java program to find largest and smallest number from an array and print its their locations. Aim Find a java program to find the largest and smallest number in an array. Program import java.util; public class LargeSmall{ public static void main(String[] args)throws Exception{ int n,i,large,small; Scanner in =new Scanner(System); System.out("\nEnter the number of elements in the array: "); n=in(); int array[]=new int[n]; System.out("Enter the elements in array: "); for(i=0;i<n;i++){ array[i]=in(); } large=array[0]; for(i=1;i<n;i++){ if(array[i]>large){ large=array[i]; } } small=array[0]; for(i=1;i<n;i++){ if(array[i]<small){ small=array[i]; } } System.out("\nArray elements are : "); for(i=0;i<n;i++){ System.out(array[i]+" "); }

Output

Question 4 Write a java program to sort ‘n’ numbers. Aim Find a java program to sort ‘n’ numbers. Program

import java.util; public class Sort{ public static void main(String[] args)throws Exception{ int n; Scanner input=new Scanner(System); System.out("Enter size of array: "); n=input(); int array[]=new int[n]; System.out("Enter the elements of array: "); for(int i=0;i<n;i++){ array[i]=input(); } System.out("\nThe entered array is : "); for(int i=0;i<n;i++){ System.out(array[i]+ " "); } //ascending orderly sorted

for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(array[i]>array[j]){ int temp; temp=array[i]; array[i]=array[j]; array[j]=temp; } } } System.out("\n\nAscending Orderly Sorted array is : ");

Output

Question 5 Write a menu-driven program to add and mautiply two matrices. Aim Obtain a java program to add and mutiply two matrices through menu-driven. Program

import java.util; public class MenuDriven{ public static void main(String[] args)throws Exception{ int r1,c1,r2,c2; Scanner in=new Scanner(System); System.out("Enter the number of rows and columns of first matrix: "); r1=in(); c1=in(); System.out("Enter the number of rows and columns of second matrix: "); r2=in(); c2=in(); int m1[][]=new int[r1][c1]; int m2[][]=new int[r2][c2]; System.out("Enter elements in matrix 1: "); for(int i=0;i<r1;i++){ for(int j=0;j<c1;j++){ m1[i][j]=in(); } } System.out("Enter elements in matrix 2: "); for(int i=0;i<r2;i++){ for(int j=0;j<c2;j++){ m2[i][j]=in(); } } System.out("Matrix 1\n********");

for(int i=0;i<r1;i++){ for(int j=0;j<c1;j++){ System.out(sum[i][j]+" "); } System.out(" "); } } break; case 2: if(c1!=r2){ System.out("Matrix multiplication won't be possible...!\n"); } else{ for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ for(int k=0;k<c1;k++){ product[i][j]=product[i][j]+m1[i][k]m2[k][j]; } } } System.out("\nProduct\n******"); for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ System.out(product[i][j]+" "); } System.out(" "); } } break; default: System.out("Invalid choice...!"); return; } System.out("\nDo you want to continue(yes(1)/no(0)): ");

c=in(); }while(c==1); } }

Output

Question Write a program to check whether a matrix is symmetric or not. Aim Find a java program to check whether a matrix is symmetric or not. Program import java.util; public class Symmetric{ public static void main(String[] args)throws Exception{ int r,c; Scanner in=new Scanner(System); System.out("Enter number of rows and columns of matrix: "); r=in(); c=in(); if(r==c){ int matrix[][]=new int[r][c]; System.out("Enter the elements of matrix: "); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ matrix[i][j]=in(); } } int transpose[][]=new int[c][r]; for(int i=0;i<c;i++){ for(int j=0;j<r;j++){ transpose[i][j]=matrix[j][i]; } } System.out("\nMatrix\n======"); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ System.out(matrix[i][j]+" "); } System.out(" "); } System.out("\nTranspose\n=========");

Output

Was this document helpful?

Java Labcycle 3 - Simple(Basic) Java Programs with outputs for each.

Course: Bachelor of Computer Applications (BCA2020)

999+ Documents
Students shared 2571 documents in this course
Was this document helpful?
LAB CYCLE 3
Question 1
Write a java program to accept the marks of a student into a 1D array and
find total marks and percentage.
Aim
Find a java program to accept marks of astudent and calculate total mark
and percentage using 1D array
Program
import java.util.Scanner;
public class Array{
public static void main(String[] args)throws Exception{
int marks[]=new int[6];
float total_mark=0;
final int grand_total=600;
float percentage;
Scanner in=new Scanner(System.in);
System.out.println("Enter your marks: ");
for(int i=0;i<6;i++){
marks[i]=in.nextInt();
total_mark=total_mark+marks[i];
}
percentage=total_mark/grand_total*100;
System.out.print("\nObtained Marks = ");
for(int i=0;i<6;i++){
System.out.print(marks[i]+" ");
}
System.out.println("\n\nTotal mark out of "+grand_total+" = "
+ total_mark);
System.out.println("\nPercentage= "+percentage);
}
}