Important Computer
Important Computer
CLASS – X-A
SUBJECT - COMPUTER
ROLL NO. 11
INTERNAL EXAMINER –
EXTERNAL EXAMINER -
1|Page
1.Write a program to input three angles of a triangle
and check whether a triangle is possible or not. If
possible then check whether it is an acute-angled
triangle, right-angled or an obtuse-angled triangle
otherwise, display 'Triangle not possible'.
import java.util.Scanner;
public class TriangleAngle1 {
public static void main() {
Scanner in = new Scanner(System.in);
System.out.println("Enter first angle: "); //INPUT
int a1 = in.nextInt();
System.out.println("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int sum = a1 + a2 + a3;
if (sum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {
if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) //
Check for right-angled triangle {
System.out.println("Right-angled Triangle");
}
else if (a1 > 90 || a2 > 90 || a3 > 90) // Check
for obtuse-angled triangle {
System.out.println("Obtuse-angled Triangle");
}
} else {
System.out.println("Triangle not possible");
}}}
2|Page
Variable Data Type Function
a1 int to input angle
a2 int to input angle
a3 int to input angle
sum int to store sum of angles
OUTPUT:
1.Enter first angle: 60
Enter second angle: 50
Enter third angle: 24
Triangle not possible
2. Enter first angle: 65
Enter second angle: 65
Enter third angle: 50
Acute-angled Triangle
3. Enter first angle: 65
Enter second angle: 65
Enter third angle: 50
Acute-angled Triangle
4. Enter first angle: 120
Enter second angle: 30
Enter third angle: 30
Obtuse-angled Triangle
3|Page
2. An Electricity Company charges their consumers
according to the units consumed per month According
to the given traffic:
Units Consumed Charges
Up to 100 units Rs. 2 per
unit
More than 100 units and
up to 200 units Rs. 1.80
per unit
More than 200 units Rs.1.50 per unit
In addition to the above, every consumer has to pay
Rs.200 as Service Charge per month. Write a program
to input the amount of units consumed and calculate
the total charges payable (Bill) by the consumer.
import java.util.*;
class ElectricityBill {
public static void main() {
int u;
double c = 0.0, tc = 0.0;
Scanner sc = new Scanner(System.in);
4|Page
else if (u > 200) c = 100 * 2 + 100 * 1.8 + (u - 200)
* 1.50; // Units above 200 at 1.50 per unit
OUTPUT:
1. Enter unit
33
Amount Payable = 266.0
2. Enter unit
22
Amount Payable = 244.0
5|Page
3. Write a program in java to input a number and
check whether it’s a perfect number or not.
A perfect number is a positive integer that is equal
to the sum of its proper divisors, excluding the
number itself. For example, 6 is a perfect number
because the sum of its divisors (1, 2, and 3) equals
6.
import java.util.*;
class PerfectNumber {
public static void main() {
Scanner sc = new Scanner(System.in); // Create a
scanner for input
System.out.print("Enter a number: "); // Prompt for
input
int num = sc.nextInt();
int sum = 0;
for (int i = 1; i <= num / 2; i++) { // Loop through
possible divisors
if (num % i == 0) { // Check if i is a divisor
sum += i; // Add divisor to the sum
}
}
if (sum == num)
System.out.println(num + " is a Perfect number.");
// Output if true
else
System.out.println(num + " is not a Perfect
number."); // Output if false
}
6|Page
}
OUTPUT:
1.
Enter a number: 6
6 is a Perfect number.
2.
Enter a number: 4
4 is not a Perfect number.
7|Page
4. Write a Java program to input a number and check
whether it is a Harshad number or not. A Harshad
number is an integer that is divisible by the sum of
its digits. For example, 18 is a Harshad number
because the sum of its digits is 1 + 8 = 9, and 18 is
divisible by 9.
import java.util.Scanner;
class HarshadNumber {
public static void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();
int r = number;
int sum = 0;
int digit;
while (number != 0) { // Loop to calculate the sum of
digits
digit = number % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
number /= 10; // Remove the last digit
}
if (r % sum == 0)
System.out.println(r + " is a Harshad number.");
else
System.out.println(r + " is not a Harshad
number.");
8|Page
}}
OUTPUT:
1. Enter a number: 44
44 is not a Harshad number.
2. Enter a number: 18
18 is a Harshad number.
9|Page
5.Write a program to sum of series
x^2/4 +x^2/5 … n terms
class aaa {
public static void main(int n,int x) {
int a=2; int b=4;double S=0;
for (int i=1;i<=n;i++) {
S=Math.pow(x,a)/b;
}
System.out.println(S);
}
OUTPUT:
1 n=44 x=3
2.25
10 | P a g e
6.Write a program in JAVA to print the Fibonacci
series upto ‘n’ terms.
class fibonacci {
public static void main(int n) {
int a=0; int b=1; int c=0;
System.out.println("the series upto " + n+"is :");
System.out.print(a + " ");
System.out.print(b + " ");
for (int i=1;i<=n-2;i++) //two terms have already been
printed
{
c=a+b;
System.out.print(c + " ") ;
a=b;
b=c; //moving ahead in the series
}
}
}
OUTPUT:
11 | P a g e
1. the series upto 10 is :
0 1 1 2 3 5 8 13 21 34
class Pattern {
public static void main() {
int c=1;
for (int i=1;i<=4;i++)
{
for (int j=1;j<=i;j++) {
System.out.print(c + " ");
c++;
}
System.out.println();
}
}
}
Variable Data Type Purpose
c int to be printed and
increase by one
i int for loop
j int for loop 2
12 | P a g e
8.Write a program to print the pattern.
A
A C
A C E
A C E G
class GTRT {
public static void main() {
char i,j;
for (i='A';i<='G';i+=2) {
for (j='A';j<=i;j+=2) {
System.out.print(j + “ “);
}
System.out.println();
}
}
}
13 | P a g e
9.Define a class Equation to print the result of the
following equation using the given functions: n!/(r!
*(n-r)!)
Int Fact(int num)- to return factorial
Void printsum(inr r,int n)- to print sum of given
series
class Equation {
14 | P a g e
obj2.Printsum(n,r);
}
}
OUTPUT:
1.n=10,r=4
210.0
15 | P a g e
10. Develop a class "Array" with the following
Specifications:
instant Variables- int a[10], k
Member functions-
public void input() To input integer members to
array A and a number separately to variable k.
public void search() To find and print 'Search
Successful' if number is found 'Search Unsuccessful'
otherwise.
import java.util.Scanner;
class func {
int a[]=new int[10];int k;
public void input() {
Scanner sc=new Scanner(System.in);
for(int i=0;i<10;i++) {
System.out.println("Enter value for index" +i+ "at
cell number"+ (i+1));
a[i]=sc.nextInt();
}
System.out.println("enter number");
k=sc.nextInt();
}
public void search() {
int flag=0;
for (int i=0;i<10;i++){
if (a[i]==k) {
16 | P a g e
System.out.println("search successful, found at
index"+i);
flag++;
System.exit(0);
}
}
if (flag==0)
System.out.println("search unsuccessful");
}
public static void main() {
OUTPUT:
1. Enter value for index0at cell number1
45
34
17 | P a g e
24
34
356
345
355
555
553
44
enter number
44
18 | P a g e
11.using 2 overloaded function by the name series
find and print the sum of the following series:
1.print sum of 1+1/2+1/3……1/n
2.print sum of 1+x+x/2^2+x/4^2……x/n^2
class arav {
void series(int n) { //first
int s=0;
for (int i=1;i<=n;i++){
s=s+1/i;
}
System.out.println(s);
}
void series(int n,int x){ //second
int s=0;
s=1+x;
for (int i=2;i<=n;i+=2){
s=(int)s+(x/(int)Math.pow(i,2));
}
System.out.println(s);
}
public static void main(int a,int b,int c) {
arav obj=new arav();
obj.series(a);
obj.series(b,c);
}
}
19 | P a g e
Variable Data Type Purpose
n(series 1) int limit
OUTPUT:
a=5
b=6
c=5
1
7
20 | P a g e
12. design a class to overload a function sum()as
1.int sum(int a,int b) to calculate and return the
sum of all the even numbers in the range a to b.
2.double sum(double n) to calculate and return the
product of the series:
Pro=1.0*1.2*……*n
class abc {
int sum(int a,int b) {
int c=0;
if (a%2==1)
c=a++;
int s=0;
for (int i=c;i<=b;i+=2){
s=s+i;
}
return s;
}
double sum(double n) {
double pro=1;
for (double i=1;i<=n;i+=0.2){
pro=pro*i;
}
return pro;
}
public static void main(int a,int b,double n){
abc obj=new abc();
System.out.println(obj.sum(a,b));
21 | P a g e
System.out.println(obj.sum(n));}}
OUTPUT:
a=4 b=56 c=5
812
2.710780430399311E8
22 | P a g e
13. Define a class SalaryCalculation described as
below: Data members:
name (String data type) basicPay, specialAlw,
conveyanceAlw, gross, pf, netSalary, annualSal
(double data types) Member methods:
a) SalaryCalculation(): A constructor to assign name
of employee (name), basic salary (basicPay) of your
choice and conveyance allowance (conveyanceAlw) as
Rs. 1000.00.
b) void salaryCal(): To calculate other allowances
and salaries as given below: specialAlw = 25% of
basic salary pf = 11% of basic salary gross =
basicPay + specialAlw + conveyanceAlw netSalary =
gross – pf annualSal = 12 months of net salary
c) void display(): To display the name and other
calculations with suitable headings. Write a main()
method to call the above member methods.
import java.util.*;
class SalaryCalculation {
String name;double
basicPay,specialAlw,conveyanceAlw,gross,pf ,netSalary,annualSa
l;
SalaryCalculation(){
Scanner sc=new Scanner(System.in);
System.out.println("name");
name=sc.next();
System.out.println("basic salary");
basicPay=sc.nextDouble();
conveyanceAlw=1000.00;
}
23 | P a g e
void salaryCal(){
specialAlw=25*basicPay/100;
pf=11*basicPay/100;
gross=basicPay+specialAlw+conveyanceAlw;
netSalary=gross-pf;
annualSal=12*netSalary;
}
void display(){
System.out.println("name " + name);
System.out.println("basic pay " + basicPay);
System.out.println("special allowance (25 per of basic
salary )is "+specialAlw);
System.out.println("gross salary" + gross );
System.out.println("net salary "+netSalary);
System.out.println("annual salary"+annualSal);
}
public static void main() {
SalaryCalculation obj=new SalaryCalculation();
obj.salaryCal();
obj.display();
}
}
Variable Name Data Type Description
name (SalaryCalculation) String Stores the name of the employee.
basicPay double Basic salary input by the user.
(SalaryCalculation)
specialAlw (salaryCal) double Special allowance calculated as 25%
of the basic salary.
conveyanceAlw double Fixed conveyance allowance
(SalaryCalculation) (1000.00).
24 | P a g e
gross (salaryCal) double Gross salary (basic pay + special
allowance + conveyance).
pf (salaryCal) double Provident Fund calculated as 11% of
the basic salary.
netSalary (salaryCal) double Net salary (gross salary - provident
fund).
annualSal (salaryCal) double Annual salary (12 times the net
salary).
sc (SalaryCalculation) Scanner Used to take input from the user.
OUTPUT:
name
ARAV
basic salary
200000
name ARAV
basic pay 200000.0
special allowance (25 per of basic salary )is 50000.0
gross salary251000.0
net salary 229000.0
annual salary2748000.0
25 | P a g e
14.Design a class Common
Data Members Arr[],Arr2[] //arraysSz1,Sz2 //size of
array
Member Methods
Void accept(int s1,int s2)//to initialize data
members and assign memory to arrays and accept value
in both the arrays(assume no duplicate values in a
array)
Void display() to display value of both arrays
Void arrange () to arrange the values of array in
ascending order
Void common() function to display only the common
values of both arrays in ascending order
import java.util.Scanner;
class Common {
int Arr[];
int Arr2[];
int sz1, sz2;
26 | P a g e
Scanner sc = new Scanner(System.in);
void display() {
System.out.println("First array:");
for (int i = 0; i < sz1; i++) {
System.out.print(Arr[i] + " ");
}
System.out.println();
System.out.println("Second array:");
for (int i = 0; i < sz2; i++) {
System.out.print(Arr2[i] + " ");
}
System.out.println();
}
void arrange() {
int temp;
27 | P a g e
for (int ph = 0; ph < sz1 - 1; ph++) {
for (int c = 0; c < sz1 - 1 - ph; c++) {
if (Arr[c] > Arr[c + 1]) {
temp = Arr[c];
Arr[c] = Arr[c + 1];
Arr[c + 1] = temp;
}
}
}
void common() {
System.out.print("Common elements: ");
boolean found = false;
for (int i = 0; i < sz1; i++) {
for (int j = 0; j < sz2; j++) {
if (Arr[i] == Arr2[j]) {
System.out.print(Arr[i] + " ");
found = true;
28 | P a g e
}
}
}
if (!found) {
System.out.print("None");
}
System.out.println();
}
obj.accept(a, b);
obj.arrange();
obj.display();
obj.common();
}
}
29 | P a g e
sz2 (Class) int Size of the second array (Arr2).
i (Method) int Loop index for iterating through the arrays
in methods.
ph (Method) int Loop index for passes in bubble sort in
arrange().
c (Method) int Loop index for comparisons in bubble sort.
temp (Method) int Temporary variable for swapping elements in
sorting.
j (Method) int Loop index for checking common elements
between arrays.
sc (Method) Scanner Scanner object for user input in the
accept() method.
OUTPUT:
Enter size for first array: 4
Enter size for second array: 4
Enter value for index 0 (cell no 1):
3
Enter value for index 1 (cell no 2):
3
Enter value for index 2 (cell no 3):
1
Enter value for index 3 (cell no 4):
5
Enter value for index 0 (cell no 1):
6
Enter value for index 1 (cell no 2):
7
Enter value for index 2 (cell no 3):
8
Enter value for index 3 (cell no 4):
30 | P a g e
9
First array:
1 3 3 5
Second array:
6 7 8 9
Common elements: None
class Arrayyy {
public static void main(String[] args) {
int[][] A = new int[3][3];
Scanner sc = new Scanner(System.in);
int S = 0, S1 = 0;
31 | P a g e
System.out.println("Sum of last column: " + S1);
}
}
OUTPUT:
Enter for row 0 column 0: 3
Enter for row 0 column 1: 2
Enter for row 0 column 2: 4
Enter for row 1 column 0: 5
Enter for row 1 column 1: 2
Enter for row 1 column 2: 5
Enter for row 2 column 0: 6
Enter for row 2 column 1: 77
Enter for row 2 column 2: 22
Sum of first row: 9
Sum of last column: 31
32 | P a g e
16.Write a program to input a sorted array(asc) and
search for a particular number as inputed by the
user.
import java.util.Scanner;
class ARARARRA {
public static void main(int A[]) {
int i;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=0;
int l=0;
int h=A.length-1;
while(l<=h){
m=(l+h)/2;
if (A[m]<n) {l=m+1;}
else if (A[m]>n){ h=m-1;}
else {
System.out.println(n + "found at "+ m);
break;
}
33 | P a g e
Variable Type Description
Name
n int Target integer to search for
m int Middle index of the current search
range
l int Lower bound of the search range
h int Upper bound of the search range
A int[ Array in which to search for the
] target integer
OUTPUT:
A[]={2,4,63,234,543,456,32}
4
4found at 1
34 | P a g e
17. write a program to input two array of size 5 by
int for marks and char code for the student and then
print a merit list after arranging in ascending
order.
import java.util.Scanner;
class MeritList {
public static void main(String[] args) {
int[] marks = new int[5];
char[] codes = new char[5];
Scanner sc = new Scanner(System.in);
35 | P a g e
codes[j] = codes[j + 1];
codes[j + 1] = tempCode;
}
}
}
System.out.println("Merit List:");
for (int i = 0; i < marks.length; i++) {
System.out.println("Character Code: " + codes[i] +
", Marks: " + marks[i]);}}}
Variable Name Type Description
OUTPUT:
Enter marks for student 1: 88
Merit List:
36 | P a g e
Character Code: C, Marks: 75
class Prog {
public static void main() {
String s = "ORANGE";
int l = s.length();
37 | P a g e
OUTPUT
O
OR
ORA
ORAN
ORANG
ORANGE
38 | P a g e
count int Stores the count of words that start
with 'A'.
i int Loop counter used to iterate through the
characters of the string.
OUTPUT:
Number of words starting with 'A': 3
39 | P a g e
Variabl Type Description
e
str Strin Stores the input string "FLY HIGH IN THE SKY"
g with an added space at the end
count int Counts the number of words that end with 'Y'
i int Used as the loop variable to iterate through
the string
OUTPUT:
Number of words ending with 'Y': 2
40 | P a g e