0% found this document useful (0 votes)
11 views45 pages

Question 1

The document contains a series of programming questions and solutions related to Java, covering topics such as generating mathematical tables, displaying patterns, checking for prime numbers, calculating courier charges, sorting numbers, and generating a series of prime numbers. Each question includes an algorithm, coding examples, and variable data types with descriptions. The document serves as an educational resource for learning Java programming concepts and practices.

Uploaded by

izmaashraf5
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)
11 views45 pages

Question 1

The document contains a series of programming questions and solutions related to Java, covering topics such as generating mathematical tables, displaying patterns, checking for prime numbers, calculating courier charges, sorting numbers, and generating a series of prime numbers. Each question includes an algorithm, coding examples, and variable data types with descriptions. The document serves as an educational resource for learning Java programming concepts and practices.

Uploaded by

izmaashraf5
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/ 45

Computer

Application

Name: Izma Ashraf


Class: IX
Sec: C
Roll no: 21
Question 1.

Write a program to display the Mathematical Table from 5 to 10 for 10


iterations in the given format:

Sample Output: Table of 5

5*1 = 5

5*2 =10

--------

5*10 = 50

Algorithm:

1.Start.
2.Set i and j to 5 and less than 10.
3.Print "the table of" +i.
4.Loop through iteration from 1 to 10:
5.Calculate product = i*j.
6.Print in the format: i+ " * "+ j + " = " + product.
7.End.

Coding
public class Q1{

public static void main(){

int endtable=10;

for(int i=5;i<=endtable;i++){

System.out.println("The table of " + i);

for(int j=1;j<=endtable;j++){

int product=i*j;

System.out.println(i + " * "+ j + " = " + product) ; }

System.out.print("---------------------------") ; }

} }
VDT:

Variable Name Data Type Description


Holds the starting table
i Int
number (5).
Holds the ending table
Endtable Int
number (10).
Iterates from 1 to 10 to
j Int display each row in the
table.

Product int Stores the result of i* j

Output:

Question 2.

Write a menu driven program in Java to display the following


patterns:

(a) (b) (c) (d)


(e)

5 1 12345 7
1

54 21 2345 79
23
543 321 345 579
456

5432 4321 45 3579


7 8 9 10

54321 54321 5 13579


11 12 13 14 15

Algorithm:

1. Display a menu with pattern options (a, b, c, d, e).


2. Accept the user’s choice.
3. Use nested loops to generate the required structure.
4. Print the output row by row.
5. Repeat until the user decides to exit.

Coding:

import java.util.Scanner;

