0% found this document useful (0 votes)
14 views12 pages

Aishiki Computer Programs

The document contains multiple Java programs that cover various topics such as railway ticket booking, checking composite numbers, printing patterns, calculating Tribonacci series, and more. Each program includes user input, logic for calculations, and output display, along with variable descriptions for clarity. The programs are structured with methods for specific functionalities and utilize a menu-driven approach for user interaction.
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)
14 views12 pages

Aishiki Computer Programs

The document contains multiple Java programs that cover various topics such as railway ticket booking, checking composite numbers, printing patterns, calculating Tribonacci series, and more. Each program includes user input, logic for calculations, and output display, along with variable descriptions for clarity. The programs are structured with methods for specific functionalities and utilize a menu-driven approach for user interaction.
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/ 12

QUESTION 1:

/* Name: Aishiki <your surname>


Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class RailwayTicket {

// Data members
private String name; // To store the customer's name
private String coach; // To store the type of coach
private long mobNo; // To store the customer's mobile number
private int amt; // To store the base ticket amount
private int totalAmt; // To store the updated total amount

// Method to input details and calculate total amount


public void inputDetails() {
Scanner sc = new Scanner(System.in);

// Taking customer details as input


System.out.print("Enter your name: ");
name = sc.nextLine();

System.out.print("Enter your mobile number: ");


mobNo = sc.nextLong();

System.out.print("Enter the type of coach (First_AC/Second_AC/Third_AC/Sleeper): ");


sc.nextLine(); // Consume the newline character
coach = sc.nextLine();

// Determine base ticket amount based on coach type


switch (coach) {
case "First_AC":
amt = 700;
break;
case "Second_AC":
amt = 500;
break;
case "Third_AC":
amt = 250;
break;
case "Sleeper":
amt = 0;
break;
default:
System.out.println("Invalid coach type! Setting amount to 0.");
amt = 0;
}

// Update total amount (logic can be extended for additional charges)


totalAmt = amt;
}

// Method to display ticket details


public void displayDetails() {
System.out.println("\nTicket Details:");
System.out.printf("Name : %s\n", name);
System.out.printf("Mobile No. : %d\n", mobNo);
System.out.printf("Coach : %s\n", coach);
System.out.printf("Ticket Amount : ₹%d\n", totalAmt);
}

// Main method to execute the program


public static void main(String[] args) {
RailwayTicket ticket = new RailwayTicket();

// Input details and calculate total amount


ticket.inputDetails();

// Display ticket details


ticket.displayDetails();
}
}

/*
Variable Description:
1. name : String to store the name of the customer.
2. coach : String to store the type of coach the customer selects.
3. mobNo : long to store the mobile number of the customer.
4. amt : int to store the basic ticket amount based on coach type.
5. totalAmt : int to store the total amount after calculations (currently same as amt).
*/

QUESTION 2:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class MenuDrivenProgram {

// Method to check if a number is composite


public static boolean isComposite(int num) {
if (num <= 1) {
return false; // Numbers <= 1 are not composite
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return true; // Found a factor other than 1 and the number itself
}
}
return false; // No factors found, hence not composite
}

// Method to find the sum of digits of a number


public static int sumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
}
return sum;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Display menu options


System.out.println("Menu:");
System.out.println("1. Check if a number is composite");
System.out.println("2. Find the sum of digits of a number");
System.out.println("Enter your choice:");
int choice = sc.nextInt(); // User's menu choice

switch (choice) {
case 1: // Check if a number is composite
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (isComposite(number)) {
System.out.println(number + " is a composite number.");
} else {
System.out.println(number + " is not a composite number.");
}
break;

case 2: // Find the sum of digits of a number


System.out.print("Enter a number: ");
int digitNumber = sc.nextInt();
int sum = sumOfDigits(digitNumber);
System.out.println("Sum of digits is: " + sum);
break;

default: // Handle invalid choice


System.out.println("Error: Invalid choice. Please select a valid option.");
}

sc.close(); // Close the scanner


}

/*
Variable Description:
1. num : int, used to store the input number for operations.
2. choice : int, used to store the user's menu choice.
3. sum : int, used to store the sum of digits of a number.
4. digitNumber : int, the number input for digit sum calculation.
*/
}

