COMPUTER
PROJECT
NAME:THIRUMURUGA
N CLASS: IX C
CODE 1 :
AIM :
To write a program to display the color of the spectrum
(VIBGYOR) according to the user's choice.
CODE:
import java.util.Scanner;
public class SpectrumColor
{
public static void main()
{
// Create a scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Display a prompt to the user
System.out.println("Enter a letter representing a color from
the VIBGYOR spectrum (V/I/B/G/Y/O/R): ");
char choice = scanner.next().toUpperCase().charAt(0); //
Read and convert to uppercase for uniformity
// Use a switch statement to determine the color
switch (choice) {
case 'V':
System.out.println("The color is Violet.");
break;
case 'I':
System.out.println("The color is Indigo.");
break;
case 'B':
System.out.println("The color is Blue.");
break;
case 'G':
System.out.println("The color is Green.");
break;
case 'Y':
System.out.println("The color is Yellow.");
break;
case 'O':
System.out.println("The color is Orange.");
break;
case 'R':
System.out.println("The color is Red.");
break;
default:
System.out.println("Invalid input. Please enter a valid
letter from VIBGYOR.");
}
}
}
OUTPUT:
CODE 2 :
AIM:
Write a program in Java to compute the perimeter and area of a
triangle.
CODE:
import java.util.Scanner;
public class TriangleProperties {
public static void main()
{
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the sides of the triangle
System.out.print("Enter side a of the triangle: ");
double a = scanner.nextDouble();
System.out.print("Enter side b of the triangle: ");
double b = scanner.nextDouble();
System.out.print("Enter side c of the triangle: ");
double c = scanner.nextDouble();
// Calculate the perimeter
double perimeter = a + b + c;
// Calculate the semi-perimeter (s)
double s = perimeter / 2;
// Calculate the area using Heron's formula
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
// Display the results
System.out.println("The perimeter of the triangle is: " +
perimeter);
System.out.println("The area of the triangle is: " + area);
}
}
OUTPUT:
CODE 3 :
AIM:
Write a menu driven program to accept a number and check and display
whether it is a an Automorphic Number or not.
CODE:
import java.util.Scanner;
public class AutomorphicNumberChecker {
public static void main() {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Display the menu to the user
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Check if a number is Automorphic");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
// Accept the number from the user to check if it is
Automorphic
System.out.print("Enter a number to check if it is
Automorphic: ");
int number = scanner.nextInt();
// Check if the number is Automorphic
if (isAutomorphic(number)) {
System.out.println(number + " is an Automorphic
number.");
} else {
System.out.println(number + " is not an Automorphic
number.");
}
break;
case 2:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 2); // Continue the loop until the user selects
option 2 to exit
// Close the scanner object
scanner.close();
}
// Method to check if the number is Automorphic
public static boolean isAutomorphic(int num) {
int square = num * num; // Find the square of the number
int temp = num;
// Find the number of digits in num
int digits = 0;
while (temp != 0) {
digits++;
temp /= 10;
}
// Extract the last 'digits' digits of the square
int lastDigits = square % (int)Math.pow(10, digits);
// Check if the last digits of the square are the same as the number
return num == lastDigits;
}
}
OUTPUT:
CODE 4 :
AIM:
Write a program to input a number N and print the table of N up to 10.
CODE:
import java.util.Scanner;
public class MultiplicationTable {
public static void main() {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to input a number N
System.out.print("Enter a number to print its multiplication table: ");
int N = scanner.nextInt();
// Print the multiplication table of N up to 10
System.out.println("Multiplication Table of " + N + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(N + " x " + i + " = " + (N * i));
}
}
}
OUTPUT:
CODE 5 :
AIM:
Write a program to input a number and check and print whether it is a
Pronic number or not. (Pronic number is a number which is the product of
two consecutive integers.)
CODE:
public class PronicNumber {
// Function to check if a number is a Pronic number
public static boolean isPronicNumber(int num) {
for (int i = 0; i * (i + 1) <= num; i++) {
if (i * (i + 1) == num) {
return true; // It's a Pronic number
}
}
return false; // Not a Pronic number
}
public static void main() {
Scanner scanner = new Scanner(System.in);
// Input a number from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is a Pronic number
if (isPronicNumber(number)) {
System.out.println(number + " is a Pronic number.");
} else {
System.out.println(number + " is not a Pronic number.");
}
}
}
OUTPUT: