SCHOOL OF COMPUTING
Department of Computer Science and Engineering
, SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE ANDENGINEERING
BONAFIDE CERTIFICATE
Bonafide record of work done by of
_ in during even/odd semester in
academic year
Staff In-charge Head of the Department
Submitted to the practical Examination held at Kalasalingam University, Krishnankoil on
REGISTER
NUMBER
Internal Examiner External Examiner
, EXPERIMENT EVALUATION SUMMARY
Name: Reg No:
Class: Faculty:
Marks Faculty
S.No Date Experiment (10) Signature
1
2
3
4
5
6
7
8
9
10
11
12
, Ex-1: Basic java Programs
1.Aim: To write a java program to find Mean and Standard deviation.
Algorithm:
Step 1 - START
Step 2 - Declare a double array namely input_array, two doube values namely sum and
standard_deviation.
Step 3 - Read the required values from the user/ define the values.
Step 4 - Compute ∑(Xi - ų)2 / N and store the value in result variable.
Step 5 - Display the result
Step 6 – Stop
Source code:
public class StandardDeviation {
public static void main(String[] args) {
double[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double SD = calculateSD(numArray);
System.out.format("Standard Deviation = %.6f",SD);
}
public static double calculateSD(double numArray[]){
double sum = 0.0, standardDeviation = 0.0;
int length = numArray.length;
for(double num : numArray) {
sum += num; }
double mean = sum/length;
for(double num: numArray) {
standardDeviation += Math.pow(num - mean, 2); }
return Math.sqrt(standardDeviation/length);
, }
}
Output:
Standard Deviation=2.872281
Algorithm(Mean):
Step1:Take an integer set A of n values.
Step2:Add all values of A together.
Step3:Divide result of Step 2 by n.
Step4:The result is mean of A's values.
Step5:end of the program.
Source Code:
public class CaculatingMean {
public static void main(String args[]){
float mean;
int sum, i;
int n = 5;
int a[] = {2,6,7,4,9};
sum = 0;
for(i = 0; i < n; i++)
{
sum+=a[i];
}
System.out.println("Mean ::"+ sum/(float)n); }
}
Output:
C:\Users\u\OneDrive\disktop>javac CaculatingMean.java
C:\Users\u\OneDrive\disktop>java CaculatingMean
Mean ::5.6
,2.Aim: write a java program to print the given pattern(number and character).
Algorithm:
Step1:Read the value of n.
Step2:Declare the pattern what we want to print using 2 for loops.
Step3:display the same pattern.
Step4:Stop the program.
Source Code:
class Characterpattern
{
public static void pyramidPattern(int n) {
for (int i=0; i<n; i++) {
for (int j=n-i; j>1; j--) {
System.out.print(" "); }
for (int j=0; j<=i; j++ ) {
System.out.print("* "); }
System.out.println(); }
}
public static void main(String args[])
{
int n = 5;
pyramidPattern(n);
Output:
C:\Users\u\OneDrive\disktop>javac Characterpattern.java
C:\Users\u\OneDrive\disktop>java Characterpattern
, *
**
***
****
*****
Algorithm:
Step1:Read the value of n.
Step2:Declare the pattern what we want to print using 2 for loops.
Step3:display the same pattern.
Step4:Stop the program.
Source Code:
public class Pyramidnumber
{
public static void main(String[] args){
int rows = 6;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j < i; j++){
System.out.print(" ");}
for (int j = i; j <= rows; j++) {
System.out.print(j+" "); }
System.out.println(); }
}
}
Output:
C:\Users\u\OneDrive\disktop>javac Pyramidnumber.java
,C:\Users\u\OneDrive\disktop>java Pyramidnumber
123456
23456
3456
456
56
6
3.Aim:write a java program to find the second and third maximum number in an array
Algorithm:
Step1:Read the values of array
Step2:First, iterate through the array and find maximum.
Step3:Store this as first maximum along with its index.
Step4:Now traverse the whole array finding the second max, excluding the maximum element.
Step5:Finally traverse the array the third time and find the third largest element i.e., excluding
the maximum and second maximum.
Source Code:
public class Array
{
public void getMax( double ar[] )
{
double max1 = ar[0];
double max2 = ar[0];
double max3 = ar[0];
int ZERO = 0;
for( int i = 0; i < ar.length; i++ )
{
if( ar[i] >= max1){
max1 = ar[i];
ZERO = i;
, }
}
ar[ZERO] = 0;
for( int j = 0; j < ar.length; j++ ){ if( ar[j] >= max2 )
{
max2 = ar[j];
ZERO = j;
}
}
ar[ZERO] = 0;
for( int k = 0; k < ar.length; k++ )
{
if( ar[k] >= max3 )
{
max3 = ar[k];
ZERO = k;
}
}
System.out.println("1st max:" + max1 + ", 2nd: " +max2 + ",3rd: "+ max3);
}
public static void main(String[] args){
Array array = new Array();
double a[] = {2.2, 3.4, 5.5, 5.5, 6.6, 5.6};
array.getMax( a ); }
}
Output:
C:\Users\u\OneDrive\disktop>javac Array.java
, C:\Users\u\OneDrive\disktop>java Array
1st max:6.6, 2nd: 5.6,3rd: 5.5
4.Aim: write a java program to find ncr,npr
Algorithm:
Step1:Read the values of n and r.
Step2:calculate the factorial of n ,rand n- r.
Step3:calculate the ncr using the formula (fact(n)) / (fact(r)*fact(n-r)) store the result in ncr.
Step4:Display the ncr.
Step5:stop the program.
Source Code:
import java.util.Scanner;
public class CodesCracker{
public static void main(String[] args) {
int n, r, ncr;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Value of n: ");
n = s.nextInt();
System.out.print("Enter the Value of r: ");
r = s.nextInt();
ncr = (fact(n)) / (fact(r)*fact(n-r));
System.out.println("\nnCr = " +ncr);
}
public static int fact(int num)
{
int fact=1;
Department of Computer Science and Engineering
, SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE ANDENGINEERING
BONAFIDE CERTIFICATE
Bonafide record of work done by of
_ in during even/odd semester in
academic year
Staff In-charge Head of the Department
Submitted to the practical Examination held at Kalasalingam University, Krishnankoil on
REGISTER
NUMBER
Internal Examiner External Examiner
, EXPERIMENT EVALUATION SUMMARY
Name: Reg No:
Class: Faculty:
Marks Faculty
S.No Date Experiment (10) Signature
1
2
3
4
5
6
7
8
9
10
11
12
, Ex-1: Basic java Programs
1.Aim: To write a java program to find Mean and Standard deviation.
Algorithm:
Step 1 - START
Step 2 - Declare a double array namely input_array, two doube values namely sum and
standard_deviation.
Step 3 - Read the required values from the user/ define the values.
Step 4 - Compute ∑(Xi - ų)2 / N and store the value in result variable.
Step 5 - Display the result
Step 6 – Stop
Source code:
public class StandardDeviation {
public static void main(String[] args) {
double[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double SD = calculateSD(numArray);
System.out.format("Standard Deviation = %.6f",SD);
}
public static double calculateSD(double numArray[]){
double sum = 0.0, standardDeviation = 0.0;
int length = numArray.length;
for(double num : numArray) {
sum += num; }
double mean = sum/length;
for(double num: numArray) {
standardDeviation += Math.pow(num - mean, 2); }
return Math.sqrt(standardDeviation/length);
, }
}
Output:
Standard Deviation=2.872281
Algorithm(Mean):
Step1:Take an integer set A of n values.
Step2:Add all values of A together.
Step3:Divide result of Step 2 by n.
Step4:The result is mean of A's values.
Step5:end of the program.
Source Code:
public class CaculatingMean {
public static void main(String args[]){
float mean;
int sum, i;
int n = 5;
int a[] = {2,6,7,4,9};
sum = 0;
for(i = 0; i < n; i++)
{
sum+=a[i];
}
System.out.println("Mean ::"+ sum/(float)n); }
}
Output:
C:\Users\u\OneDrive\disktop>javac CaculatingMean.java
C:\Users\u\OneDrive\disktop>java CaculatingMean
Mean ::5.6
,2.Aim: write a java program to print the given pattern(number and character).
Algorithm:
Step1:Read the value of n.
Step2:Declare the pattern what we want to print using 2 for loops.
Step3:display the same pattern.
Step4:Stop the program.
Source Code:
class Characterpattern
{
public static void pyramidPattern(int n) {
for (int i=0; i<n; i++) {
for (int j=n-i; j>1; j--) {
System.out.print(" "); }
for (int j=0; j<=i; j++ ) {
System.out.print("* "); }
System.out.println(); }
}
public static void main(String args[])
{
int n = 5;
pyramidPattern(n);
Output:
C:\Users\u\OneDrive\disktop>javac Characterpattern.java
C:\Users\u\OneDrive\disktop>java Characterpattern
, *
**
***
****
*****
Algorithm:
Step1:Read the value of n.
Step2:Declare the pattern what we want to print using 2 for loops.
Step3:display the same pattern.
Step4:Stop the program.
Source Code:
public class Pyramidnumber
{
public static void main(String[] args){
int rows = 6;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j < i; j++){
System.out.print(" ");}
for (int j = i; j <= rows; j++) {
System.out.print(j+" "); }
System.out.println(); }
}
}
Output:
C:\Users\u\OneDrive\disktop>javac Pyramidnumber.java
,C:\Users\u\OneDrive\disktop>java Pyramidnumber
123456
23456
3456
456
56
6
3.Aim:write a java program to find the second and third maximum number in an array
Algorithm:
Step1:Read the values of array
Step2:First, iterate through the array and find maximum.
Step3:Store this as first maximum along with its index.
Step4:Now traverse the whole array finding the second max, excluding the maximum element.
Step5:Finally traverse the array the third time and find the third largest element i.e., excluding
the maximum and second maximum.
Source Code:
public class Array
{
public void getMax( double ar[] )
{
double max1 = ar[0];
double max2 = ar[0];
double max3 = ar[0];
int ZERO = 0;
for( int i = 0; i < ar.length; i++ )
{
if( ar[i] >= max1){
max1 = ar[i];
ZERO = i;
, }
}
ar[ZERO] = 0;
for( int j = 0; j < ar.length; j++ ){ if( ar[j] >= max2 )
{
max2 = ar[j];
ZERO = j;
}
}
ar[ZERO] = 0;
for( int k = 0; k < ar.length; k++ )
{
if( ar[k] >= max3 )
{
max3 = ar[k];
ZERO = k;
}
}
System.out.println("1st max:" + max1 + ", 2nd: " +max2 + ",3rd: "+ max3);
}
public static void main(String[] args){
Array array = new Array();
double a[] = {2.2, 3.4, 5.5, 5.5, 6.6, 5.6};
array.getMax( a ); }
}
Output:
C:\Users\u\OneDrive\disktop>javac Array.java
, C:\Users\u\OneDrive\disktop>java Array
1st max:6.6, 2nd: 5.6,3rd: 5.5
4.Aim: write a java program to find ncr,npr
Algorithm:
Step1:Read the values of n and r.
Step2:calculate the factorial of n ,rand n- r.
Step3:calculate the ncr using the formula (fact(n)) / (fact(r)*fact(n-r)) store the result in ncr.
Step4:Display the ncr.
Step5:stop the program.
Source Code:
import java.util.Scanner;
public class CodesCracker{
public static void main(String[] args) {
int n, r, ncr;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Value of n: ");
n = s.nextInt();
System.out.print("Enter the Value of r: ");
r = s.nextInt();
ncr = (fact(n)) / (fact(r)*fact(n-r));
System.out.println("\nnCr = " +ncr);
}
public static int fact(int num)
{
int fact=1;