QUESTION 3:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class MenuDrivenPatternsTables {

// Method to print the letter triangle pattern


public static void printLetterTriangle() {
System.out.println("Letter Triangle:");
for (char row = 'A'; row <= 'E'; row++) {
char letter = 'A';
for (char col = 'A'; col <= row; col++) {
System.out.print(letter + "\t");
letter++;
}
System.out.println();
}
}

// Method to print multiplication tables from 1 to 12


public static void printMultiplicationTables() {
System.out.println("Multiplication Tables (1 to 12):");
for (int i = 1; i <= 12; i++) {
System.out.println("Table of " + i + ":");
for (int j = 1; j <= 10; j++) {
System.out.printf("%d × %d = %d\n", i, j, i * j); // × is the multiplication symbol
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Display menu options


System.out.println("Menu:");
System.out.println("1. Print Letter Triangle Pattern");
System.out.println("2. Print Multiplication Tables from 1 to 12");
System.out.print("Enter your choice: ");

int choice = sc.nextInt(); // User's menu choice

switch (choice) {
case 1:
printLetterTriangle();
break;

case 2:
printMultiplicationTables();
break;

default:
System.out.println("Error: Invalid choice. Please select a valid option.");
}

sc.close(); // Close the scanner


}

/*
Variable Description:
1. row, col : char, used for printing rows and columns of the letter triangle pattern.
2. i, j : int, used for iterating through multiplication tables from 1 to 12 and 1 to 10 respectively.
3. choice : int, used to store the user's menu choice.
*/
}

QUESTION 4:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class TribonacciSeries {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Input the number of terms


System.out.print("Enter the number of terms (n): ");
int n = sc.nextInt();

if (n <= 0) {
System.out.println("Please enter a positive integer.");
} else {
// Print the Tribonacci series
System.out.println("The Tribonacci series up to " + n + " terms is:");

// Initializing the first three terms


int a = 0, b = 0, c = 1;
System.out.print(a + " " + b + " " + c + " ");

for (int i = 4; i <= n; i++) {


int next = a + b + c; // Next term is the sum of the last three terms
System.out.print(next + " ");
a = b; // Shift terms
b = c;
c = next;
}

System.out.println(); // Print a new line after the series


}

sc.close(); // Close the scanner


}

/*
Variable Description:
1. n : int, the number of terms in the Tribonacci series to be printed.
2. a : int, the first term of the series (initially 0).
3. b : int, the second term of the series (initially 0).
4. c : int, the third term of the series (initially 1).
5. next: int, stores the current term being calculated.
6. i : int, loop counter for iterating through terms after the first three.
*/
}
QUESTION 5:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class PronicNumberCheck {

public static boolean isPronic(int num) {


for (int i = 0; i * (i + 1) <= num; i++) {
if (i * (i + 1) == num) {
return true; // Number is Pronic
}
}
return false; // Number is not Pronic
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Input the number to check


System.out.print("Enter a number: ");
int num = sc.nextInt();

// Check and print whether the number is Pronic or not


if (isPronic(num)) {
System.out.println(num + " is a Pronic number.");
} else {
System.out.println(num + " is not a Pronic number.");
}

sc.close(); // Close the scanner


}

/*
Variable Description:
1. num : int, the number to check for being Pronic.
2. i : int, loop counter used to find consecutive integers whose product equals num.
*/
}
QUESTION 6:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class SeriesSum {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Input the number of terms


System.out.print("Enter the number of terms (n): ");
int n = sc.nextInt();

// Input the value of x


System.out.print("Enter the value of x: ");
int x = sc.nextInt();

int sum = 0; // Variable to store the sum of the series


// Calculate the sum of the series
for (int i = 1; i <= n; i++) {
sum += Math.pow(x, i); // Add the ith power of x to the sum
}

// Print the result


System.out.println("The sum of the series S = x1 + x2 + x3 + ... + xn is: " + sum);

sc.close(); // Close the scanner


}

/*
Variable Description:
1. n : int, the number of terms in the series.
2. x : int, the base value for each term in the series.
3. sum : int, stores the cumulative sum of the series.
4. i : int, loop counter for iterating through the terms of the series.
*/
}
QUESTION 7:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

public class StarPattern {

public static void main(String[] args) {


// Number of rows for the pattern
int rows = 5;

// Loop to print the pattern


for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println(); // Move to the next line after each row
}
}

