IX COMPUTER PROJECT
A) PROGRAM USING ASSIGNMENT STATEMENTS:
QUESTION-1: Write a Java program to swap two values.
class Swap
{
int a=10, b=20, c;
void interchange( )
{
[Link]("Before swapping a=" + a + " b=" + b);
c = a;
a = b;
b = c;
[Link]("After swapping a=" + a + " b=" + b);
}
public static void main( )
{
Swap obj = new Swap( );
[Link]( );
}
}
LIST OF VARIABLES:
[Link] VARIABLE NAME DATA TYPE DESCRIPTION
1. a int to assign value
2. b int to assign value
3. c int to interchange
B) PROGRAM BASED ON - INPUT THROUGH PARAMETERS:
QUESTION-2: Write a Java program to find the area of a triangle.
class Area
{
void areaOfTriangle(int b, int h)
{
double ar;
ar = 1.0/2 * b * h;
[Link]("AREA=" + ar);
}
public static void main( )
{
Area aa = new Area( );
int base = 10, height = 50;
[Link](base, height);
}
}
LIST OF VARIABLES:
[Link] VARIABLE NAME DATA TYPE DESCRIPTION
1. base int to assign value
2. height int to assign value
3. b, h int to pass as arguments
4. ar double to calculate area
C) PROGRAM BASED ON - INPUT THROUGH SCANNER CLASS:
QUESTION-3: Write a Java program to find the area of a rectangle.
import [Link].*;
class Rect
{
public static void main( )
{
int len, br, ar;
Scanner sc = new Scanner([Link]);
[Link]("Enter length");
len = [Link]();
[Link]("Enter breadth");
br = [Link]();
ar = len * br;
[Link]("Area of the rectangle = " + ar);
}
}
LIST OF VARIABLES:
[Link] Variable Name Data Type Description
1 len int to accept length
2 br int to accept breadth
3 ar int to calculate area
D) PROGRAM BASED ON MATHEMATICAL METHODS:
QUESTION–4: Write a Java program to find the area of a triangle for the given sides.
class TriangleArea
{
public static void main( )
{
int a = 10, b = 20, c = 30;
double ar, s;
s = (a + b + c) / 2;
ar = [Link](s * (s - a) * (s - b) * (s - c));
[Link]("AREA = " + ar);
}
}
LIST OF VARIABLES:
[Link] Variable Name Data Type Description
1 a int to assign value
2 b int to assign value
3 c int to assign value
4 s double to calculate semi perimeter
5 ar double to calculate area
E) PROGRAMS BASED ON IF, ELSE IF, IF ELSE IF LADDER, NESTED IF:
QUESTION–5: Write a Java program to find the largest or smaller number of the
given two.
class Large
{
public static void main( )
{
int a = 10, b = 20;
if (a > b)
[Link](a + " is big");
else
[Link](b + " is big");
}
}
LIST OF VARIABLES:
[Link] Variable Name Data Type Description
1 a int to assign value
2 b int to assign value
QUESTION–6: Write a program to check the given character is upper case or lower case.
class UpperLower
{
public static void main( )
{
char x = 'b';
if (x >= 65 && x <= 90)
[Link](x + " is in upper case");
else
[Link](x + " is in lower case");
}
}
LIST OF VARIABLES:
[Link] Variable Name Data Type Description
1 x char to assign a character value
QUESTION–7: Write a Java program to check whether the given character is in
upper case, lower case, digit, special character or space.
class Check
{
public static void main( )
{
char a = '$';
if (a >= 65 && a <= 90)
[Link](a + " is in upper case");
else if (a >= 97 && a <= 122)
[Link](a + " is in lower case");
else if (a >= 48 && a <= 57)
[Link](a + " is a digit");
else if (a == 32)
[Link](a + " is space");
else
[Link](a + " is a special character");
}
}
LIST OF VARIABLES:
[Link] Variable Name Data Type Description
1 a char to assign a character value
F) PROGRAMS BASED ON SWITCH CASE :
QUESTION–8: Write a Java program to print all the days of a week.
class DayOfWeek
{
public static void main( )
{
int dd = 5;
switch (dd)
{
case 1: [Link]("SUNDAY");
break;
case 2: [Link]("MONDAY");
break;
case 3: [Link]("TUESDAY");
break;
case 4: [Link]("WEDNESDAY");
break;
case 5: [Link]("THURSDAY");
break;
case 6: [Link]("FRIDAY");
break;
case 7: [Link]("SATURDAY");
break;
default: [Link]("Week days can never be more than 7");
}
}
}
LIST OF VARIABLES:
[Link] VARIABLE NAME DATA TYPE DESCRIPTION
1. dd int to assign a integer value
QUESTION-9: Write a Java program to check a character is vowel or consonant.
class VowelConsonant
{
public static void main()
{
char alp = 'E';
switch(alp)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': [Link]("It is a vowel");
break;
default: [Link]("It is a consonant");
}
}
}
LIST OF VARIABLES:
[Link] VARIABLE NAME DATA TYPE DESCRIPTION
1. alp char to assign a character value
QUESTION-10: Write a Java program to find sum, difference, product, quotient and
remainder.
class Calculator
{
public static void main()
{
int a = 100, b = 34;
char op = '+';
switch(op)
{
case '+': [Link]("SUM = " + (a + b));
break;
case '-': [Link]("DIFFERENCE = " + (a - b));
break;
case '*': [Link]("PRODUCT = " + (a * b));
break;
case '/': [Link]("QUOTIENT = " + (a / b));
break;
case '%': [Link]("REMAINDER=" + (a % b));
break;
default: [Link]("IT IS NOT AN ARITHME-TIC OPERATOR");
}
}
}
LIST OF VARIABLES:
[Link] | VARIABLE NAME | DATA TYPE | DESCRIPTION
1. | a | int | to assign an integer value
2. | b | int | to assign an integer value
3. | op | char | to assign an operator
G) PROGRAMS ON LOOPING STATEMENTS:
QUESTION–11: Write a Java program to print the sum of the following series
1 1+2 1+2+3
+ + + ⋯ + (𝑛 𝑡𝑒𝑟𝑚𝑠)
1 1∗2 1∗2∗3
class Series
{
public static void main()
{
int n=5, s, p;
double sum=0;
for(int i=1; i<=n; i++)
{
s = i;
p = 1;
for(int j=1; j<=i; j++)
{
s = s + j;
p = p * j;
}
sum = sum + s/p;
}
[Link]("SUM OF THE SERIES=" + sum);
}
}
LIST OF VARIABLES:
[Link] | VARIABLE NAME | DATA TYPE | DESCRIPTION
1. | n | int | to assign the no. of terms
2. | s | int | to calculate the sum
3. | p | int | to calculate the product
4. | sum | double | to find the total using two loops
QUESTION–12: Write a Java program to find LCM and HCF of given two numbers.
class LcmHcf
{
public static void main()
{
int a, b, x, i, hcf;
a = 10; b = 20;
if(a < b)
x = a;
else
x = b;
for(int i = 1; i <= x; i++)
{
if(a % i == 0 && b % i == 0)
hcf = i;
}
[Link]("HCF=" + hcf);
[Link]("LCM=" + a * b / hcf);
}
}
LIST OF VARIABLES:
[Link] | VARIABLE NAME | DATA TYPE | DESCRIPTION
1. | a | int | to assign integer value
2. | b | int | to assign integer value
3. | x | int | to find least value among a and b
4. | i | int | to run the loop
5. | hcf | int | to calculate hcf
QUESTION–13: Write a Java program to find whether the given number is palindrome
or not.
class Palindrome
{
public static void main()
{
int n, temp, s=0, rem;
n = 595;
temp = n;
while(n > 0)
{
rem = n % 10;
s = (s * 10) + rem;
n = n / 10;
}
if(temp == s)
[Link](temp + " is a palindrome no.");
else
[Link](temp + " is not a palindrome number");
}
}
LIST OF VARIABLES:
[Link] | VARIABLE NAME | DATA TYPE | DESCRIPTION
1. | n | int | to assign a number
2. | temp | int | to store temporary value of n
3. | sum | int | to find remainders
4. | s | int | to find reversed number
H) PROGRAM ON NESTED LOOPS :
QUESTION–14: Write a Java program to print the following pattern:
1
12
123
1234
12345
class Pattern
{
public static void main( )
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
[Link](j);
}
[Link]();
}
}
}
LIST OF VARIABLES:
[Link] | VARIABLE NAME | DATA TYPE | DESCRIPTION
1. | i | int | to run outer loop
2. | j | int | to run inner loop
I) MENU DRIVEN PROGRAM:
QUESTION-15: Write a menu driven program to check for two different numbers, Niven
number and Armstrong number.
import [Link].*;
class Menu
{
public static void main()
{
int n, ch;
Scanner sc = new Scanner([Link]);
[Link]("1. Niven Number");
[Link]("2. Armstrong Number");
[Link]("Enter your choice:");
ch = [Link]();
[Link]("Enter a number:");
n = [Link]();
switch(ch)
{
case 1:
int s = 0, t, r;
t = n;
while(n > 0)
{
r = n % 10;
s = s + r;
n = n / 10;
}
if(t % s == 0)
[Link]("It is a niven number");
else
[Link]("It is not a niven number");
break;
case 2:
int s = 0, t, r;
t = n;
while(n > 0)
{
r = n % 10;
s = s + r*r*r;
n = n / 10;
}
if(s == t)
[Link]("It is an armstrong number");
else
[Link]("It is not an armstrong number");
break;
default:
[Link]("You have entered wrong choice");
}
}
}
LIST OF VARIABLES:
[Link] VARIABLE NAME DATA TYPE DESCRIPTION
1. n int to accept an integer value
2. ch int to enter the choice
3. s int to find the sum
4. t int to store temporary value
5. r int to store the remainder