Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Prachi Java Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Pre-Joining Assignment | - Core JAVA

Name Prachi Baranwal


College KIET Group of
Institutions

Section 1: Basic Java


1. Write a Java program to print 'Hello' on screen and then print your name on a
separate line.

CODE:-
class Helloworld{
public static void main(String args[])
{
System.out.println("Hello World \nPrachi Baranwal");
}
}

OUTPUT

2. Write a Java program that accepts four integers from the user and prints equal if all
four are equal, and not equal otherwise.

CODE:-
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
System.out.print("Enter first number: ");
int n1 = i.nextInt();
System.out.print("Enter second number: ");
int n2 = i.nextInt();
System.out.print("Enter third number: ");
int n3 = i.nextInt();
System.out.print("Enter fourth number: ");
int n4 = i.nextInt();
if (n1 == n2 && n2 == n3 && n3 == n4)
{
System.out.println("Integer are equal.");
}
else
{
System.out.println("Integer are not equal!");
}
}
}

OUTPUT

3. Java program to Find Factorial


CODE:-
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
long factorial = 1;
Scanner f = new Scanner(System.in);
int num = f.nextInt();
for(int i = 1; i <= num; ++i)
{
factorial *= i;
}
System.out.println(factorial);
}
}

OUTPUT

4. Write a Java program that takes three numbers as input to calculate and print the
average of the numbers

CODE:-

import java.util.Scanner;
public class average {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Input the first number: ");
double x = in.nextDouble();
System.out.print("Input the second number: ");
double y = in.nextDouble();
System.out.print("Input the third number: ");
double z = in.nextDouble();
System.out.print("The average value is " + average(x, y, z)+"\n" );
}

public static double average(double x, double y, double z)


{
return (x + y + z) / 3;
}
}

OUTPUT

5. Write a Java program to


compute a specified
formula. Specified Formula:
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11)

CODE:-

public class Exercise10 {


public static void main(String[] args) {
double result = 4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11));
System.out.println(result); //
}
}

OUTPUT

Section 2: Arrays
1. Write a Java program to print an array after changing the rows and columns of a
given two-dimensional array.
Original Array:
10 20 30
40 50 60
After changing the rows and columns of the said array:
10 40
20 50
30 60

CODE:-

import java.util.Scanner;
public class Transpose {
public static void main(String[] args) {
int[][] arr = {
{10, 20, 30},
{40, 50, 60}
};
System.out.print("Original Array:\n");
print_array(arr);
System.out.print("After changing the rows and columns of the said array:");
transpose(arr);
}

private static void transpose(int[][] arr) {


int[][] newarr = new int[arr[0].length][arr.length];

for (int i = 0; i < arr.length; i++) {


for (int j = 0; j < arr[0].length; j++) {
newarr[j][i] = arr[i][j];
}
}

print_array(newarr);
}
private static void print_array(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println( );
}

}
}

OUTPUT

2. Write a Java program to find the k largest elements in a given array.


Elements in the array can be in any order. Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 largest elements of the said array are:
100 25 17

CODE:-

import java.util.*;
public class Largest {

public static void main(String[] args)


{
Integer arr[] = new Integer[]{1, 4, 17, 7, 25, 3, 100};
int k = 3;
System.out.println("Original Array: ");
System.out.println(Arrays.toString(arr));
System.out.println(k +" largest elements of the said array are:");
Arrays.sort(arr, Collections.reverseOrder());
for (int i = 0; i < k; i++)
System.out.print(arr[i] + " ");
}
}

OUTPUT

3. Write a Java program to find the k smallest elements in a given array.


Elements in the array can be in any order. Expected Output:
Original Array:
[1, 4, 17, 7, 25, 3, 100]
3 largest
elements of
the said array
are: 1, 3, 4
CODE:-