/*
Variable Description:
1. rows : int, the number of rows in the pattern.
2. i, j : int, loop counters for rows and stars in the pattern.
*/
}
QUESTION 8:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class TriangleAndSeries {

// Method to print a triangle pattern


public static void printTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}

// Method to print an inverted triangle pattern


public static void printInvertedTriangle(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}

// Method to print the series: 2, 5, 10, 17, 26, ...


public static void printSeries(int n) {
int term = 2;
for (int i = 1; i <= n; i++) {
System.out.print(term + " ");
term += (2 * i + 1); // Update term based on the series logic
}
System.out.println();
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Menu:");
System.out.println("1. Generate a triangle or inverted triangle");
System.out.println("2. Print the series: 2, 5, 10, 17, 26...");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter the number of terms (n): ");
int n = sc.nextInt();
System.out.println("Choose type of triangle: ");
System.out.println("1. Triangle");
System.out.println("2. Inverted Triangle");
System.out.print("Enter your choice: ");
int type = sc.nextInt();

if (type == 1) {
printTriangle(n);
} else if (type == 2) {
printInvertedTriangle(n);
} else {
System.out.println("Invalid choice for triangle type.");
}
break;

case 2:
System.out.print("Enter the number of terms (n): ");
int terms = sc.nextInt();
printSeries(terms);
break;

default:
System.out.println("Invalid choice. Please select a valid option.");
}

sc.close();
}

/*
Variable Description:
1. n : int, the number of terms for the triangle or series.
2. choice: int, the user's menu selection.
3. type : int, the type of triangle pattern (1 for normal, 2 for inverted).
4. term : int, stores the current term in the series.
*/
}
QUESTION 9:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

public class SeriesSum {

public static void main(String[] args) {


double sum = 0.0; // Variable to store the sum of the series

// Loop through the terms of the series


for (int i = 1; i <= 49; i++) {
sum += (double) i / (i + 1); // Add the current term to the sum
}

// Print the result


System.out.printf("The sum of the series S = 1/2 + 2/3 + 3/4 + ... + 49/50 is: %.4f\n", sum);
}

/*
Variable Description:
1. sum : double, used to store the cumulative sum of the series.
2. i : int, loop counter to represent the numerator of the current term.
*/
}
QUESTION 10:
/* Name: Aishiki <your surname>
Class : 9 Section : <your section>
Roll number <roll no> */

import java.util.*;

public class MenuDrivenSpyNiven {

// Method to check if a number is a spy number


public static boolean isSpyNumber(int num) {
int sum = 0, product = 1;
while (num > 0) {
int digit = num % 10; // Extract last digit
sum += digit; // Add digit to sum
product *= digit; // Multiply digit to product
num /= 10; // Remove last digit
}
return sum == product; // Check if sum and product are equal
}

// Method to check if a number is a NIVEN number


public static boolean isNivenNumber(int num) {
int sum = 0, originalNum = num;
while (num > 0) {
int digit = num % 10; // Extract last digit
sum += digit; // Add digit to sum
num /= 10; // Remove last digit
}
return (sum != 0 && originalNum % sum == 0); // Check divisibility by sum of digits
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Display menu options


System.out.println("Menu:");
System.out.println("1. Check if a number is a Spy Number");
System.out.println("2. Check if a number is a NIVEN Number");
System.out.print("Enter your choice: ");

int choice = sc.nextInt(); // User's menu choice

switch (choice) {
case 1: // Check for Spy Number
System.out.print("Enter a number: ");
int spyNum = sc.nextInt();
if (isSpyNumber(spyNum)) {
System.out.println(spyNum + " is a Spy Number.");
} else {
System.out.println(spyNum + " is not a Spy Number.");
}
break;

case 2: // Check for NIVEN Number


System.out.print("Enter a number: ");
int nivenNum = sc.nextInt();
if (isNivenNumber(nivenNum)) {
System.out.println(nivenNum + " is a NIVEN Number.");
} else {
System.out.println(nivenNum + " is not a NIVEN Number.");
}
break;

default: // Handle invalid choice


System.out.println("Error: Invalid choice. Please select a valid option.");
}

sc.close(); // Close the scanner


}

/*
Variable Description:
1. num : int, used for storing input number for operations.
2. sum, product : int, used for calculating the sum and product of digits.
3. originalNum : int, stores the original input number for divisibility checks.
4. choice : int, stores the user's menu selection.
*/
}

You might also like