[go: up one dir, main page]

0% found this document useful (0 votes)
143 views8 pages

Java Programming Assignments

The document provides details of a student named Noman Khan enrolled in a coding batch. It includes their personal details, assignments submitted which involve writing pseudocode and programs for various algorithms, designing flowcharts and analyzing algorithm efficiency using Big O notation.

Uploaded by

Sai Kiran
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)
143 views8 pages

Java Programming Assignments

The document provides details of a student named Noman Khan enrolled in a coding batch. It includes their personal details, assignments submitted which involve writing pseudocode and programs for various algorithms, designing flowcharts and analyzing algorithm efficiency using Big O notation.

Uploaded by

Sai Kiran
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/ 8

Name : Noman Khan

Email: nomankhan.bca@gmail.com

Mobile:9699359336

Batch: PCAT- Java batch 8

Superset I’d: 3689096

Resume no: 26180072

Cloud I’d : 24NAG0528-01

Assignment 1: Pseudocode Development - Task: Write a detailed pseudocode for a simple program
that takes a number as input, calculates the square if it's even or the cube if it's odd, and then outputs
the result. Incorporate conditional and looping constructs.
Pseudocode:
1. Start
2. Prompt the user to enter a number and store it in a variable 'num'
3. Check if 'num' is even or odd
4. If 'num' is even:
4.1. Calculate the square of 'num' and store the result in a variable 'result'
4.2. Output "The square of the number is: result"
5. If 'num' is odd:
5.1. Calculate the cube of 'num' and store the result in a variable 'result'
5.2. Output "The cube of the number is: result"
6. End

Program In Java:
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int num = scanner.nextInt();

int result; // Variable to store the result

// Check if the number is even


if (num % 2 == 0) {
// Calculate the square of the number
result = num * num;
System.out.println("The square of the number is: " + result);
} else { // If the number is odd
// Calculate the cube of the number
result = num * num * num;
System.out.println("The cube of the number is: " + result);
}

scanner.close();
}
}

Assignment 2: Flowchart Creation - Design a flowchart that outlines the logic for a user login process.
It should include conditional paths for successful and unsuccessful login attempts, and a loop that
allows a user three attempts before locking the account.
+-------------------------------+
| Start: User Login |
+-------------------------------+
|
v
+-----------------------------+
| Enter Username and |
| Password |
+-----------------------------+
|
v
+-----------------------------+
| Attempt to Authenticate |
| Username and Password |
+-----------------------------+
| |
| Successful | Unsuccessful
v v
+-----------------------------------+
| Display Successful Login |
| Message and Proceed |
+-----------------------------------+
|
v
+-------------------------------+
| End |
+-------------------------------+

|
v
+-------------------+
| Display Error |
| Message |
+-------------------+
|
v
+-------------------+
| Retry Login |
| (Loop Counter) |
+-------------------+
|
v
+-------------------+
| Is Loop Counter |
| Reached 3? |
+-------------------+
|
+----------|----------+
| | |
| YES |
| | |
| v |
| Lock Account |
+----------+----------+
|
v
+-------------------+
| Display Locked |
| Account Message |
+-------------------+
|
v
+-------------------+
| End |
+-------------------+

Assignment 3: Function Design and Modularization - Create a document that describes the design of
two modular functions: one that returns the factorial of a number, and another that calculates the nth
Fibonacci number. Include pseudocode and a brief explanation of how modularity in programming
helps with code reuse and organization."

Pseudocode: 1. Function to Calculate Factorial

public class FactorialCalculator {


// Function to calculate factorial of a number
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}

2. Function to Calculate nth Fibonacci Number

public class FibonacciCalculator {

// Function to calculate nth Fibonacci number


public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

Assignment 4: Pseudocode and Flowchart for Sorting Algorithm - Write pseudocode and create a
flowchart for a bubble sort algorithm. Provide a brief explanation of how the algorithm works and a
simple array of integers to demonstrate a dry run of your algorithm.

Pseudocode:
1.Start with an array A of items to be sorted.
2.Set n as the length of the array.
3.Repeat the following steps until no swaps are needed:
I.Set swapped to false.
II.Iterate through the array from the beginning to the second-to-last element.
III.Compare each pair of adjacent elements.
IV.If they are in the wrong order (i.e., the preceding element is greater than the succeeding one),
swap them and set swapped to true.

V.Decrease n by 1 (since after each Pass, the largest unsorted element settles at the end).

4.Continue this process until no swaps are made in a complete pass through the array, indicating that
the array is sorted. function bubbleSort(arr):
n = length of arr
for i from 0 to n-1:
for j from 0 to n-i-1:
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])

FlowChart:
+---------------------------+
| Start: Bubble Sort |
+---------------------------+
|
v
+---------------------------+
| Initialize variables: |
| n = length of array |
+---------------------------+
|
v
+-------------------------------------+
| for i from 0 to n-1: |
| for j from 0 to n-i-1: |
| if arr[j] > arr[j+1]: |
| swap(arr[j], arr[j+1])|
+-------------------------------------+
|
v
+---------------------------+
| End: Sorted Array |
+---------------------------+

Assignment 5: Recursive Function and Efficiency Analysis - Write a recursive function pseudocode
and calculate the nth Fibonacci number and use Big O notation to analyze its efficiency. Compare this
with an iterative approach and discuss the pros and cons in terms of space and time complexity."

Pseudocode:
function fibonacciRecursive(n):
if n is 0:
return 0
else if n is 1:
return 1
else:
return fibonacciRecursive(n-1) + fibonacciRecursive(n-2)

function fibonacciIterative(n):
if n is 0:
return 0
else if n is 1:
return 1
else:
prev = 0
curr = 1
for i from 2 to n:
next = prev + curr
prev = curr
curr = next
return curr

You might also like