0% found this document useful (0 votes)
25 views4 pages

Lab 1

java
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)
25 views4 pages

Lab 1

java
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

Lab1:

1.Write java programs to perform arithmetic, logical and bitwise operations.


Ans:
class q1 {
public static void main(String[] args) {
int a = 20, b = 4;
System.out.println("Sum: " + (a + b));
System.out.println("Product: " + (a * b));
boolean x = true, y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
int p = 7, q = 3;
System.out.println("p & q: " + (p & q));
System.out.println("p ^ q: " + (p ^ q));
}
}

Output:

2. Write java program to show type conversion.


Ans:
class q2 {
public static void main(String[] args) {
int a = 100;
float b = a;
System.out.println("Implicit Type Conversions:");
System.out.println("int to float: " + b);
double n1 = 1234.56;
int n2 = (int) n1;
System.out.println("\nExplicit Type Conversion (Casting):");
System.out.println("double to int: " + n2);
char char1 = 'A';
int unicodeValue = (int) char1;
System.out.println("char to int (unicode value of 'A'): " +
unicodeValue);
}
}

Output:

3.Using Function call approach write a program to find difference of two numbers.

public class DifferenceOfNumbers {


public static void main(String[] args) {
int num1 = 10;
int num2 = 5;

int difference = findDifference(num1, num2);

System.out.println("Difference of " + num1 + " and " + num2 + " is: " + difference);
}

// Static method to find the difference of two numbers


public static int findDifference(int a, int b) {
return a - b;
}
}

Output:
4. Write a program to input 5 values from the user. Display sum of odd and even values
separately. Your program should use loop , store even and odd values in two separate
arrays.

Ans:

import java.util.Scanner;
class q4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] even = new int[5];
int[] odd = new int[5];
int evenSum = 0;
int oddSum = 0;
System.out.println("Enter 5 numbers:");
for (int i = 0; i < 5; i++) {
int num = scanner.nextInt();
if (num % 2 == 0) {
even[i] = num;
evenSum += num;
} else {
odd[i] = num;
oddSum += num;
}}
System.out.println("\nEven Numbers:");
for (int num : even) {
if (num != 0) {
System.out.print(num + " ");
}}
System.out.println("\nSum of Even Numbers: " + evenSum);
System.out.println("\nOdd Numbers:");
for (int num : odd) {
if (num != 0) {
System.out.print(num + " ");
} }
System.out.println("\nSum of Odd Numbers: " + oddSum);

scanner.close();
}
}
Output:

Enter 5 numbers:
2
3
4
5
6

Even Numbers:
246
Sum of Even Numbers: 12

Odd Numbers:
35
Sum of Odd Numbers: 8

You might also like