0% found this document useful (0 votes)
18 views3 pages

Set 14

The document contains two Java programs: one for converting an integer to its binary representation and counting the number of zeros, and another for checking if one string ends with another. The first program prompts the user for a number and outputs its binary form along with the zero count, while the second program takes two strings and checks if the first ends with the second, displaying the result. Both programs utilize the Scanner class for user input.

Uploaded by

kareena.it222110
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)
18 views3 pages

Set 14

The document contains two Java programs: one for converting an integer to its binary representation and counting the number of zeros, and another for checking if one string ends with another. The first program prompts the user for a number and outputs its binary form along with the zero count, while the second program takes two strings and checks if the first ends with the second, displaying the result. Both programs utilize the Scanner class for user input.

Uploaded by

kareena.it222110
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/ 3

Set14

import java.util.Scanner;

public class BinaryConversion {

// Method to convert an integer to binary and count zeros

public static void convertToBinaryAndCountZeros(int num) {

// Convert the integer to binary representation using Integer.toBinaryString()

String binaryString = Integer.toBinaryString(num);

System.out.println("Binary representation of " + num + " is: " + binaryString);

// Count the number of zeros in the binary representation

int zeroCount = 0;

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

if (binaryString.charAt(i) == '0') {

zeroCount++;

System.out.println("Number of zero bits: " + zeroCount);

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Input first number: ");

int number = scanner.nextInt();

convertToBinaryAndCountZeros(number);
scanner.close();

Output

Input first number: 25

Binary representation of 25 is: 11001

Number of zero bits: 2

Input first number: 8

Binary representation of 8 is: 1000

Number of zero bits: 3

Q2

import java.util.Scanner;

public class StringEndsWithCheck {

// Function to check if the first string ends with the second string

public static boolean endsWith(String str1, String str2) {

if (str1.endsWith(str2)) {

return true;

return false;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first string: ");

String string1 = scanner.nextLine();

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

String string2 = scanner.nextLine();


boolean result = endsWith(string1, string2);

System.out.println("Does the first string end with the second string? " + result);

scanner.close();

Output

Enter the first string: HelloWorld

Enter the second string: World

Does the first string end with the second string? True

Enter the first string: Programming

Enter the second string: Code

Does the first string end with the second string? false

You might also like