Prachi Java Assignment
Prachi Java Assignment
Prachi Java Assignment
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
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 {
OUTPUT
CODE:-
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);
}
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
CODE:-
import java.util.*;
public class Largest {
OUTPUT
import java.util.*;
public class Smallest {
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
CODE:-
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 {
{
int i,n,sum=0;
{
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:-
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 {
System.out.println("Binary to Decimal");
System.out.println(num + " = " + decimal);
}
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
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;
}
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;
}
OUTPUT