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

Important Computer

The document outlines a computer project by Arindam Singh for Class X-A, detailing various Java programming assignments. It includes programs for checking triangle types based on angles, calculating electricity bills, determining perfect and Harshad numbers, generating Fibonacci series, and more. Each program is accompanied by code, variable descriptions, and sample outputs.

Uploaded by

Arindam Singh
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 views40 pages

Important Computer

The document outlines a computer project by Arindam Singh for Class X-A, detailing various Java programming assignments. It includes programs for checking triangle types based on angles, calculating electricity bills, determining perfect and Harshad numbers, generating Fibonacci series, and more. Each program is accompanied by code, variable descriptions, and sample outputs.

Uploaded by

Arindam Singh
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/ 40

COMPUTER PROJECT

NAME – ARINDAM SINGH

CLASS – X-A

SUBJECT - COMPUTER

ROLL NO. 11

INTERNAL EXAMINER –

EXTERNAL EXAMINER -

1|Page
1.Write a program to input three angles of a triangle
and check whether a triangle is possible or not. If
possible then check whether it is an acute-angled
triangle, right-angled or an obtuse-angled triangle
otherwise, display 'Triangle not possible'.
import java.util.Scanner;
public class TriangleAngle1 {
public static void main() {
Scanner in = new Scanner(System.in);
System.out.println("Enter first angle: "); //INPUT
int a1 = in.nextInt();
System.out.println("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int sum = a1 + a2 + a3;
if (sum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {
if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) //
Check for right-angled triangle {
System.out.println("Right-angled Triangle");
}
else if (a1 > 90 || a2 > 90 || a3 > 90) // Check
for obtuse-angled triangle {
System.out.println("Obtuse-angled Triangle");
}
} else {
System.out.println("Triangle not possible");
}}}

2|Page
Variable Data Type Function
a1 int to input angle
a2 int to input angle
a3 int to input angle
sum int to store sum of angles

OUTPUT:
1.Enter first angle: 60
Enter second angle: 50
Enter third angle: 24
Triangle not possible
2. Enter first angle: 65
Enter second angle: 65
Enter third angle: 50
Acute-angled Triangle
3. Enter first angle: 65
Enter second angle: 65
Enter third angle: 50
Acute-angled Triangle
4. Enter first angle: 120
Enter second angle: 30
Enter third angle: 30
Obtuse-angled Triangle

3|Page
2. An Electricity Company charges their consumers
according to the units consumed per month According
to the given traffic:
Units Consumed Charges
Up to 100 units Rs. 2 per
unit
More than 100 units and
up to 200 units Rs. 1.80
per unit
More than 200 units Rs.1.50 per unit
In addition to the above, every consumer has to pay
Rs.200 as Service Charge per month. Write a program
to input the amount of units consumed and calculate
the total charges payable (Bill) by the consumer.

import java.util.*;

class ElectricityBill {
public static void main() {
int u;
double c = 0.0, tc = 0.0;
Scanner sc = new Scanner(System.in);

System.out.println("Enter unit"); // Prompt for input


u = sc.nextInt(); // Read the number of units

if (u <= 100) c = 2 * u; // First 100 units at 2 per


unit
else if (u > 100 && u <= 200) c = 100 * 2 + (u - 100)
* 1.8; // Next 100 units at 1.8 per unit

4|Page
else if (u > 200) c = 100 * 2 + 100 * 1.8 + (u - 200)
* 1.50; // Units above 200 at 1.50 per unit

tc = 200 + c; // Add fixed charge of 200


System.out.println("Amount Payable = " + tc); //
Output the total payable amount }}

Variable Data Type Function


u int to input unit
c double cost
tc double total cost

OUTPUT:
1. Enter unit
33
Amount Payable = 266.0
2. Enter unit
22
Amount Payable = 244.0

5|Page
3. Write a program in java to input a number and
check whether it’s a perfect number or not.
A perfect number is a positive integer that is equal
to the sum of its proper divisors, excluding the
number itself. For example, 6 is a perfect number
because the sum of its divisors (1, 2, and 3) equals
6.
import java.util.*;

class PerfectNumber {
public static void main() {
Scanner sc = new Scanner(System.in); // Create a
scanner for input
System.out.print("Enter a number: "); // Prompt for
input
int num = sc.nextInt();
int sum = 0;
for (int i = 1; i <= num / 2; i++) { // Loop through
possible divisors
if (num % i == 0) { // Check if i is a divisor
sum += i; // Add divisor to the sum
}
}

if (sum == num)
System.out.println(num + " is a Perfect number.");
// Output if true
else
System.out.println(num + " is not a Perfect
number."); // Output if false
}

6|Page
}

Variable Data Type Function


i int for loop
num int to input number
sum int total sum of divisors

OUTPUT:
1.
Enter a number: 6
6 is a Perfect number.
2.
Enter a number: 4
4 is not a Perfect number.

7|Page
4. Write a Java program to input a number and check
whether it is a Harshad number or not. A Harshad
number is an integer that is divisible by the sum of
its digits. For example, 18 is a Harshad number
because the sum of its digits is 1 + 8 = 9, and 18 is
divisible by 9.

import java.util.Scanner;

class HarshadNumber {
public static void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();
int r = number;

int sum = 0;
int digit;
while (number != 0) { // Loop to calculate the sum of
digits
digit = number % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
number /= 10; // Remove the last digit
}

if (r % sum == 0)
System.out.println(r + " is a Harshad number.");
else
System.out.println(r + " is not a Harshad
number.");

8|Page
}}

Variable Data Type Function


number int to input the number
r int to save a copy of the
number
sum int to store sum of digits
digit int digits

OUTPUT:
1. Enter a number: 44
44 is not a Harshad number.
2. Enter a number: 18
18 is a Harshad number.

9|Page
5.Write a program to sum of series
x^2/4 +x^2/5 … n terms

class aaa {
public static void main(int n,int x) {
int a=2; int b=4;double S=0;
for (int i=1;i<=n;i++) {
S=Math.pow(x,a)/b;
}
System.out.println(S);
}

Variable Data Type Function


a int store the exponential values
b int variable to store the
denominator values
S double store the sum
n int limit
x int base number

OUTPUT:
1 n=44 x=3
2.25

10 | P a g e
6.Write a program in JAVA to print the Fibonacci
series upto ‘n’ terms.

class fibonacci {
public static void main(int n) {
int a=0; int b=1; int c=0;
System.out.println("the series upto " + n+"is :");
System.out.print(a + " ");
System.out.print(b + " ");
for (int i=1;i<=n-2;i++) //two terms have already been
printed
{
c=a+b;
System.out.print(c + " ") ;
a=b;
b=c; //moving ahead in the series
}
}
}

Variable Data Type Function


a int 1st term
b int 2nd term
c int third term and then moves
the series ahead
n int limit
i int loop

OUTPUT:

11 | P a g e
1. the series upto 10 is :
0 1 1 2 3 5 8 13 21 34

7.Write a program to print the following pattern.


1
2 3
4 5 6
7 8 9 10

class Pattern {
public static void main() {
int c=1;
for (int i=1;i<=4;i++)
{
for (int j=1;j<=i;j++) {
System.out.print(c + " ");
c++;
}
System.out.println();
}
}
}
Variable Data Type Purpose
c int to be printed and
increase by one
i int for loop
j int for loop 2

12 | P a g e
8.Write a program to print the pattern.
A
A C
A C E
A C E G
class GTRT {
public static void main() {
char i,j;
for (i='A';i<='G';i+=2) {
for (j='A';j<=i;j+=2) {
System.out.print(j + “ “);
}
System.out.println();

}
}
}

Variable Data type Purpose


i char for loop 1
j char for loop 2 and to
be printed

13 | P a g e
9.Define a class Equation to print the result of the
following equation using the given functions: n!/(r!
*(n-r)!)
Int Fact(int num)- to return factorial
Void printsum(inr r,int n)- to print sum of given
series

class Equation {

int fact(int num) {


int fa=1;
for (int i=1;i<=num;i++) {
fa=fa*i;
}
return fa;
}
void Printsum(int n,int r) {
Equation obj=new Equation();
int a= obj.fact(n);
int b= obj.fact(r);
int c= obj.fact(n-r);
double s=a/(b*c);
System.out.println(s);
}
public static void main(int n,int r) {
Equation obj2=new Equation();

14 | P a g e
obj2.Printsum(n,r);
}
}

Variable Data Type Purpose


num(int fact) int num whose factorial
would be calculated
fa(int fact) int to store and return
the factorial
n,r(void printsum) int to receive values
from the void main
function
a,b,c(void int to receive the
printsum) factorials from the
int fact function
s(void printsum) double to store the sum of
series
n(void main) int to be inputed by
the user
r(void main) int to be inputed by
the user

OUTPUT:
1.n=10,r=4
210.0

15 | P a g e
10. Develop a class "Array" with the following
Specifications:
instant Variables- int a[10], k
Member functions-
public void input() To input integer members to
array A and a number separately to variable k.
public void search() To find and print 'Search
Successful' if number is found 'Search Unsuccessful'
otherwise.

import java.util.Scanner;
class func {
int a[]=new int[10];int k;
public void input() {
Scanner sc=new Scanner(System.in);
for(int i=0;i<10;i++) {
System.out.println("Enter value for index" +i+ "at
cell number"+ (i+1));
a[i]=sc.nextInt();
}
System.out.println("enter number");
k=sc.nextInt();
}
public void search() {
int flag=0;
for (int i=0;i<10;i++){
if (a[i]==k) {

16 | P a g e
System.out.println("search successful, found at
index"+i);
flag++;
System.exit(0);
}
}
if (flag==0)
System.out.println("search unsuccessful");
}
public static void main() {

func obj=new func();


obj.input();
obj.search();
}
}

Variable Data Type Purpose


a[10](instance) int array to store the
values
k(instance) int to store the value
to be searched
i(void input) int for loop variable
i(void search) int for loop variable
flag(void search) int flag

OUTPUT:
1. Enter value for index0at cell number1
45

Enter value for index1at cell number2

34

Enter value for index2at cell number3

17 | P a g e
24

Enter value for index3at cell number4

34

Enter value for index4at cell number5

356

Enter value for index5at cell number6

345

Enter value for index6at cell number7

355

Enter value for index7at cell number8

555

Enter value for index8at cell number9

553

Enter value for index9at cell number10

44

enter number

44

search successful, found at index9

18 | P a g e
11.using 2 overloaded function by the name series
find and print the sum of the following series:
1.print sum of 1+1/2+1/3……1/n
2.print sum of 1+x+x/2^2+x/4^2……x/n^2

class arav {
void series(int n) { //first
int s=0;
for (int i=1;i<=n;i++){
s=s+1/i;
}
System.out.println(s);
}
void series(int n,int x){ //second
int s=0;
s=1+x;
for (int i=2;i<=n;i+=2){
s=(int)s+(x/(int)Math.pow(i,2));

}
System.out.println(s);
}
public static void main(int a,int b,int c) {
arav obj=new arav();
obj.series(a);
obj.series(b,c);
}
}

19 | P a g e
Variable Data Type Purpose
n(series 1) int limit

s(both series) int sum


n,x(series 2) int input
a,b,c int void main variables

OUTPUT:
a=5

b=6

c=5

1
7

20 | P a g e
12. design a class to overload a function sum()as
1.int sum(int a,int b) to calculate and return the
sum of all the even numbers in the range a to b.
2.double sum(double n) to calculate and return the
product of the series:
Pro=1.0*1.2*……*n
class abc {
int sum(int a,int b) {
int c=0;
if (a%2==1)
c=a++;
int s=0;
for (int i=c;i<=b;i+=2){
s=s+i;
}
return s;
}
double sum(double n) {
double pro=1;
for (double i=1;i<=n;i+=0.2){
pro=pro*i;
}
return pro;
}
public static void main(int a,int b,double n){
abc obj=new abc();
System.out.println(obj.sum(a,b));

21 | P a g e
System.out.println(obj.sum(n));}}

Variable Name Data Type Description

a (main) int First integer for sum calculation.

b (main) int Second integer for sum calculation.

c (sum(int a, int b)) int Stores a if odd, else a + 1.

s (sum(int a, int b)) int Sum of odd numbers between a and b.

n (main) double Number for product calculation.

pro (sum(double n)) double Product of numbers from 1 to n.

OUTPUT:
a=4 b=56 c=5
812
2.710780430399311E8

22 | P a g e
13. Define a class SalaryCalculation described as
below: Data members:
name (String data type) basicPay, specialAlw,
conveyanceAlw, gross, pf, netSalary, annualSal
(double data types) Member methods:
a) SalaryCalculation(): A constructor to assign name
of employee (name), basic salary (basicPay) of your
choice and conveyance allowance (conveyanceAlw) as
Rs. 1000.00.
b) void salaryCal(): To calculate other allowances
and salaries as given below: specialAlw = 25% of
basic salary pf = 11% of basic salary gross =
basicPay + specialAlw + conveyanceAlw netSalary =
gross – pf annualSal = 12 months of net salary
c) void display(): To display the name and other
calculations with suitable headings. Write a main()
method to call the above member methods.
import java.util.*;
class SalaryCalculation {
String name;double
basicPay,specialAlw,conveyanceAlw,gross,pf ,netSalary,annualSa
l;
SalaryCalculation(){
Scanner sc=new Scanner(System.in);
System.out.println("name");
name=sc.next();
System.out.println("basic salary");
basicPay=sc.nextDouble();
conveyanceAlw=1000.00;
}

23 | P a g e
void salaryCal(){
specialAlw=25*basicPay/100;
pf=11*basicPay/100;
gross=basicPay+specialAlw+conveyanceAlw;
netSalary=gross-pf;
annualSal=12*netSalary;
}
void display(){
System.out.println("name " + name);
System.out.println("basic pay " + basicPay);
System.out.println("special allowance (25 per of basic
salary )is "+specialAlw);
System.out.println("gross salary" + gross );
System.out.println("net salary "+netSalary);
System.out.println("annual salary"+annualSal);
}
public static void main() {
SalaryCalculation obj=new SalaryCalculation();
obj.salaryCal();
obj.display();
}

}
Variable Name Data Type Description
name (SalaryCalculation) String Stores the name of the employee.
basicPay double Basic salary input by the user.
(SalaryCalculation)
specialAlw (salaryCal) double Special allowance calculated as 25%
of the basic salary.
conveyanceAlw double Fixed conveyance allowance
(SalaryCalculation) (1000.00).

24 | P a g e
gross (salaryCal) double Gross salary (basic pay + special
allowance + conveyance).
pf (salaryCal) double Provident Fund calculated as 11% of
the basic salary.
netSalary (salaryCal) double Net salary (gross salary - provident
fund).
annualSal (salaryCal) double Annual salary (12 times the net
salary).
sc (SalaryCalculation) Scanner Used to take input from the user.

OUTPUT:
name
ARAV
basic salary
200000
name ARAV
basic pay 200000.0
special allowance (25 per of basic salary )is 50000.0
gross salary251000.0
net salary 229000.0
annual salary2748000.0

25 | P a g e
14.Design a class Common
Data Members Arr[],Arr2[] //arraysSz1,Sz2 //size of
array
Member Methods
Void accept(int s1,int s2)//to initialize data
members and assign memory to arrays and accept value
in both the arrays(assume no duplicate values in a
array)
Void display() to display value of both arrays
Void arrange () to arrange the values of array in
ascending order
Void common() function to display only the common
values of both arrays in ascending order

import java.util.Scanner;

class Common {
int Arr[];
int Arr2[];
int sz1, sz2;

void accept(int s1, int s2) {


sz1 = s1;
sz2 = s2;
Arr = new int[sz1];
Arr2 = new int[sz2];

26 | P a g e
Scanner sc = new Scanner(System.in);

for (int i = 0; i < sz1; i++) {


System.out.println("Enter value for index " + i +
" (cell no " + (i + 1) + "): ");
Arr[i] = sc.nextInt();
}

for (int i = 0; i < sz2; i++) {


System.out.println("Enter value for index " + i +
" (cell no " + (i + 1) + "): ");
Arr2[i] = sc.nextInt();
}
}

void display() {
System.out.println("First array:");
for (int i = 0; i < sz1; i++) {
System.out.print(Arr[i] + " ");
}
System.out.println();

System.out.println("Second array:");
for (int i = 0; i < sz2; i++) {
System.out.print(Arr2[i] + " ");
}
System.out.println();
}

void arrange() {
int temp;

27 | P a g e
for (int ph = 0; ph < sz1 - 1; ph++) {
for (int c = 0; c < sz1 - 1 - ph; c++) {
if (Arr[c] > Arr[c + 1]) {
temp = Arr[c];
Arr[c] = Arr[c + 1];
Arr[c + 1] = temp;
}
}
}

for (int ph = 0; ph < sz2 - 1; ph++) {


for (int c = 0; c < sz2 - 1 - ph; c++) {
if (Arr2[c] > Arr2[c + 1]) {
temp = Arr2[c];
Arr2[c] = Arr2[c + 1];
Arr2[c + 1] = temp;
}
}
}
}

void common() {
System.out.print("Common elements: ");
boolean found = false;
for (int i = 0; i < sz1; i++) {
for (int j = 0; j < sz2; j++) {
if (Arr[i] == Arr2[j]) {
System.out.print(Arr[i] + " ");
found = true;

28 | P a g e
}
}
}
if (!found) {
System.out.print("None");
}
System.out.println();
}

public static void main(String[] args) {


Common obj = new Common();
Scanner sc = new Scanner(System.in);

System.out.print("Enter size for first array: ");


int a = sc.nextInt();
System.out.print("Enter size for second array: ");
int b = sc.nextInt();

obj.accept(a, b);
obj.arrange();
obj.display();
obj.common();
}
}

Variable Name (Scope) Type Description


Arr[] (Class) int[] First array to store integer values entered
by the user.
Arr2[] (Class) int[] Second array to store integer values
entered by the user.
sz1 (Class) int Size of the first array (Arr).

29 | P a g e
sz2 (Class) int Size of the second array (Arr2).
i (Method) int Loop index for iterating through the arrays
in methods.
ph (Method) int Loop index for passes in bubble sort in
arrange().
c (Method) int Loop index for comparisons in bubble sort.
temp (Method) int Temporary variable for swapping elements in
sorting.
j (Method) int Loop index for checking common elements
between arrays.
sc (Method) Scanner Scanner object for user input in the
accept() method.

OUTPUT:
Enter size for first array: 4
Enter size for second array: 4
Enter value for index 0 (cell no 1):
3
Enter value for index 1 (cell no 2):
3
Enter value for index 2 (cell no 3):
1
Enter value for index 3 (cell no 4):
5
Enter value for index 0 (cell no 1):
6
Enter value for index 1 (cell no 2):
7
Enter value for index 2 (cell no 3):
8
Enter value for index 3 (cell no 4):

30 | P a g e
9
First array:
1 3 3 5
Second array:
6 7 8 9
Common elements: None

15.Create an array of size [3][3] with integers. Find


and print
1.sum of first rom
2.sum of last column
Also print the array.
import java.util.Scanner;

class Arrayyy {
public static void main(String[] args) {
int[][] A = new int[3][3];
Scanner sc = new Scanner(System.in);
int S = 0, S1 = 0;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
System.out.print("Enter for row " + i + "
column " + j + ": ");
A[i][j] = sc.nextInt();
}
}

for (int i = 0; i < 3; i++) S += A[0][i];


for (int j = 0; j < 3; j++) S1 += A[j][2];

System.out.println("Sum of first row: " + S);

31 | P a g e
System.out.println("Sum of last column: " + S1);
}
}

Variable Name (Scope) Type Description


A (Class) int[3] 2D array to store integers (3x3
[3] matrix)
sc (Class) Scanner Scanner object for input
S (Method) int Sum of elements in the first row
S1 (Method) int Sum of elements in the last column
i (Loop) int Loop counter for rows
j (Loop) int Loop counter for columns

OUTPUT:
Enter for row 0 column 0: 3
Enter for row 0 column 1: 2
Enter for row 0 column 2: 4
Enter for row 1 column 0: 5
Enter for row 1 column 1: 2
Enter for row 1 column 2: 5
Enter for row 2 column 0: 6
Enter for row 2 column 1: 77
Enter for row 2 column 2: 22
Sum of first row: 9
Sum of last column: 31

32 | P a g e
16.Write a program to input a sorted array(asc) and
search for a particular number as inputed by the
user.
import java.util.Scanner;
class ARARARRA {
public static void main(int A[]) {
int i;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=0;
int l=0;
int h=A.length-1;
while(l<=h){
m=(l+h)/2;
if (A[m]<n) {l=m+1;}
else if (A[m]>n){ h=m-1;}
else {
System.out.println(n + "found at "+ m);
break;
}

33 | P a g e
Variable Type Description
Name
n int Target integer to search for
m int Middle index of the current search
range
l int Lower bound of the search range
h int Upper bound of the search range
A int[ Array in which to search for the
] target integer

OUTPUT:

A[]={2,4,63,234,543,456,32}
4
4found at 1

34 | P a g e
17. write a program to input two array of size 5 by
int for marks and char code for the student and then
print a merit list after arranging in ascending
order.
import java.util.Scanner;
class MeritList {
public static void main(String[] args) {
int[] marks = new int[5];
char[] codes = new char[5];
Scanner sc = new Scanner(System.in);

for (int i = 0; i < 10; i++) {


System.out.print("Enter marks for student " + (i +
1) + ": ");
marks[i] = sc.nextInt();
System.out.print("Enter character code for student
" + (i + 1) + ": ");
codes[i] = sc.next().charAt(0);
}

for (int i = 0; i < marks.length - 1; i++) {


for (int j = 0; j < marks.length - 1 - i; j++) {
if (marks[j] > marks[j + 1]) {
int tempMark = marks[j];
marks[j] = marks[j + 1];
marks[j + 1] = tempMark;
char tempCode = codes[j];

35 | P a g e
codes[j] = codes[j + 1];
codes[j + 1] = tempCode;
}
}
}

System.out.println("Merit List:");
for (int i = 0; i < marks.length; i++) {
System.out.println("Character Code: " + codes[i] +
", Marks: " + marks[i]);}}}
Variable Name Type Description

marks int[] Array to store marks of 10 students

codes char[] Array to store character codes of 10 students

sc Scanne Scanner for taking input


r

i int Loop counter for iterating through the arrays

tempMark int Temporary variable for swapping marks

tempCode char Temporary variable for swapping character codes

j int Loop counter for sorting

OUTPUT:
Enter marks for student 1: 88

Enter character code for student 1: A

Enter marks for student 2: 92

Enter character code for student 2: B

Enter marks for student 3: 75

Enter character code for student 3: C

Enter marks for student 4: 85

Enter character code for student 4: D

Enter marks for student 5: 90

Enter character code for student 5: E

Merit List:

36 | P a g e
Character Code: C, Marks: 75

Character Code: D, Marks: 85

Character Code: A, Marks: 88

Character Code: E, Marks: 90

Character Code: B, Marks: 92

18.Write a program to print


O
OR
ORA
ORAN
ORANG
ORANGE
import java.util.Scanner;

class Prog {
public static void main() {
String s = "ORANGE";
int l = s.length();

for (int i = 1; i <= l; i++) {


System.out.println(s.substring(0, i)); // Print
substring from index 0 to I }}}

Variable Data Description


Name Type

s String Holds the string "ORANGE."

l int Length of the string s.

i int Counter for looping through


s.

37 | P a g e
OUTPUT
O
OR
ORA
ORAN
ORANG
ORANGE

19. Write a program to initialize a string "AN APPLE A


DAY" print number of words starting with 'A'.
public class CountWordsStartingWithA {
public static void main() {
String str = "AN APPLE A DAY".trim();
str = " " + str; // Add a space before the trimmed
string
int count = 0;

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


if (str.charAt(i) == 'A' && str.charAt(i - 1) == '
') {
count++;
}
}

System.out.println("Number of words starting with 'A':


" + count);
}
}

Variable Data Description


Name Type
str String Holds the input string with a space
added before it.

38 | P a g e
count int Stores the count of words that start
with 'A'.
i int Loop counter used to iterate through the
characters of the string.

OUTPUT:
Number of words starting with 'A': 3

20.Write a program a initialize a string “FLY HIGH IN


THE SKY” and find the number of words the end with Y.
public class CountWordsEndingWithY {
public static void main(String[] args) {
String str = "FLY HIGH IN THE SKY".trim();
str = str + " "; // Add a space at the end of the
string
int count = 0;

for (int i = 1; i < str.length(); i++) {


// Check if the current character is 'Y' and the
next character is a space
if (str.charAt(i) == 'Y' && str.charAt(i + 1) == '
') {
count++;
}
}

System.out.println("Number of words ending with 'Y': "


+ count);
}
}

39 | P a g e
Variabl Type Description
e
str Strin Stores the input string "FLY HIGH IN THE SKY"
g with an added space at the end
count int Counts the number of words that end with 'Y'
i int Used as the loop variable to iterate through
the string

OUTPUT:
Number of words ending with 'Y': 2

40 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy