0% found this document useful (0 votes)
12 views28 pages

Assign Men 7

The document contains a series of Java programming assignments focused on various computational tasks, such as counting set bits, calculating parity, swapping bits, and performing arithmetic operations without using standard operators. Each assignment includes a brief description, the Java code implementation, and sample outputs. The assignments cover a range of topics including number manipulation, prime number checking, and digit frequency counting.
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)
12 views28 pages

Assign Men 7

The document contains a series of Java programming assignments focused on various computational tasks, such as counting set bits, calculating parity, swapping bits, and performing arithmetic operations without using standard operators. Each assignment includes a brief description, the Java code implementation, and sample outputs. The assignments cover a range of topics including number manipulation, prime number checking, and digit frequency counting.
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/ 28

Assignment-07

Name-Aditya Kumar Satapathy


Regd no-2341019349
Sec-2G1
Branch-CSE(B.Tech)
1. Write a Java program to count the number of bits that are set to
1 in an integer.
Program-package Assignment7;

public class Ass7_Q1 {


public static int countSetBits(int number) {
int count = 0;
while (number != 0) {
count += number & 1;
number >>>= 1;
}
return count;
}

public static void main(String[] args) {


// Example usage
int number = 29; // Binary representation: 11101
int result = countSetBits(number);
System.out.println("The number of 1 bits in " + number + " is:
" + result);
}
}
Output-

2. The parity of a binary word is 1 if the number of 1s in the word


is odd; otherwise,it is 0. Write a Java program to count the
parity of an integer number.
Program-package Assignment7;

import java.util.Scanner;

public class Ass7_Q2 {

public static int calculateParity(int number) {


int count = 0;
while (number != 0) {
count += number & 1;
number >>= 1;
}
return count % 2;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
int parity = calculateParity(number);
System.out.println("The parity of the number is: " + parity);

scanner.close();
}
}
Output-

3. Write a program to swap the ith bit with jth bit of a number.
Program-package Assignment7;

public class Ass7_Q3 {

public static int swapBits(int number, int i, int j) {


int bit1 = (number >> i) & 1;
int bit2 = (number >> j) & 1;
if (bit1 == bit2) {
return number;
}
int bitMask = (1 << i) | (1 << j);
return number ^ bitMask;
}
public static void main(String[] args) {
int number = 65;
int i = 1;
int j = 6;

System.out.println("Original number: " + number + " (Binary: "


+ Integer.toBinaryString(number) + ")");
int result = swapBits(number, i, j);
System.out.println("After swapping: " + result + " (Binary: " +
Integer.toBinaryString(result) + ")");
}
}
Output-

4. Write a program that takes a 64-bit word and returns the 64-bit
word consisting
of the bits of the input word in reverse order. For example, if the
input is
alternating 1s and 0s, i.e., (1010...10), the output should be
alternating 0s and
1s, i.e.,(0101...01).
Program-package Assignment7;

public class Ass7_Q4 {

public static long reverseBits(long number) {


long reversed = 0;
for (int i = 0; i < 8; i++) {
long bit = number & 1;
reversed = (reversed << 1) | bit;
number >>= 1;
}
return reversed;
}

public static void main(String[] args) {


long input = 0xAAAAAAAAAAAAAAAAL;
System.out.println("Input (Binary): " +
Long.toBinaryString(input));
long output = reverseBits(input);
System.out.println("Output (Binary): " +
Long.toBinaryString(output));
}
}
Output-

5.Write a java program to copmute x × y without arithmetic


operators.
Program-package Assignment7;

public class Ass7_Q5 {

public static int multiply(int x, int y) {


int result = 0;
while (y != 0) {
if ((y & 1) != 0) {
result = add(result, x);
}
x <<= 1;
y >>= 1;
}

return result;
}
private static int add(int a, int b) {
while (b != 0) {
// Calculate carry
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}

public static void main(String[] args) {


int x = 7;
int y = 3;

System.out.println("x: " + x + ", y: " + y);


System.out.println("x * y: " + multiply(x, y));
}
}
Output-

6. Write a java program to copmute x/y without arithmetic


operators.
Program-package Assignment7;

public class Ass7_Q6 {

public static int divide(int dividend, int divisor) {


if (divisor == 0) {
throw new ArithmeticException("Division by zero is
undefined.");
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean negative = (dividend < 0) ^ (divisor < 0);
long absDividend = Math.abs((long) dividend);
long absDivisor = Math.abs((long) divisor);

int result = 0;

while (absDividend >= absDivisor) {


long tempDivisor = absDivisor;
int multiple = 1;
while (absDividend >= (tempDivisor << 1)) {
tempDivisor <<= 1;
multiple <<= 1;
}
absDividend -= tempDivisor;
result += multiple;
}
return negative ? -result : result;
}

public static void main(String[] args) {


int dividend = 56;
int divisor = 6;
System.out.println("Dividend: " + dividend + ", Divisor: " +
divisor);
System.out.println("Quotient: " + divide(dividend, divisor));
}
}
Output-

7. Write a program to find xy.


Program-package Assignment7;

public class Ass7_Q7 {

public static long power(int x, int y) {


if (y < 0) {
throw new IllegalArgumentException("Negative powers are
not supported.");
}
long result = 1;
long base = x;
while (y > 0) {
if ((y & 1) == 1) {
result *= base;
}
base *= base;
y >>= 1;
}

return result;
}

public static void main(String[] args) {


int x = 3;
int y = 4;

System.out.println(x + " raised to the power " + y + " is: " +


power(x, y));
}
}
Output-

8. Write a program to find the reverse of a number. For example, if


the input is
123 output is 321, and if the input is -245 output is -542.
Program-package Assignment7;
public class Ass7_Q8 {

public static int reverse(int number) {


int reversed = 0;

while (number != 0) {

int digit = number % 10;


if (reversed > Integer.MAX_VALUE / 10 || reversed <
Integer.MIN_VALUE / 10) {
throw new ArithmeticException("Overflow occurred during
reversal.");
}
reversed = reversed * 10 + digit;
number /= 10;
}

return reversed;
}

public static void main(String[] args) {


int input = -245;
System.out.println("Original number: " + input);
System.out.println("Reversed number: " + reverse(input));
}
}
Output-

9. Write a program to check whether a number is palindrome or not.


Program-package Assignment7;
public class Ass7_Q9 {

public static boolean isPalindrome(int number) {


// Negative numbers are not palindromes
if (number < 0) {
return false;
}

int original = number; // Store the original number


int reversed = 0;

// Reverse the number


while (number != 0) {
int digit = number % 10;

// Check for overflow


if (reversed > Integer.MAX_VALUE / 10) {
return false; // Overflow indicates it's not a valid palindrome
}

reversed = reversed * 10 + digit;


number /= 10;
}

// Check if the reversed number equals the original


return original == reversed;
}

public static void main(String[] args) {


int input = 121; // Example input
System.out.println("Is " + input + " a palindrome? " +
isPalindrome(input));

int input2 = -121; // Another example input


System.out.println("Is " + input2 + " a palindrome? " +
isPalindrome(input2));
}
}
Output-

10. Write a Java program that reads two float numbers and checks
whether the difference between these two numbers is less than ϵ (ϵ
< 1).
Program-package Assignment7;
import java.util.Scanner;

public class Ass7_Q10 {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first float number: ");
float num1 = scanner.nextFloat();
System.out.print("Enter the second float number: ");
float num2 = scanner.nextFloat();
final float EPSILON = 0.0001f;
float difference = Math.abs(num1 - num2);
if (difference < EPSILON) {
System.out.println("The difference between the two numbers
is less than " + EPSILON);
} else {
System.out.println("The difference between the two numbers
is greater than or equal to " + EPSILON);
}
scanner.close();
}
}
Output-

11. Write a Java program that reads an integer number and counts
the number of digits that are even.
Program-package Assignment7;
import java.util.Scanner;
public class Ass7_Q11 {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
number = Math.abs(number);

int evenCount = 0;
while (number > 0) {
int digit = number % 10;
if (digit % 2 == 0) {
evenCount++;
}

number /= 10;
}
System.out.println("Number of even digits: " + evenCount);
scanner.close();
}
}
Output-

12. Write a Java program that reads two integer number and create a
third number by taking the first two digits of the first number and the
last two digits of the second number.
Example: Input: 45678, 312 Output:4512
Program-package Assignment7;

import java.util.Scanner;

public class Ass7_Q12 {

public static int createNumber(int num1, int num2) {


int firstTwoDigits = num1 / (int)Math.pow(10,
(int)Math.log10(num1) - 1);

int lastTwoDigits = num2 % 100;


return firstTwoDigits * 100 + lastTwoDigits;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


int num2 = scanner.nextInt();
int result = createNumber(num1, num2);
System.out.println("The new number is: " + result);
scanner.close();
}
}
Program-

13. Write a Java program to count the frequency of each digit of a


number.
Program-package Assignment7;

import java.util.Scanner;
public class Ass7_Q13 {

public static void countDigitFrequency(int number) {


int[] frequency = new int[10];
number = Math.abs(number);
while (number > 0) {
int digit = number % 10;
frequency[digit]++;
number /= 10;
}
System.out.println("Digit frequencies:");
for (int i = 0; i < 10; i++) {
if (frequency[i] > 0) {
System.out.println("Digit " + i + ": " + frequency[i]);
}
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
countDigitFrequency(number);
scanner.close();
}
}
Output-

14. Write a Java program to check whether a number is prime or not.


Program-package Assignment7;
import java.util.Scanner;

public class Ass7_Q14 {

public static boolean isPrime(int number) {


// Handle edge cases
if (number <= 1) {
return false;
}
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}

return true;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
}
Output-

15. Write a program to print the first 100th prime number.


Program-package Assignment7;
public class Ass7_Q15 {
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {


int count = 0;
int number = 2;
while (count < 100) {
if (isPrime(number)) {
count++;
}
number++;
}
System.out.println("The 100th prime number is: " + (number -
1));
}
}
Output-

16. Write a Java program to print the prime number in a range.


Program-package Assignment7;
import java.util.Scanner;

public class Ass7_Q16 {


public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the starting number of the range: ");
int start = scanner.nextInt();

System.out.print("Enter the ending number of the range: ");


int end = scanner.nextInt();
System.out.println("Prime numbers between " + start + " and " +
end + " are:");
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
scanner.close();
}
}
Output-

17. Write a program that returns true if the number is even else
returns false.
Program-package Assignment7;
import java.util.Scanner;

public class Ass7_Q17 {


public static boolean isEven(int number) {
return number % 2 == 0;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println(isEven(number));
scanner.close();
}
}
Output-

You might also like