import java.util.*;
public class Smallest {

public static void main(String[]


args)
{
Integer arr[] = new Integer[]{1,
4, 17, 7, 25, 3, 100};
int k = 3;
System.out.println("Original Array:
");

System.out.println(Arrays.toString(
arr));
System.out.println(k + " smallest
elements of the said array are:");
Arrays.sort(arr);
for (int i = 0; i < k; i++)
System.out.print(arr[i] + " ");
}
}

OUTPUT

4. Write a Java program


to sort array in
ascending order
Expected Output:
Original array: [-2, 3, 4, -1, -3, 1, 2, -4, 0]
Result: [-4, -3, -2, -1, 0, 1, 2, 3, 4]

CODE:-

public class SortAsc {


public static void main(String[] args) {
int [] arr = new int [] {-2, 3, 4, -1, -3, 1, 2, -4, 0};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.println();
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

OUTPUT
Section 3: Conditional Statement
1. Write a program in Java to display the ‘n’ terms of odd natural number and their
sum.
Input number of terms is: 5
Expected Output:
The odd numbers are:
1
3
5
7
9

CODE:-

import java.util.Scanner;
public class Oddsum {

public static void main(String[] args)

{
int i,n,sum=0;

System.out.print("Input number of terms is: ");


{
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("\nThe odd numbers are :");
for(i=1;i<=n;i++)

{
System.out.println (2*i-1);
sum+=2*i-1;
}
System.out.println ("The Sum of odd Natural Number upto " +n+" terms is: "
+sum);

}
}
}

OUTPUT

2. Write a program in Java to make such a pattern like a pyramid with a number
which will repeat the number in the same row.
Enter no. of Rows? 4
1
22
333
4444

CODE:-
public class pyramid{
public static void main(String args[])
{
int k=2*4-2,i,j;
for(i=1; i<=4; i++)
{
for(j=1; j<=k+1; j++)
{
System.out.print(" ");
}
k=k-1;
for(j=1; j<=i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}

OUTPUT

3. Write a program that accepts three numbers from the user and prints "increasing"
if the numbers are in increasing order, "decreasing" if the numbers are in
decreasing order, and "Neither increasing or decreasing order" otherwise.
Test Data
Input first number: 1524
Input second number: 2345
Input third number: 3321
Expected Output:
Increasing order

CODE:-

import java.util.Scanner;
public class Incdec {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
double x = in.nextDouble();
System.out.print("Input second number: ");
double y = in.nextDouble();
System.out.print("Input third number: ");
double z = in.nextDouble();
if (x < y && y < z)
{
System.out.println("Increasing order");
}
else if (x > y && y > z)
{
System.out.println("Decreasing order");
}
else
{
System.out.println("Neither increasing or decreasing order");
}
}
}

OUTPUT
4. Java program to Display Fibonacci Series

CODE:-

public class fib {


public static void main(String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;


System.out.println("Fibonacci Series till " + n + " terms:");

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


System.out.print(firstTerm + ", ");

// compute the next term


int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}

OUTPUT
5. Java program to check leap year

CODE:-

import java.util.Scanner;
public class LeapYear
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
boolean flag = false;
if(year % 400 == 0)
{
flag = true;
}
else if (year % 100 == 0)
{
flag = false;
}
else if(year % 4 == 0)
{
flag = true;
}
else
{
flag = false;
}
if(flag)
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
}

OUTPUT

Section 4: Operators
1. Java program for Binary to decimal conversion.

CODE:-

class binarytodecimal {

public static void main(String[] args) {


// binary number
long num = 110110111;

// call method by passing the binary number


int decimal = convertBinaryToDecimal(num);

System.out.println("Binary to Decimal");
System.out.println(num + " = " + decimal);
}

public static int convertBinaryToDecimal(long num) {


int decimalNumber = 0, i = 0;
long remainder;

while (num != 0) {
remainder = num % 10;
num /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;
}

return decimalNumber;
}
}

OUTPUT
2. Java program to find largest of three numbers using ternary operator.

CODE:-

import java.util.Scanner;
public class Largestno
{
public static void main(String[] args)
{
int a, b, c, largest, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
temp=a>b?a:b;
largest=c>temp?c:temp;
System.out.println("The largest number is: "+largest);
}
}
OUTPUT

3. Java program to swap two numbers using bitwise operator

CODE:-

import java.util.Scanner;
public class Swap
{
public static void main(String args[])
{
int a, b;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
a = scanner.nextInt();
System.out.print("Enter the second number: ");
b = scanner.nextInt();
System.out.println("Before swapping:");
System.out.println("a = " +a +", b = " +b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After swapping:");
System.out.print("a = " +a +", b = " +b);
}
}

OUTPUT

4. Write a Java program that will accept an integer and convert it into a binary
representation. Now count the number of bits which is equal to zero of the said binary
representation.
Expected Output:
Input first number: 25
Binary representation of 25 is: 11001
Number of zero bits: 2

CODE:-

import java.util.Scanner;
public class inttobinary {
public static int countBitsTozeroBasedOnString(int n) {
int ctr = 0;
String binaryNumber = Integer.toBinaryString(n);
System.out.print("Binary representation of "+n+" is: "+binaryNumber+"\n");
for (char ch : binaryNumber.toCharArray()) {
ctr += ch == '0' ? 1 : 0;
}
return ctr;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
int n = in.nextInt();
System.out.println("\nNumber of zero bits: " + countBitsTozeroBasedOnString(n));
}
}

OUTPUT

Section 5: Strings
1. Write a Java program to reverse the content of a sentence (assume a single space
between two words) without reverse every word.
Input a string: The quick brown fox jumps over the lazy dog
Result: dog lazy the over jumps fox brown quick The

CODE:-

import java.util.*;
public class Reversesentence{
public static String reverse_str_word(String input_sentence) {
if (input_sentence == null) {
throw new IllegalArgumentException("Input param can't be null.");
}
StringBuilder stringBuilder = new StringBuilder();
String[] words = input_sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
stringBuilder.append(words[i]);
if (i != 0) {
stringBuilder.append(" ");
}
}
return stringBuilder.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input a string: ");
String input = scanner.nextLine();
System.out.println("\nResult: " + reverse_str_word(input));
}
}

OUTPUT

2. Write a Java program to accept two string and test if the second string contains the
first one. Input first string: Once in a blue moon
Input second string: See eye to eye
If the second string contains the first one? False

CODE:-

import java.util.*;
public class check {
public static boolean is_str_contains(String str1, String str2) {
if (str1 == null || str2 == null) {
throw new IllegalArgumentException("You can't pass null strings as input.");
}
boolean ans = false;
for (int i = 0; i < str2.length() - 1; i++) {
if (str2.charAt(i) == str1.charAt(0)) {
for (int j = 0; j < str1.length(); j++) {
if ((i + j) < str2.length() && str1.charAt(j) == str2.charAt(i + j) && j == str1.length() - 1) {
ans = true;
break;
}
}
}
}
return ans;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Input first string: ");
String str1 = scanner.nextLine();
System.out.print("Input second string: ");
String str2 = scanner.nextLine();
System.out.println("If the second string contains the first one? "+is_str_contains(str1,
str2));
}
}

OUTPUT

You might also like