0% found this document useful (0 votes)
22 views

Lab Report 3

The document describes two Java programming experiments, the first to count the occurrences of the digit 2 in a positive integer input by the user, and the second to determine if a user-inputted string is a palindrome by reversing the string and comparing it to the original. It includes the objective, algorithm, code, and sample output for each experiment.

Uploaded by

22103119
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)
22 views

Lab Report 3

The document describes two Java programming experiments, the first to count the occurrences of the digit 2 in a positive integer input by the user, and the second to determine if a user-inputted string is a palindrome by reversing the string and comparing it to the original. It includes the objective, algorithm, code, and sample output for each experiment.

Uploaded by

22103119
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/ 6

IUBAT – International University of

Business Agriculture and Technology.

Lab Report – 03
Course Code: CSC-384
Course Name: Programming In Java Lab

Submitted to:
Naeem Mia
Lecturer, Department of Computer Science and
Engineering.

Submitted by:
Name: Sabbir Hossain
ID: 22103119
Section: F
Semester: Spring -2024
Program: BCSE
Submission Date: 17-02-2024
Experiment :1
Objective: The objective of the program in Java is to prompt the user to input a
positive integer and then count the occurrences of the digit '2' in that number.

Algorithm:
• It starts by declaring variables for the user input ('n') and the result of the
digit counting function ('result').
• It prompts the user to input a number, reads the input, and checks if it's
greater than 0.
• If the number is positive, it calls the test function to count the occurrences
of the digit '2' and displays the result.
• The test function takes an integer parameter, initializes a counter ('ctr'),
and iterates through each digit of the number, incrementing the counter if
the digit is '2'.
• The final count is returned.

Code:

package my.userinput;

import java.util.Scanner;
public class intValue2 {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int n = in.nextInt();
if (n>0)
{
System.out.println(test(n));
}
}
public static int test(int num)
{
int ctr = 0;
int n = num;
do{
if (n % 10 == 2){
ctr ++;
}
n /= 10;
}while(n > 0);
return ctr;
}

}
Output:

Experiment :2

Objective: The objective of the program in Java is to interactively determine


whether a user-inputted string is a palindrome. A palindrome is a sequence of
characters that reads the same forwards as backward.
Algorithm:
• It starts by declaring variables for the user input string (myString) and the
reversed string (reversedString).
• It prompts the user to input a palindrome string and reads the input.
• It uses a StringBuffer to reverse the input string and stores the result in
reversedString.
• It compares the original string with its reversed version and displays
whether it is a palindrome or not.

Code:
package my.userinput;

import java.util.Scanner;

public class PalindromeString {

public static void main(String args[]) {


Scanner input = new Scanner (System.in);

System.out.print("Enter your palindrome string : ");


String myString = input.nextLine();

// String myString = "anna";

StringBuffer buffer = new StringBuffer(myString);


buffer.reverse();
String data = buffer.toString();
if(myString.equals(data)){
System.out.println("Given String is palindrome");
} else {
System.out.println("Given String is not palindrome");
}
}
}

Output:

You might also like