0% found this document useful (0 votes)
16 views

DSA ALL NOTES BEFORE MIDTERM

Uploaded by

romanhasansourab
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)
16 views

DSA ALL NOTES BEFORE MIDTERM

Uploaded by

romanhasansourab
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/ 4

BASIC JAVA PROGRAM

import java.util.Scanner;

public class DataTypesDemo {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting and taking input for an integer


System.out.print("Enter an integer: ");
int intValue = scanner.nextInt();

// Prompting and taking input for a float


System.out.print("Enter a float value: ");
float floatValue = scanner.nextFloat();

// Prompting and taking input for a double


System.out.print("Enter a double value: ");
double doubleValue = scanner.nextDouble();

// Prompting and taking input for a character


System.out.print("Enter a character: ");
char charValue = scanner.next().charAt(0);

// Clearing scanner buffer


scanner.nextLine(); // To consume the leftover newline

// Prompting and taking input for a string


System.out.print("Enter a string: ");
String stringValue = scanner.nextLine();

// Displaying the inputs back to the user


System.out.println("\nYou entered:");
System.out.println("Integer: " + intValue);
System.out.println("Float: " + floatValue);
System.out.println("Double: " + doubleValue);
System.out.println("Character: " + charValue);
System.out.println("String: " + stringValue);

// Closing the scanner


scanner.close();
}
}

EXPLATION: This Java program demonstrates the usage of various data types, including int, float, double,
char, and String. The program begins by importing the Scanner class, which is essential for taking user
inputs. Inside the main method, a Scanner object is created to facilitate reading data from the console. The
program then prompts the user to enter values for each data type in sequence.

An int data type is used to store whole numbers like 42. Next, a float variable is used for storing
single-precision decimal numbers, such as 3.14. The double data type, which allows for higher precision,
stores more accurate decimal values like 2.71828. The program then reads a single character using the
char data type, capturing a single letter or symbol, such as A. Finally, it uses the String data type to store a
sequence of characters, which could represent text input like "Hello, Java!".

After gathering all inputs, the program displays them back to the user, highlighting the values entered for
each data type. The scanner.nextLine() method is used strategically to clear the input buffer, ensuring the
program functions correctly when transitioning from numeric inputs to string inputs. Finally, the
scanner.close() method is called to release the resources used by the Scanner. This program is a
foundational example of handling different data types in Java, providing a clear understanding of how to
interact with various inputs and outputs effectively.

INPUT :

Enter an integer: 42
Enter a float value: 3.14
Enter a double value: 2.71828
Enter a character: A
Enter a string: Hello, Java!

OUTPUT :
You entered:
Integer: 42
Float: 3.14
Double: 2.71828
Character: A
String: Hello, Java!

BASIC CODE TO FIND THE ASCII VALUE OF A CHARACTER :

import java.util.Scanner;

public class ASCIIValue {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting the user to enter a character


System.out.print("Enter a character: ");
char character = scanner.next().charAt(0);

// Finding the ASCII value of the character


int asciiValue = (int) character;

// Displaying the ASCII value


System.out.println("The ASCII value of '" + character + "' is: " + asciiValue);

}
}
JAVA CODE OF ARRAY PROMPT FROM USER AND SHOWING OUTPUT

import java.util.Scanner;

public class ArrayInputOutput {


public static void main(String[] args) {
// Creating a Scanner object for input
Scanner scanner = new Scanner(System.in);

// Prompting the user to enter the size of the array


System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();

// Declaring an array of the given size


int[] numbers = new int[size];

// Taking input for the array elements


System.out.println("Enter " + size + " numbers:");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}

// Displaying the array elements


System.out.println("The elements of the array are:");
for (int i = 0; i < size; i++) {
System.out.println("Element " + (i + 1) + ": " + numbers[i]);
}

}
}

LEFT ROTATE ARRAY

import java.util.Scanner;

public class LeftRotateArray {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Taking array size as input


System.out.println("Enter the size of the array: ");
int size = scanner.nextInt();

// Declaring the array


int[] array = new int[size];

// Taking array elements as input


System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

// Taking number of rotations as input


System.out.print("Enter the number of rotations: ");
int rotations = scanner.nextInt();

// Perform left rotations


for (int r = 0; r < rotations; r++) {
// Store the first element temporarily
int firstElement = array[0];

// Shift elements to the left using array[i] = array[i - 1] logic


for (int i = 1; i < size; i++) {
array[i - 1] = array[i];
}

// Place the first element at the end


array[size - 1] = firstElement;
}

// Displaying the rotated array


System.out.println("Array after " + rotations + " left rotations:");
for (int num : array) {
System.out.print(num + " ");
}

// Closing the scanner


scanner.close();
}
}

You might also like