Question 1
Question 1
Application
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{
int endtable=10;
for(int i=5;i<=endtable;i++){
for(int j=1;j<=endtable;j++){
int product=i*j;
System.out.print("---------------------------") ; }
} }
VDT:
Output:
Question 2.
5 1 12345 7
1
54 21 2345 79
23
543 321 345 579
456
Algorithm:
Coding:
import java.util.Scanner;
do {
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Pattern (a):");
break;
case 2:
System.out.println("Pattern (b):");
System.out.println() ; }
break;
case 3:
System.out.println("Pattern (c):");
System.out.println();
break;
case 4:
System.out.println("Pattern (d):");
System.out.println();
break;
case 5:
System.out.println("Pattern (e):");
int num = 1;
num++;
System.out.println();
break;
case 6:
default:
} }
}}}
VDT:
Output:
Question 3.
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.
Coding :
while (n != 0) {
else {
}}}
VDT:
Output:
Question 4.
based on the weight of the parcel as per the tariff given below:
rupees)
Up to 100 gm 80 100
Write a program to input weight of a parcel and type of booking (‘O’ for
ordinary and
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:
public class Q4 {
} else {
}}
VDT:
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 2
Sample Output
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
if (choice == 1) {
else if (choice == 2) {
if (num1 < num2) { int temp = num1; num1 = num2; num2
= temp; }
} else
} }}
VDT:
Data
Variable Name Description
Type
num1 Int Holds the first number.
Cho in
ice Stores the user's choice (1
t
for ascending, 2 for
descending).
Output:
Question 6.
Example: Input : 7
[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:
VDT:
Data
Variable Name Description
Type
Question 7.
Write a menu driven program to display all prime and non-prime numbers from
1 to 100.
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:
if (n % i == 0) return false;
return true;
if (choice == 1) {
if (isPrime(i)) {
else if (choice == 2) {
if (!isPrime(i)) {
} }
else {
System.out.println("Invalid choice.");
} }}
VDT:
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).
It is an Armstrong Number.
Algorithm:
Coding:
import java.util.Scanner; // Import Scanner class for user
input
return (sum == n) ? 1 : 0;
if (Armstrong(num) == 1) {
System.out.println(num + " is an Armstrong
Number.");
else {
} }}
VDT :
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.
Coding:
public class Q9 {
if (i * (i + 1) == n) { // Check if product of
consecutive integers equals n
}}
if (Pronic(num) == 1) {
else {
} }}
VDT:
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
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
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
int firstFactor = 0;
if (n % i == 0) { // Check if i is a factor
firstFactor = i;
}}
if (firstFactor != 0) {
int secondFactor = n / firstFactor; // Calculate the second
factor
else {
fact(num);
}}
VDT:
Output:
Question 11.
Write a program to input a letter. Find its ASCII code. Reverse the ASCII code
and
Sample Input: Y
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.*;
char ch=sc.next().charAt(0);
int x=ch;
int y=x;
while(x>10){
rem=x%10;
rev=rev*10+rem;
x=x/10;
}
ch1=(char)rev;
} }
VDT:
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{
case 1:
break;
case 2:
for(char ch1='a';ch1<='z';ch1++){
System.out.println(ch1);
break;
default:
}} }
VDT:
Output:
Question 13.
Write a program to read ten characters from the user and display in upper
case if the
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.
Coding:
import java.util.Scanner;
char ch = sc.next().charAt(0);
break;
if (Character.isDigit(ch)) {
System.out.println("It is a digit");
} else if (Character.isLetter(ch)) {
} }
else {
VDT:
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)
Sample Input :
Sample Output :
d= (68-80) = -12
Algorithm:
1. Input:
2. Prompt the user to enter two characters from the keyboard.
Coding:
import java.util.*;
char ch2=sc.next().charAt(0);
int d=x-y;
if(d==0){
else if(d<0){
else{
}} }
VDT:
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.
11. Output:
12. After processing each character, display the current count
of vowels and consonants.
Coding:
import java.util.Scanner;
int vowelCount=0;
int consonantCount=0;
System.out.println("Enter 20 letter");
char ch=sc.next().charAt(0);
ch=(char)(ch-32);
}
if(ch=='A' || ch=='E' || ch=='I' || ch=='o' || ch=='U'){ //
Convert lowercase letters to uppercase
vowelCount++;
consonantCount++;
}}}
VDT:
Output: