0% found this document useful (0 votes)
3 views10 pages

Aldorithm and Code

The document outlines the differences between primitive and non-primitive data types, highlighting that primitive types hold values directly while non-primitive types store references to objects. It also provides algorithms and Java code for calculating variance and standard deviation from a set of integers, detailing steps for initialization, summation, mean calculation, and output. Additionally, it includes a brief mention of the factorial calculation process.

Uploaded by

udochukwutiago84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Aldorithm and Code

The document outlines the differences between primitive and non-primitive data types, highlighting that primitive types hold values directly while non-primitive types store references to objects. It also provides algorithms and Java code for calculating variance and standard deviation from a set of integers, detailing steps for initialization, summation, mean calculation, and output. Additionally, it includes a brief mention of the factorial calculation process.

Uploaded by

udochukwutiago84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DIFFERENCES BETWEEN PRIMITIVE AND A NON-PRIMITIVE DATA TYPE

PRIMITIVE NON-PRIMITIVE

These are the basic built-in data types These are custom or reference types created
provided by the programming language. using classes, arrays, or interfaces.

A primitive data type directly holds the value A non-primitive data type stores a reference
assigned to it (e.g., an int directly holds an (memory address) to the actual object rather
integer value). than the value itself.

The memory used is highly efficient and Objects may also have additional overhead
optimized. due to their reference nature.

ALGORITHM FOR VARIANCE

Input: A set of integers x = {x₁, x₂, ..., xₙ}


Output: The variance of the set

Step 0: Initialization

 Let sum = 0

 Let count = 0

Step 1: Calculate the Sum and Count


 For each number x in the set:

o Add x to sum

o Increase count by 1

Step 2: Calculate the Mean

 mean = sum / count

Step 3: Calculate the Squared Differences

 Initialize squared_diff_sum = 0

 For each number x in the set:

o diff = x - mean

o diff_squared = diff × diff

o Add diff_squared to squared_diff_sum


Step 4: Calculate Variance

 variance = squared_diff_sum / count

Step 5: Output the Result

 Display the value of variance

This is my code

import java.util.Scanner;

public class VarianceCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Step 0: Input the set of numbers

System.out.print("Enter the number of elements: ");

int n = scanner.nextInt();

double[] numbers = new double[n];

System.out.println("Enter the numbers:");

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

numbers[i] = scanner.nextDouble();

// Step 1: Calculate sum and count

double sum = 0;
for (double x : numbers) {

sum += x;

// Step 2: Calculate mean

double mean = sum / n;

// Step 3: Calculate squared differences

double squaredDiffSum = 0;

for (double x : numbers) {

double diff = x - mean;

double diffSquared = diff * diff;

squaredDiffSum += diffSquared;

// Step 4: Calculate variance

double variance = squaredDiffSum / n;

// Step 5: Output result

System.out.println("The variance is: " + variance);

scanner.close();

}
This is the output

STANDARD DEVIATION

Step 0: Initialize

 Set sum = 0, count = 0

🔶 Step 1: Calculate sum and count

 For each number x in the set:

o Add x to sum

o Increase count by 1

🔶 Step 2: Calculate Mean

 mean = sum / count


🔶 Step 3: Calculate Total Squared Difference

 Set squared_diff_sum = 0

 For each number x:

o diff = x - mean

o squared = diff * diff

o Add squared to squared_diff_sum

🔶 Step 4: Calculate Variance

 variance = squared_diff_sum / count

🔶 Step 5: Calculate Standard Deviation

 std_dev = square root of variance

🔶 Step 6: Output

 Display std_dev
THIS IS MY CODE

import java.util.Scanner;

public class StandardDeviationCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input

System.out.print("Enter the number of values: ");

int n = scanner.nextInt();

double[] data = new double[n];

System.out.println("Enter the numbers:");

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

data[i] = scanner.nextDouble();

// Step 1: Find sum and mean

double sum = 0;

for (double num : data) {

sum += num;

double mean = sum / n;

// Step 2: Squared difference sum

double squaredDiffSum = 0;
for (double num : data) {

double diff = num - mean;

squaredDiffSum += diff * diff;

// Step 3: Variance and Standard Deviation

double variance = squaredDiffSum / n;

double stdDev = Math.sqrt(variance);

// Output

System.out.println("Standard Deviation: " + stdDev);

scanner.close();

THIS IS MY OUTPUT

FACTORIAL
Input: A positive integer n
Output: The factorial of n (i.e., n! = n × (n-1) × ... × 1)

🔶 Step 0: Initialize

 Let result = 1

 Let step = n

🔶 Step 1: Loop with a backward multiplier

 While step > 1:

o Multiply result by step

o Decrease step by 1

🔶 Step 2: Output

 Display the value of result


THIS IS MY CODE

THIS IS MY OUTPUT

Let’s go into the standard deviation code explanation now

You might also like