Computer Project SS
Computer Project SS
COMPUTER PROJECT
Class- ix
Primarily I would thank God for being able to complete this project
with success. Then I would like to thank my principal Mr. David George
Cyrill and the computer teacher Mr. Manish Mishra, whose valuable
guidance has been the ones that helped me patch this project and
make it full proof success. His suggestions and his instructions have
served as the major contributor toward the completion of the project.
Then I would like to thank my parents and the friends who have
helped me with their valuable suggestions and the guidance has been
very helped in various phrases of the completion of the project.
Last but not the least I would like to thank my classmates who
have helped me a lot.
int number ;
System.out.println(“Enter an integer : ”) ;
number = sc.nextInt() ;
if (testEven(number) == true)
System.out.println(number + “ is an EVEN number”) ;
else
System.out.println(number + “ is an ODD number”) ;
}
}
Output:
Enter an integer :
6
6 is an EVEN number
Enter an integer :
9
9 is an ODD number
Variable Description:
Data Variable Description
Type Name
int n Stores the value of the variable ‘number’
int number Stores the value from the user
***
Program 2
Write a program that takes an integer and tests
whether it is divisible by 2, 4, and 5. Apply
divisibility rules that say:
• A number is divisible by 2 if the number is ending in an
even digit 0, 2, 4, 6 or 8.
• A number is divisible by 4 if the last 2 digits of the
number are divisible by 4.
• A number is divisible by 5 if the number is ending in 0
or 5.
Code:
import java.util.Scanner ;
public class DivisiblityTest {
Output:
Enter an integer :
840
840 is divisible by all 2, 4 and 5
Enter an integer :
844
844 is NOT divisible by all 2, 4 and 5
Variable Description:
Data Variable Description
Type Name
boolean divby2 Stores the true/false value for the
‘lastDigit’ variable
boolean divby4 Stores the true/false value for the
‘last2Digits’ variable
boolean divby5 Stores the true/false value for the
‘lastDigit’ variable
int number Stores the value from the user
int lastDigit Stores the last 1 digit of the variable
‘number’
int last2Digits Stores the last 2 digits of the variable
‘number’
***
Program 3
Write a static method named median that
accepts three integers as parameters and that
returns the middle value of the three.
Code:
import java.util.Scanner ;
public class Test {
Output:
Enter 3 integers below
23 39 10
Median number of 3 given integers is 23
Variable Description:
Data Variable Description
Type Name
int n1 Stores the value from the user
int n2 Stores the value from the user
int n3 Stores the value from the user
int med Stores the median value
int x Stores the value of variable ‘n1’
int y Stores the value of variable ‘n2’
int z Stores the value of variable ‘n3’
***
Program 4
Write a program that reads a 4-digit year and
tests whether it is a leap year or not.
Code:
import java.util.Scanner ;
public class TestLeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
int year ;
System.out.println(“Enter a 4 digit year (yyyy) ”) ;
year = sc.nextInt() ;
if(year % 100 == 0) {
if (year % 400 == 0)
System.out.println(“Year ” + year + “ is a
leap year”) ;
else
System.out.println(“Year ” + year + “ is
not a leap year”) ;
}
else {
if (year % 4 == 0)
System.out.println(“Year ” + year + “ is a
leap year”) ;
else
System.out.println(“Year ” + year + “ is
not a leap year”) ;
}
}
}
Output:
Enter a 4 digit year (yyyy)
1900
Year 1900 is not a leap year
Enter a 4 digit year (yyyy)
2000
Year 2000 is a leap year
Enter a 4 digit year (yyyy)
2006
Year 2006 is not a leap year
Variable Description:
Data Variable Description
Type Name
int year Stores the value from the user
***
Program 5
Write a program to test whether 3 given sides
from a triangle or not.
Code:
import java.util.Scanner ;
public class TriangleTest {
public static boolean CanMake(int s1, int s2, int s3) {
boolean result ;
if (s1+s2 > s3 && s2+s3 > s1 && s3+s1 + s2)
result = true ;
else
result = false ;
return result ;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
int side1, side2, side3 ;
System.out.println(“Enter 3 sides ”) ;
side1 = sc.nextInt() ;
side2 = sc.nextInt() ;
side3 = sc.nextInt() ;
if (CanMake(side1, side2, side3) == true)
System.out.println(“Given 3 sides will make a
triangle”) ;
else
System.out.println(“Given 3 sides CANNOT make
a triangle”) ;
}
}
Output:
Enter 3 sides
10 14 12
Given 3 sides will make a triangle
Enter 3 sides
10 4 3
Given 3 sides CANNOT make a triangle
Variable Description:
Data Type Variable Name Description
int s1 Stores the value of 1st side of triangle
int s2 Stores the value of 2nd side of triangle
int s3 Stores the value of 3rd side of triangle
boolean result Stores the True/False value for If-condition
int side1 Stores the value from the user
int side2 Stores the value from the user
int side3 Stores the value from the user
***
Program 6
Write a program that takes three angles and
states whether these angles form an acute
triangle or an obtuse angle or a right-angled
triangle.
Code:
import java.util.Scanner ;
public class TriangleTest {
public static boolean CanMake(double a1, double a2,
double a3) {
boolean result ;
if (a1 + a2 + a3 == 180)
result = true ;
else
result = false ;
return result ;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in) ;
double angle1, angle2, angle3 ;
System.out.println(“Enter 3 angles ”) ;
System.out.print(“Angle1 : ”) ;
angle1 = sc.nextInt() ;
System.out.print(“Angle2 : ”) ;
angle2 = sc.nextInt() ;
System.out.print(“Angle3 : ”) ;
angle3 = sc.nextInt() ;
if (CanMake(angle1, angle2, angle3) == true)
{
if (angle1 > 90 || angle2 > 90 || angle3 > 90)
System.out.println(“Given 3 angles make an
Obtuse triangle”) ;
else if (angle1 < 90 && angle2 < 90 && angle3 < 90)
System.out.println(“Given 3 angles make an
Acute triangle”) ;
else if (angle1 == 90 || angle2 == 90 || angle3 == 90)
System.out.println(“Given 3 angles make an
Right-angled triangle”) ;
}
else
System.out.print(“Given 3 angles CANNOT make a
triangle”) ;
}
}
Output:
Enter 3 angles
Angle1 : 30
Angle2 : 40
Angle3 : 110
Given 3 angles make an Obtuse triangle
Enter 3 angles
Angle1 : 90
Angle2 : 60
Angle3 : 30
Given 3 angles make a Right-angled triangle
Enter 3 angles
Angle1 : 60
Angle2 : 60
Angle3 : 60
Given 3 angles make an Acute triangle
Variable Description:
Data Type Variable Name Description
double a1 Stores the value of 1st angle
double a2 Stores the value of 2nd angle
double a3 Stores the value of 3rd angle
boolean result Stores the True/False value for If-condition
double angle1 Stores the value from the user
double angle2 Stores the value from the user
double angle3 Stores the value from the user
***
Program 7
Write a program to read three sides of a triangle
(base, height, hypotenuse) and determine
whether they form a right-angled triangle or not.
Code:
import java.util.Scanner ;
public class TriangleTest {
public static boolean CanMake(int b, int h, int hy) {
boolean result ;
if (hy*hy == b*b + h*h)
result = true ;
else
result = false ;
result result ;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
int base, height, hypotenuse ;
System.out.println(“Enter 3 sides ”) ;
System.out.print(“Base : ”) ;
base = sc.nextInt() ;
System.out.print(“Height : ”) ;
height = sc.nextInt() ;
System.out.print(“Hypotenuse : ”) ;
hypotenuse = sc.nextInt() ;
if (CanMake(base, height, hypotenuse) == true)
System.out.println(“Given 3 sides make a Right-
angled triangle”) ;
else
System.out.println(“Given 3 sides CANNOT make
a Right-angled triangle”) ;
}
}
Output:
Enter 3 sides
Base : 2
Height : 3
Hypotenuse : 4
Given 3 sides CANNOT make a Right-angled triangle
Enter 3 sides
Base : 3
Height : 4
Hypotenuse : 5
Given 3 sides make a Right-angled triangle
Variable Description:
Data Type Variable Name Description
int b Stores the value of variable 'base'
int h Stores the value of variable 'height'
int hy Stores the value of variable 'hypotenuse'
boolean result Stores the True/False value for If-condition
int base Stores the value from the user
int height Stores the value from the user
int hypotenuse Stores the values from the user
***
Program 8
Write a program that reads the month number
and tells the number of days in the month.
Code:
import java.util.Scanner ;
class MonthsandYears {
public static void main(String[] args) {
int month, year, numDays = 0 ;
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31 ;
break ;
case 4:
case 6:
case 9:
case 11:
numDays = 30 ;
break ;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29 ;
else
numDays = 28 ;
default :
System.out.println(“Invalid month.”) ;
break ;
}
System.out.println(“Number of Days = ” + numDays) ;
}
}
Output:
Enter month (1..12) : 5
Enter year (yyyy) : 2000
Number of Days = 31
Enter month (1..12) : 2
Enter year (yyyy) : 2000
Number of Days = 29
Enter month (1..12) : 2
Enter year (yyyy) : 2013
Number of Days = 28
Variable Description:
Data Type Variable Name Description
int month Stores month number from the user
int year Stores year from the user
int numDays Stores the day number of different months
***
Program 9
Write a program to display menu for areas of
different shapes and then calculate area of
selected shape by asking for required
information.
Code:
import java.util.Scanner ;
public class AreaMenu {
public static void main(String[] args)
{
float a, b, area = 0 ;
int choice ;
Scanner kb = new Scanner(System.in) ;
case 2:
System.out.println(“Area of Square calculation”) ;
System.out.println(“Enter side: ”) ;
a = kb.nextFloat() ;
area = a*a ;
break ;
case 3:
System.out.println(“Area of Rectangle
calculation”) ;
System.out.println(“Enter length and breadth: ”) ;
a = kb.nextFloat() ;
b = kb.nextFloat() ;
area = a*b ;
break ;
case 4:
System.out.println(“Area of Triangle calculation”) ;
System.out.println(“Enter base and height: ”) ;
a = kb.nextFloat() ;
b = kb.nextFloat() ;
area = 0.5f*a*b ;
break ;
default:
System.out.println(“Valid choices are 1 to
4.”) ;
}
System.out.println(“Area = ” + area) ;
}
}
Output:
Shapes’ Area Menu
1. Circle.
2. Square.
3. Rectangle.
4. Triangle.
Enter your choice 1 to 4
1
Area of Circle calculation
Enter radius:
11
Area = 379.94
Variable Description:
Data Type Variable name Description
float a Stores a value from the user
float b Stores a value from the user
float area Stores the area
int choice Stores the choice of the user
***
Program 10
Write a program that prints the series showing
products of two consecutive numbers. Get the
starting number from user and stop the moment
product exceeds 100.
Code:
import java.util.Scanner ;
public class Numbers2 {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in) ;
int num = 0 ;
int result = 0 ;
System.out.print(“Enter starting number : ”) ;
num = kb.nextInt() ;
while(result < 100)
{
result = num*(num+1) ;
System.out.println(num + “ x ” + (num+1) + “ = ” +
result) ;
num = num =1
}
}
}
Output:
Enter starting number : 5
5 x 6 = 30
6 x 7 = 42
7 x 8 = 56
8 x 9 = 72
9 x 10 = 90
10 x 11 = 110
Variable Description:
Data Type Variable Name Description
int num Stores a value from the user
int result Stores the product of two consecutive
numbers
***
Program 11
Write a program to print sum of digits of a given
number.
Code:
public class Test {
static int SumDigits(int num) {
int num2 = num ;
int dig, q, sum = 0 ;
while(num2 > 0) {
q = num2 / 10 ;
dig = num2 % 10 ;
sum = sum + dig ;
num2 = q ;
}
return sum ;
}
public static void main(String args[]) {
int n1 = 345, n2 = 672, n3 = 13602 ;
int s = 0 ;
s = SumDigits(n1) ;
System.out.println(“Sum of digits of ” +n1+ “ is : ” +s) ;
s = SumDigits(n2) ;
System.out.println(“Sum of digits of ” +n2+ “ is : ” +s) ;
s = SumDigits(n3) ;
System.out.println(“Sum of digits of ” +n3+ “ is : ” +s) ;
}
}
Output:
Sum of digits of 345 is : 12
Sum of digits of 672 is : 15
Sum of digits of 13602 is : 12
Variable Description:
Data Type Variable Name Description
int num Stores a value
int num2 Stores the value of variable ‘num’
int dig Stores the last digit of variable ‘num’
int q Stores the first digit of variable ‘num’
int sum Stores the sum
int n1 Stores a value
int n2 Stores a value
int n3 Stores a value
int s Stores the final sum
***
Program 12
Write a program to count and display divisors of a
number.
Code:
import java.util.Scanner ;
public class Test {
static void genDivisors(int num) {
int i = 2, count = 2, lim = num/2 ;
System.out.println(“Other than the number itself and
1, the divisors of ” +num+ “ are: ”) ;
while(i <= lim) {
if(num % i == 0)
{
count++ ;
System.out.println(i) ;
}
i++ ;
}
System.out.println(“Total number of divisors : ” +
count) ;
}
Public static void main(String args[]) {
Scanner kb = new Scanner(System.in) ;
System.out.print(“Enter the number whose divisors
are to be generated : ”) ;
int num = kb.nextInt() ;
genDivisors(num) ;
}
}
Output:
Enter the number whose divisors are to be generated : 128
Other than the number itself and 1, the divisors of 128 are :
2
4
8
16
32
64
Total number of divisors : 8
Variable Description:
Data Type Variable Name Description
int num Stores a value from the user
int i Stores different values
int count Stores the number of divisors
int lim Stores the divisors
Program 13
Write a program to find whether given number is
Armstrong or not.
Code:
import java.util.Scanner ;
public cclass Armstrong {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in) ;
System.out.print(“enter a 3digit number to be
checked : ”) ;
int num = sc.nextInt() ;
int n = num ;
int check = 0, remainder ;
do {
remainder = n % 10 ;
check = check + (int)Math.pow(remainder,3) ;
n = n / 10 ;
} while(n != 0) ;
if(check == num)
System.out.print(num + “ is an Armstrong Number”) ;
else
System.out.print(num + “ is not an Armstrong
Number”) ;
}
}
Output:
Enter a 3digit number to be checked : 345
345 is not a Armstrong Number
Enter a 3digit number to be checked : 153
153 is a Armstrong Number
Enter a 3digit number to be checked : 407
407 is a Armstrong Number
Variable Description:
Data Type Variable Name Description
int num Stores a value from the user
int n Stores the value of variable ‘num’
int check Stores the value of an equation
int remainder Stores a digit from variable 'n'
***
Program 14
Write a program to test if a number is a
palindrome or not.
Code:
impost java.util.Scanner ;
public class Palindrome {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in) ;
System.out.print(“Enter a number to be checked : ”) ;
int number = sc.nextInt() ;
int reversedNumber = 0, temp = 0, savedNumber ;
savedNumber = number ;
do {
temp = number % 10 ;
number = number / 10 ;
reversedNumber = reversedNumber * 10 + temp ;
} while(number != 0) ;
if(reversedNumber == savedNumber)
System.out.println(savedNumber + “ is a
palindrome”) ;
else
System.out.println(savedNumber + “ not a
palindrome”) ;
}
}
Output:
Enter a number to be checked : 1221
1221 is a palindrome
Enter a number to be checked : 1212
1212 not a palindrome
Variable Description:
Data Variable Name Description
Type
int number Stores a value from the user
int reversedNumber Stores the reversed form of the value of
variable ’number’
int temp Stores a digit of value of variable ‘number’
int savedNumber Stores the value of variable 'number'
***
Program 15
Write a menu program that reads the radius of a
circle and displays options to calculate area or
perimeter of the circle and depending upon
choice, does the same.
Code:
import java.util.Sacnner ;
public class Menu {
public static void main(String args[]) {
public radius, area, perm ;
int choice ;
Scanner sc = new Scanner(System.in) ;
System.out.print(“Enter radius of the circle : ”) ;
radius – sc.nextDouble() ;
do {
System.out.println(“\t CIRCLE OPTIONS”) ;
System.out.println(“1. Calculate Area”) ;
System.out.println(“2. Calculate Perimeter”) ;
System.out.println(“3. Exit”) ;
System.out.print(“Enter your choice (1/2/3) : ”) ;
choice = sc.nextInt() ;
switch(choice) {
case 1: area = Math.PI*radius*radius ;
System.out.println(“Area of circle is”
+area) ;
break ;
case 2: perm = 2*Math.PI*radius ;
System.out.println(“Perimeter of
circle is ” + perm) ;
break ;
case 3: break ;
default: System.out.println(“Invalid choice”);
break ;
}
} while (choice != 3) ;
}
}
Output:
Enter radius of the circle : 6.25
CIRCLE OPTIONS
1. Calculate Area
2. Calculate Perimeter
3. Exit
Enter your choice (1/2/3) : 1
Area of circle is 122.7184630308513
CIRCLE OPTIONS
1. Calculate Area
2. Calculate Perimeter
3. Exit
Enter your choice (1/2/3) : 2
Perimeter of circle is 39.269908169872416
Variable Description:
Data Type Variable Name Description
double radius Stores a value from the user
double area Stores the area
double perm Stores the perimeter
int choice Stores the choice of the user
***
Program 16
Write a program to input a number and test if it is
a prime number.
Code:
import java.util.Scanner ;
public class Prime {
public static void main(String args[]) {
int number ;
Scanner sc = new Scanner(System.in) ;
System.out.println(“Enter number : ”) ;
number = sc.nextInt() ;
int n = 2, mid = number / 2 ;
boolean primetest = true ;
do {
if (number % n == 0)
{ System.out.println(“A factor ” +n+ “ of ”
+number+ “ found!”) ;
System.out.println(“Hence NOT a prime
number!!”) ;
primetest = false ;
break ;
}
n++ ;
} while (n <= med) ;
if (primetest == true)
System.out.println(number +“ is a prime
number!!”) ;
}
}
Output:
Enter number : 15
A factor 3 is of 15 found!
Hence NOT a prime number!!
Enter number : 17
17 is a prime number!!
Variable Description:
Data Type Variable Name Description
int number Stores a value from the user
int n Stores a value
int mid Stores the half value of variable ‘number’
boolean primetest Stores the True/False value of prime
number test
***
Program 17
Write a program to print a patter like:
****1
***22
**333
*4444
55555
Code:
public class Patter
{
void print()
{
int NOL = 5 ;
for (int line = 1; line <= 5; line++)
{
for (int j = 1; j <= (NOL - line) ; j++) {
System.out.print(“*”) ;
}
for (int k = 1; k < line; k++)
{
System.out.print(line) ;
}
System.out.println(line) ;
}
}
}
Output:
****1
***22
**333
*4444
55555
Variable Description:
Data Type Variable Name Description
int NOL Stores a value
int line Stores a value
int j Stores a value
int k Stores a value
***
program 18
Write a program to print a pattern as shown
below:
****1
***2*
**3**
*4***
5****
Code:
public class Patter
{
void print()
{
int NOL = 5 ;
for (int line = 1; line <= 5; line++)
{
for (int j = 1; j <= (NOL - line) ; j++)
{
System.out.print(“*”) ;
}
System.out.print(line) ;
for (int j = 1; j < line; j++)
{
System.out.print(“*”) ;
}
System.out.println() ;
}
}
}
Output:
****1
***2*
**3**
*4***
5****
Variable Description:
Data Type Variable Name Description
int NOL Stores a value
int line Stores a value
int j Stores a value
***
Program 19
Write a program to print a pattern like:
****1
***2
**3
*4
5
Code:
public class Patter
{
void print()
{
int NOL = 5 ;
for (int line = 1; line <= 5; line++)
{
for (int j = 1; j <= (NOL - line) ; j++)
{
System.out.print(“*”) ;
}
System.out.print(line) ;
}
}
}
Output:
****1
***2
**3
*4
5
Variable Description:
Data Type Variable Name Description
int NOL Stores a value
int line Stores a value
int j Stores a value
***
Program 20
Write a program to obtain two numbers and
display their absolute value, ceiling numbers and
floor numbers.
Code:
import java.util.Sacnner ;
public class someMathFn
{
void compute()
{
Scanner sc = new Scanner(System.in) ;
double num1, num2 ;
System.out.println(“Enter first number: ”) ;
num1 = sc.nextDouble() ;
System.out.println(“Enter second number: ”) ;
num2 = sc.nextDouble() ;
System.out.println(“For ” +num1+ “ : ”) ;
System.out.println(“ Ceiling = ” +Math.ceil(num1)) ;
System.out.println(“ Floor = ” +Math.floor(num1)) ;
System.out.println(“ Absolute value = ”
+Math.abs(num1)) ;
System.out.println(“For ” +num2+ “ : ”) ;
System.out.println(“ Ceiling = ” +Math.ceil(num2)) ;
System.out.println(“ Floor = ” +Math.floor(num2)) ;
System.out.println(“ Absolute value = ”
+Math.abs(num2)) ;
}
}
Output:
Enter first number: 17.63
Enter second number: -34.03
For 17.63 :
Ceiling = 18.0
Floor = 17.0
Absolute value = 17.63
For -34.03
Ceiling = -34.0
Floor = -35.0
Absolute value = 34.03
Variable Description:
Data Type Variable Name Description
double num1 Stores a value from the user
double num2 Stores a value from the user
***
Bibliography
• DHANPAT RAI & CO. (Pvt.) Ltd. ICSE Computer
Applications Class IX, Sumita Arora