public class Q2{

public static void main() {

Scanner sc = new Scanner(System.in);

int choice; // Variable to store the user's menu choice

do {

// Displaying the menu options

System.out.println("\nChoose a pattern to display:");

System.out.println("1. Pattern (a)");

System.out.println("2. Pattern (b)");

System.out.println("3. Pattern (c)");

System.out.println("4. Pattern (d)");

System.out.println("5. Pattern (e)");

System.out.println("6. Exit");
System.out.print("Enter your choice: ");

choice = sc.nextInt();

switch (choice) {

case 1:

System.out.println("Pattern (a):");

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

for (int j = i; j >= 1; j--) {

System.out.print(j + " ");

System.out.println(); //Move to the next line after


each row

break;

case 2:

System.out.println("Pattern (b):");

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

for (int j = 5; j >= 6 - i; j--) {

System.out.print(j + " ");

System.out.println() ; }

break;

case 3:

System.out.println("Pattern (c):");

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

for (int j = i; j <= 5; j++) {

System.out.print(j + " ");


}

System.out.println();

break;

case 4:

System.out.println("Pattern (d):");

for (int i = 9; i >= 1; i -= 2) {

for (int j = i; j <= 9; j += 2) {

System.out.print(j + " ");

System.out.println();

break;

case 5:

System.out.println("Pattern (e):");

int num = 1;

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

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

System.out.print(num + " ");

num++;

System.out.println();

break;

case 6:

System.out.println("Exiting program. Goodbye!");


break;

default:

System.out.println("Invalid choice. Please try again.");

} }

while (choice != 6);

}}}

VDT:

Variable Name Data Type Description


Stores the user's menu
Choice int choice for the pattern to
display.
Loop control variable,
i int represents rows in the
patterns.
Loop control variable,
j int represents columns in
the patterns.
Represents the number
n int of rows for the patterns
(defaulted to 5).

Output:
Question 3.

Write a program to input a number and perform the following tasks:

(a) to check whether it is a prime number or not

(b) to reverse the number

If the number as well as the reverse is a ‘Prime’then display ‘Twisted Prime’

Otherwise ‘Not a twisted Prime’.

Sample Input: 167

Sample Output: 167 and 761 both are prime.

It is a ‘Twisted Prime’.

Algorithm:

1. Start
2. Accept a number as input.
3. Check if the number is prime using a helper method
isPrime.
4. Reverse the number using another helper method
reverseNumber.
5. Check if the reversed number is also prime.

6. If both the original and reversed numbers are prime,


display "Twisted Prime"; otherwise, display "Not a twisted
Prime".
7. End

Coding :

import java.util.Scanner; // Importing Scanner class for input


public class Q3{

// Method to check if a number is prime

static boolean isPrime(int n) {

if (n <= 1) return false; // Numbers <= 1 are not prime

for (int i = 2; i <= Math.sqrt(n); i++) { // Check divisibility up


to sqrt(n)

if (n % i == 0) return false; // If divisible by i, it's not prime

return true; // If no divisors found, it's prime

// Method to reverse a given number

static int reverseNumber(int n) {

int reverse = 0; // Variable to store reversed number

while (n != 0) {

reverse = reverse * 10 + n % 10; // Extract last digit and add


to reverse

n /= 10; // Remove last digit from n

return reverse; // Return the reversed number

public static void main() {

Scanner sc = new Scanner(System.in); // Scanner object for


user input

System.out.print("Enter a number: ");

int number = sc.nextInt(); // Input number

// Check if the original number is prime

boolean isOriginalPrime = isPrime(number);


int reversedNumber = reverseNumber(number); // Reverse the
number

boolean isReversedPrime = isPrime(reversedNumber); //


Check if the reversed number is prime

// Display results based on the prime checks

if (isOriginalPrime && isReversedPrime) {

System.out.println(number + " and " + reversedNumber + "


both are prime.");

System.out.println("It is a 'Twisted Prime'.");

else {

System.out.println("Not a twisted Prime.");

}}}

VDT:

Variable Name Data Type Description


Stores the input number
Num Int
to be checked.
Stores the
Reverse Int reversed
number.

Temporary variable used


Temp int for reversing the
number.

Stores whether the


isPrimenum boolean
input number is prime.
Stores whether the
reversed number is
isReversePrime boolean
prime.

Output:
Question 4.

A courier company charges differently for “Ordinary” booking and “Express”


booking

based on the weight of the parcel as per the tariff given below:

Weight of parcel Ordinary booking (in rupees) Express booking(in

rupees)
Up to 100 gm 80 100

101 to 500 gm 150 200

501 gm to 1000 gm 210 250

More than 1000 gm 250 300

Write a program to input weight of a parcel and type of booking (‘O’ for
ordinary and

‘E’ for express). Calculate and print the charges accordingly.

Algorithm:

1. Start.
2. Accept the weight of the parcel and type of booking (ordinary
or express).
3. Use conditional statements to determine the charge based on
the weight range and booking type.
4. Display the calculated charges.
5. End.

Coding:

import java.util.Scanner; // Importing Scanner class for input

public class Q4 {

public static void main() {

Scanner sc = new Scanner(System.in); // Scanner object for


user input

// Accept parcel weight and booking type from user

System.out.print("Enter the weight of the parcel (in grams):


");

int weight = sc.nextInt();

System.out.print("Enter type of booking (O for Ordinary, E


for Express): ");

char type = sc.next().charAt(0);

int charges = 0; // Variable to store charges

// Calculate charges based on weight and type


if (weight <= 100) {

charges = (type == 'O') ? 80 : 100;

} else if (weight <= 500) {

charges = (type == 'O') ? 150 : 200;

} else if (weight <= 1000) {

charges = (type == 'O') ? 210 : 250;

} else {

charges = (type == 'O') ? 250 : 300;

// Display the calculated charges

System.out.println("The courier charges are: Rs " +


charges);

}}

VDT:

Variable Name Data Type Description


Stores the weight of the
Weight Int
parcel.
Stores booking type,
bookingType Char
either ‘O’ or ‘E’.
charge Stores the calculated
Int charge based on
weight/type.

Output:
Question 5.

Write a program that accepts three numbers from the user and displays them
either in “Increasing Order” or in “Decreasing Order” as per the user’s choice.

Choice 1: Ascending order

Choice 2: Descending order

Sample Inputs: 394, 475, 296

Choice 2

Sample Output

First number : 475

Second number: 394

Third number : 296

The numbers are in decreasing order.

Algorithm:

1. Start.
2. Input three numbers: num1, num2, and num3.
3. Prompt user for sorting order (ascending/descending).
4. Sort and display the numbers in the chosen order.
5. End.

Coding:
import java.util.Scanner; // Import Scanner class for user input

public class Q5{

public static void main() {

Scanner sc = new Scanner(System.in); // Scanner object to


read user input

// Input three numbers from the user

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

int num1 = sc.nextInt();

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

int num2 = sc.nextInt();

System.out.print("Enter third number: ");

int num3 = sc.nextInt();

// Input user choice for sorting order

System.out.print("Enter choice (1 for Ascending, 2 for


Descending): ");

int choice = sc.nextInt();

// Display numbers in ascending order if choice is 1

if (choice == 1) {

if (num1 > num2) { int temp = num1; num1 = num2; num2


= temp; }

if (num2 > num3) { int temp = num2; num2 = num3; num3


= temp; }

if (num1 > num2) { int temp = num1; num1 = num2; num2


= temp; }

System.out.println("Ascending Order: " + num1 + ", " +


num2 + ", " + num3);

// Display numbers in descending order if choice is 2

else if (choice == 2) {
if (num1 < num2) { int temp = num1; num1 = num2; num2
= temp; }

if (num2 < num3) { int temp = num2; num2 = num3; num3


= temp; }

if (num1 < num2) { int temp = num1; num1 = num2; num2


= temp; }

System.out.println("Descending Order: " + num1 + ", " +


num2 + ", " + num3);

} else

System.out.println("Invalid choice."); // Invalid choice


message

} }}

VDT:

Data
Variable Name Description
Type
num1 Int Holds the first number.

num2 Int Holds the second number.

num3 Int Holds the third number.

Cho in
ice Stores the user's choice (1
t
for ascending, 2 for
descending).

Temporary variable used for


Temp int
swapping during sorting.

Output:
Question 6.

Design a class Prime_series with two methods:

 boolean getPrime(int n ) it checks and returns true if number n is prime


otherwise returns false. A prime number is a whole number greater than 1
that cannot be exactly divided by any whole number other than itself and
1.

Example: Input : 7

Output : Yes, 7 is a prime number as it is only divisible by 1 and itself.

 void printSeries() to generate first ten prime numbers by calling

getPrime(int n) method to determine if the number is prime or not.

[Hint: 2,3,5,7,11,13,17,19,23,29]

Algorithm:

1. Start.
2. Implement getPrime(int n) method to check if n is prime.
3. Implement printSeries() to call getPrime() and print the first
10 primes.

4. End.

Coding:

public class Q6{


// Method to check if a number is prime
static boolean getPrime(int n) {
if (n <= 1) return false; // Numbers less than 2 are not
prime
for (int i = 2; i <= Math.sqrt(n); i++) { // Check divisibility
if (n % i == 0) return false; // Not prime if divisible
}
return true; // Prime if no divisors found
}

// Method to print the first 10 prime numbers


static void printSeries() {
int count = 0; // Count of prime numbers printed
int num = 2; // Start checking from 2

while (count < 10) { // Continue until 10 primes are


printed
if (getPrime(num)) { // If num is prime, print it
System.out.print(num + " ");
count++; // Increment count of primes found
}
num++; // Check the next number
}
}

public static void main() {


System.out.print("First 10 prime numbers: ");
printSeries(); // Call method to print prime series
}}

VDT:

Data
Variable Name Description
Type

N Int Stores the number to be


checked for primality.

Tracks the number of prime


Count Int
numbers printed.

Stores the current number


Num Int
being checked for primality.

isPrime boolean Stores whether the current


number is prime.
Output:

Question 7.

Write a menu driven program to display all prime and non-prime numbers from
1 to 100.

Enter 1: to display all prime numbers

Enter 2: to display all non-prime numbers

Hint: A number is said to be prime if it is only divisible by 1 and the number


itself.

Algorithm:

1. Start.
2. Prompt user for choice to display primes or non-primes.
3. Based on choice, use a loop and a prime check to display the
selected numbers.
4. End.

Coding:

public class Q7{

// Method to check if a number is prime

static boolean isPrime(int n) {

if (n <= 1) return false;

for (int i = 2; i <= Math.sqrt(n); i++) {

if (n % i == 0) return false;

return true;

public static void main() {

Scanner sc = new Scanner(System.in);

// Input choice from the user

System.out.println("Enter 1 to display all prime


numbers from 1 to 100");

System.out.println("Enter 2 to display all non-prime


numbers from 1 to 100");

int choice = sc.nextInt();

if (choice == 1) {

System.out.print("Prime numbers from 1 to 100: ");

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

if (isPrime(i)) {

System.out.print(i + " ");


}}}

else if (choice == 2) {

System.out.print("Non-prime numbers from 1 to


100: ");

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

if (!isPrime(i)) {

System.out.print(i + " ");

} }

else {

System.out.println("Invalid choice.");

} }}
VDT:

Variable Name Data Type Description


Stores the user's choice
Choice int (1 for primes, 2 for non-
primes).
Loop control variable to
i int iterate through numbers
1 to 100.

Stores whether the


isPrime boolean
current number is
prime.

Output:
Question 8.

Write a program to input a three digit number. Use a method int Armstrong(int
n) to

accept the number and check it. The method returns 1, if the number is
Armstrong,

otherwise zero(0).

Sample Input: 153

Sample Output: 153 ⇒ 1 3 + 5 3 + 3 3 = 153

It is an Armstrong Number.

Algorithm:

1. Accept a three-digit number as input.


2. Define a method int Armstrong(int n):
3. Calculate the sum of the cubes of its digits.
4. Return 1 if the sum equals the original number, otherwise
return 0.
5. Check the result of the Armstrong method and display whether
the number is an Armstrong number.

Coding:
import java.util.Scanner; // Import Scanner class for user
input

public class Q8{

// Method to check if a number is an Armstrong


number

static int Armstrong(int n) {

int sum = 0, temp = n;

// Calculate the sum of cubes of the digits

while (temp > 0) {

int digit = temp % 10; // Extract last digit

sum += digit * digit * digit; // Add cube of the


digit

temp /= 10; // Remove last digit

// Return 1 if the number is Armstrong, otherwise 0

return (sum == n) ? 1 : 0;

public static void main() {

Scanner sc = new Scanner(System.in); // Scanner


object to read user input

// Input a three-digit number

System.out.print("Enter a three-digit number: ");

int num = sc.nextInt();

// Call Armstrong method and display result

if (Armstrong(num) == 1) {
System.out.println(num + " is an Armstrong
Number.");

else {

System.out.println(num + " is not an Armstrong


Number.");

} }}
VDT :

Variable Data Description


Name Type

n in Stores the input


t number to be
checked.
Sum int Stores the sum of
the cubes of the
digits of the
number.
Temp int Temporary variable
used to extract and
manipulate digits.
Digit int Stores the current
digit being
processed.

Output :
Question 9.

Write a program to input a number and check and print whether it is a Pronic
number or not. Use a method int Pronic(int n) to ace-=rt;]’ pt a number. The
method returns 1, if the number isPronic otherwise returns zero (0).

(Hint: Pronic number is the number which is the product of two consecutive
integers)

Examples:
12 = 3 * 4

20 = 4 * 5

42 = 6 * 7

Algorithm:
1. Accept a number as input.
2. Define a method int Pronic(int n):
3. Loop through numbers starting from 1.
4. Check if the product of consecutive integers equals the input
number.
5. Return 1 if the number is pronic, otherwise return 0.

6. Use the result of the Pronic method to display whether the


number is pronic.

Coding:

import java.util.Scanner; // Import Scanner class for user


input

public class Q9 {

// Method to check if a number is Pronic

static int Pronic(int n) {

for (int i = 1; i * (i + 1) <= n; i++) { // Loop through


integers

if (i * (i + 1) == n) { // Check if product of
consecutive integers equals n

return 1; // Return 1 if the number is pronic

}}

return 0; // Return 0 if not a pronic number

public static void main( {

Scanner sc = new Scanner(System.in); // Scanner


object to read user input
// Input a number from the user

System.out.print("Enter a number: ");

int num = sc.nextInt();

// Call Pronic method and display result

if (Pronic(num) == 1) {

System.out.println(num + " is a Pronic Number.");

else {

System.out.println(num + " is not a Pronic


Number.");

} }}

VDT:

Variable Data Descript


Name Type ion

n in Stores the input


t number to be
checked.
I int Loop control variable
to iterate through
potential factors.
isPronic boolean Stores whether the
number is a pronic
number.

Output:
Question 10.

Write a program to enter a two digit number and find out its first factor
excluding 1

(one). The program then find the second factor (when the number is divided
by the first

factor) and finally displays both the factors.

Hint: Use a non-return type method as void fact(int n) to accept the number.

Sample Input: 21
The first factor of 21 is 3

Sample Output: 3, 7

Sample Input: 30

The first factor of 30 is 2

Sample Output: 2, 15

Algorithm:

1. Start
2. Accept a two-digit number as input.
3. Define a method void fact(int n):
4. Loop to find the first factor (excluding 1).
5. Calculate the second factor as number / firstFactor.
6. Display both factors.
7. Call the method and display the result.
8. End

Coding:
import java.util.Scanner; // Import Scanner class for user input

public class Q10{

// Method to find and display factors

static void fact(int n) {

int firstFactor = 0;

// Loop to find the first factor excluding 1

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) { // Check if i is a factor

firstFactor = i;

break; // Stop once the first factor is found

}}

if (firstFactor != 0) {
int secondFactor = n / firstFactor; // Calculate the second
factor

System.out.println("First Factor: " + firstFactor);

System.out.println("Second Factor: " + secondFactor);

else {

System.out.println("No factors found other than 1 and " +


n);

public static void main() {

Scanner sc = new Scanner(System.in); // Scanner object to


read user input

// Input a two-digit number

System.out.print("Enter a two-digit number: ");

int num = sc.nextInt();

// Call fact method to display factors

fact(num);

}}

VDT:

Variable Data Descript


Name Type ion

N in Stores the input two-


t digit number.
Firstfactor int Stores the first
factor of the number
(excluding 1).
Secondfactor Int Stores the second
factor of the number.
I Int Loop control variable
to find the first
factor.

Output:

Question 11.

Write a program to input a letter. Find its ASCII code. Reverse the ASCII code
and

display the equivalent character.

Sample Input: Y

Sample Output: ASCII Code = 89

Reverse the code = 98

Equivalent character: b

Algorithm:

1. Start
2. Input
3. Accept a character from the user.
4. Convert to ASCII:
5. Convert the input character to its ASCII value and store it.
6. Reverse the ASCII Value:
7. Use a while loop to reverse the digits of the ASCII value.
8. Convert Back to Character:
9. Convert the reversed ASCII value back into a character.
10. Output Results:
11. Print the ASCII value of the input character.
12. Print the reversed ASCII value.
13. Print the character corresponding to the reversed ASCII
value.
14. End

Coding:

import java.util.*;

public class Q11{

public static void main(){

int rem=0; // Variable to store the remainder when reversing


the ASCII value

int rev=0; // Variable to store the reversed ASCII value

char ch1; // Variable to store the character equivalent of the


reversed ASCII

Scanner sc=new Scanner(System.in);

System.out.println("Enter a letter"); // Input: Prompt the user


to enter a character

char ch=sc.next().charAt(0);

int x=ch;

System.out.println("The ascii code for character is "+x);

int y=x;

while(x>10){

rem=x%10;

rev=rev*10+rem;

x=x/10;
}

System.out.println("The reverse of ascii code is"+ rev);

ch1=(char)rev;

System.out.println("The character equivalent for asciii code


is"+ch1);

} }

VDT:

Variable Data Descript


Name Type ion

Rem in Stores the remainder


t when calculating the
reverse of the ASCII
value.
Rev int

Stores the reversed


digits of the ASCII
value.
ch1 char Stores the character
equivalent of the
reversed ASCII value.
Ch char Stores the input
character entered by
the user.
x int Stores the ASCII
value of the input
character and is used
for manipulation.
y int Stores a copy of the
ASCII value of the
input character.

Output:
Question 12.

Write a menu driven program to generate the upper case letters from Z to A
and lowercase letters from a to z as per the user’s choice. Enter to display
upper case letters from Z to A and Enter; to display lower case letters from a
to z.

Algorithm :

1. Input:
2. Prompt the user to enter their choice:
a. 1: Display uppercase alphabets in reverse order (Z to A).
b. 2: Display lowercase alphabets in normal order (a to z).

3. Switch Case:
4. Use a switch statement to handle the user's choice.
5. For 1: Use a for loop to print uppercase alphabets in reverse
order.
6. For 2: Use a for loop to print lowercase alphabets in normal
order.
7. For any other input, display a message to enter a valid choice.

8. Output:
9. Print the alphabets based on the user's choice.

Coding:

import java.util.*;
public class Q12{

public static void main(){

Scanner sc=new Scanner(System.in);

System.out.println("Enter your choice");

int choice=sc.nextInt(); //read the user’s choice

switch(choice){ // Use a switch statement to handle the choice

case 1:

for(char ch='Z';ch>='A';ch--) { // Case 1: Print uppercase


alphabets in reverse order (Z to A)

System.out.println(ch); // Print the current character

break;

case 2:

for(char ch1='a';ch1<='z';ch1++){

System.out.println(ch1);

break;

default:

System.out.println("Enter the choice");

}} }

VDT:

Variable Data Descript


Name Type ion

choice in Stores the user's


t choice for displaying
alphabets..
ch1 Char Loop control variable
to iterate through
uppercase alphabets
(Z to A).
Ch Char Loop control variable
to iterate through
lowercase alphabets
(a to z).

Output:
Question 13.

Write a program to read ten characters from the user and display in upper
case if the

character entered is in lowercase and vice versa. Display a separate message


if the

entered character is not an alphabet. Terminate the code if at any point a


negative

number is entered.

Algorithm:

1. Input:
2. Prompt the user to enter 10 characters one by one.
3. Read each character in a loop.

4. Check Character:
5. If the character is '-', terminate the program with a message.
6. If the character is a digit (Character.isDigit(ch)), display "It is
a digit."
7. If the character is a letter (Character.isLetter(ch)):
a. Convert lowercase letters ('a' to 'z') to uppercase.
b. Convert uppercase letters ('A' to 'Z') to lowercase.
8. If the character is neither a digit nor a letter, classify it as a
special character.

9. Output Results:
10. Print the respective message or the converted character.

11. Terminate Early:


12. Stop the loop if a negative sign ('-') is encountered.

Coding:

import java.util.Scanner;

public class Q13 {

public static void main() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter 10 characters:"); // Prompt the user


to enter 10 characters

for (int i = 1; i <= 10; i++) { // Loop to process each of the 10


characters

char ch = sc.next().charAt(0);

if (ch == '-') { // Check if the character is a negative sign

System.out.println("Negative sign entered. Terminating


the program.");

break;

if (Character.isDigit(ch)) {

System.out.println("It is a digit");

} else if (Character.isLetter(ch)) {

if (ch >= 'a' && ch <= 'z') {

char ch1 = (char) (ch - 32); // Convert lowercase to


uppercase

System.out.println("The uppercase equivalent of the


character is " + ch1);

} else if (ch >= 'A' && ch <= 'Z') {

char ch2 = (char) (ch + 32); // Convert uppercase to


lowercase
System.out.println("The lowercase equivalent of the
character is " + ch2);

} }

else {

System.out.println("It is a special character."); }}}}

VDT:

Variable Data Descript


Name Type ion

Ch In Stores each
t character entered by
the user.
ch1 Char Stores the uppercase
equivalent of a
lowercase character.
ch2 Char
Stores the lowercase
equivalent of an
uppercase character.
I Int Loop control variable
to iterate through 10
inputs.

Output:
Question 14.

Write a program to input two characters from the keyboard. Find the
difference (d)

between their ASCII codes. Display the following messages:

If d=0 : both the characters are same.

If d&lt;0 : first character is smaller.

If d&gt;0 : second character is smaller.

Sample Input :

Sample Output :

d= (68-80) = -12

First character is smaller

Algorithm:

1. Input:
2. Prompt the user to enter two characters from the keyboard.

3. Convert Characters to ASCII:


4. Convert the entered characters to their ASCII values.

5. Compare ASCII Values:


6. Calculate the difference between the ASCII values of the two
characters.
7. If the difference is 0, both characters are the same.
8. If the difference is negative, the first character is smaller.
9. If the difference is positive, the second character is smaller.

10. Output Results:


11. Print the ASCII values of both characters.
12. Display the comparison results.

Coding:

import java.util.*;

public class Q14{


public static void main(){

Scanner sc=new Scanner(System.in);

System.out.println("Enter the two characters from the


keyboard");

char ch1=sc.next().charAt(0); / Read the first character

char ch2=sc.next().charAt(0);

int x= ch1; / /ASCII value of the first character

int y=ch2; // ASCII value of the second character

System.out.println("The ascii equivalent of ch1 is"+ x);

System.out.println("The ascii equivalent to ch2 is"+y);

int d=x-y;

if(d==0){

System.out.println("Both the characters are same");

else if(d<0){

System.out.println("First character is smaller");

else{

System.out.print("Second character is smaller");

}} }

VDT:

Variable Data Descript


Name Type ion

Ch1 ch Stores the first


ar character entered by
the user.
Ch2 Char
Stores the second
character entered by
the user.
x int Stores the ASCII
value of the first
character.
y int
Stores the ASCII
value of the second
character.

Output:

Question 15.

Write a program to input a set of 20 letters. Convert each letter into upper
case. Find and display the number of vowels and number of consonants
present in the set of given letters.

Algorithm:

1. Initialize Variables:
2. Create counters for vowels (vowelCount) and consonants
(consonantCount) and initialize them to 0.

3. Input:
4. Prompt the user to enter 20 characters, one at a time.

5. Process Each Character:


6. If the character is lowercase ('a' to 'z'), convert it to
uppercase.
7. Check if the character is a vowel ('A', 'E', 'I', 'O', 'U'):
8. If true, increment the vowel counter.
9. Otherwise, check if it is an uppercase letter ('A' to 'Z'):
10. If true, increment the consonant counter.

11. Output:
12. After processing each character, display the current count
of vowels and consonants.

Coding:

import java.util.Scanner;

public class Q15 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Initialize counters for vowels and consonants

int vowelCount=0;

int consonantCount=0;

System.out.println("Enter 20 letter");

for(int i=1;i<=20;i++){ // Loop to process 20 characters

char ch=sc.next().charAt(0);

if(ch>='a' && ch<='z'){

ch=(char)(ch-32);

}
if(ch=='A' || ch=='E' || ch=='I' || ch=='o' || ch=='U'){ //
Convert lowercase letters to uppercase

vowelCount++;

else if(ch>='A' && ch<='Z'){

consonantCount++;

System.out.println("No of vowels" +vowelCount);

System.out.print("No of consonants"+ consonantCount);

}}}

VDT:

Variable Data Descript


Name Type ion

vowelCount in Stores the


t count of vowels
consonantCount int
Stores the count of
consonants.
ch char Stores each
character entered by
the user during
iteration.
i int
Stores the ASCII
value of the second
character.

Output:

You might also like