Computer Project
Computer Project
[2021-2022]
ADITI KUBER
[2021-2022]
9-A
ENRNO-4174
INDEX
Program 1(A): -.....................................................................................2
Program 1(B): -.....................................................................................3
Program 2: -..........................................................................................4
Program 3: -..........................................................................................5
Program 4: -..........................................................................................6
Program 5: -..........................................................................................8
Program 6: -..........................................................................................9
Program 7: -........................................................................................10
Program 8: -........................................................................................12
Program 9: -........................................................................................13
Program 10: -......................................................................................14
Program 11: -......................................................................................16
Program 12: -......................................................................................18
Program 13: -......................................................................................19
Program 14: -......................................................................................20
Program 15: -......................................................................................21
Program 16: -......................................................................................22
Program 17: -......................................................................................23
Program 18:-.......................................................................................24
Program 19: -......................................................................................26
Program 20: -......................................................................................28
Acknowledgement.............................................................................29
1
Program 1(A): -
Write the programs in Java to display the first ten terms of the following series:
A) 1, -3, 5, -7, 9, B) 1, 11, 111, 1111,
Program: -
import java.util.Scanner;
public class Pr1_a
{
public void main(String args[])
{
int term = 1;
System.out.print(term);
for (int i = 2; i <= 10; i++)
{
term += 2;
if (i % 2 == 0)
System.out.print(", " + (-term));
else
System.out.print(", " + term);
}
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
term Integer First Number is set and displayed as 1
i Integer Second Number to be incremented as per
requirement
Output: -
2
Program 1(B): -
Write the programs in Java to display the first ten terms of the following series:
B) 1, 11, 111, 1111,
Program: -
import java.util.Scanner;
public class Pr1_b
{
public void main(String args[])
{
int term = 1;
Variable description: -
Output: -
3
Program 2: -
2) Write the programs in Java to find the sum of the following series: S = 2 - 4 + 6 - 8 + ....... to n
Program: -
import java.util.Scanner;
public class Pr2
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int sum = 0;
for (int i = 1, j = 2; i <= n; i++, j = j + 2)
{
if (i % 2 == 0)
{
sum -= j;
}
else
{
sum += j;
}
}
System.out.println("Sum=" + sum);
}
}
Variable description: -
Output: -
4
Program 3: -
4) Write a program to enter two numbers and check whether they are co-prime or not. [Two numbers are
said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.
Program: -
import java.util.Scanner;
public class Pr4_coprime
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
int hcf = 1;
for (int i = 1; i <= a && i <= b; i++)
{
if (a % i == 0 && b % i == 0)
hcf = i;
}
if (hcf == 1)
System.out.println(a + " and " + b + " are co-prime");
else
System.out.println(a + " and " + b + " are not co-prime");
}
}
Variable description: -
Output: -
5
Program 4: -
6) Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output: 1
22
333
4444
55555
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output: 6 6 6 6 6 6
55555
4444
333
22
1
Program: -
import java.util.Scanner;
public class Pr6_Pattern
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
switch (ch)
{
case 1:
for (int i = 1; i <= n; i++)
6
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Variable description: -
Output: -
7
Program 5: -
7)Write a program to input a number. Display the product of the successors of even digits of the number
entered by user.
Input: 2745
Output: 15
[Hint: The even digits are: 2 and 4
The product of successor of even digits is: 3*5= 15]
Program: -
import java.util.Scanner;
public class Pr7_even_successor
{
public void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = in.nextInt();
int orgNum = num;
int prod = 1;
while (num != 0)
{
int digit = num % 10;
num /= 10;
if (digit % 2 == 0)
prod = prod * (digit + 1);
}
if (prod == 1)
System.out.println("No even digits in " + orgNum);
else
System.out.println("Product of even digits successors is "
+ prod);
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
I Integer Enter a number
8
num Integer To store the number
Orgnum Integer To store the number
Digit Integer To store the number
prod Integer To store the number
Output: -
Program 6: -
8)Write a program that inputs number of runs made by a cricket player on each ball. Entering runs as -1
should display the message that player is out. Finally it displays the number of runs made and balls played by
the player.
Program: -
import java.util.Scanner;
public class Pr8_Player_Score
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
int runs = 0, balls = 0, r = 0;
do {
System.out.print("Enter runs: ");
r = in.nextInt();
if (r == -1)
System.out.println("Player is out");
else
runs += r;
balls++;
}
while(r != -1);
System.out.println("Total Runs: " + runs);
System.out.println("Total Balls: " + balls);
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
Runs Integer Enter a number
Balls Integer To store the number
r Integer To store the number
9
Output: -
Program 7: -
9)Using a switch statement, write a menu driven program to:
(a) Generate and display the first 10 terms of the Fibonacci series
0, 1, 1, 2, 3, 5
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the
previous two.
(b) Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed.
Program: -
import java.util.Scanner;
public class Pr9_FibonacciNDigitSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Fibonacci Series");
System.out.println("2. Sum of digits");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch)
{
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
10
* term onwards.
*/
for (int i = 3; i <= 10; i++)
{
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
break;
case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0)
{
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " + sum);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
Variable description: -
Output: -
11
Program 8: -
10) Write a program to print the sum of the series: S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n
Program: -
import java.util.Scanner;
public class Pr10_
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
for (int i = 1; i <= n; i++)
{
double f = 1;
for (int j = 1; j <= i; j++)
{
f *= j;
}
sum += a / f;
}
System.out.println("Sum=" + sum);
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
A Integer Enter your choice
n Integer To store the number
Sum Double To store the number
f double To store the number
12
i integer To store the number
I integer To store the number
Output
Program 9: -
11)A tech number has even number of digits. If the number is split in two equal halves, then the square of
sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech
numbers.
Example: Consider the number 3025
Square of sum of the halves of 3025 = (30 + 25)2
= (55)2
= 3025 is a tech number.
Program: -
import java.util.Scanner;
public class Pr11_TechNumbers
{
public static void main(String args[])
{
for (int i = 1000; i <= 9999; i++)
{
int secondHalf = i % 100;
int firstHalf = i / 100;
int sum = firstHalf + secondHalf;
if (i == sum * sum)
System.out.println(i);
}
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
I Integer Enter your choice
firstHalf Integer To store the number
SecondHalf Integer To store the number
sum Integer To store the number
13
Output: -
Program 10: -
12) The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on
every book. The ISBN is based upon a 10-digit code.
The ISBN is legal if:
1 × digit1 + 2 × digit2 + 3 × digit3 + 4 × digit4 + 5 × digit5 + 6 × digit6 + 7 × digit7 + 8 × digit8 + 9 × digit9 + 10 ×
digit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9 + 10 × 9 = 253 which is divisible by 11.
Write a program to:
1. Input the ISBN code as a 10-digit integer.
2. If the ISBN is not a 10-digit integer, output the message "Illegal ISBN" and terminate the program. 3. If the
number is divisible by 11, output the message "Legal ISBN". If the sum is not divisible by 11, output the
message "Illegal ISBN".
Program: -
import java.util.Scanner;
public class Pr12_ISBNCheck
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the ISBN: ");
long isbn = in.nextLong();
int sum = 0, count = 0, m = 10;
while (isbn != 0)
{
int d = (int)(isbn % 10);
count++;
sum += d * m;
m--;
isbn /= 10;
}
if (count != 10)
{
System.out.println("Illegal ISBN");
14
}
else if (sum % 11 == 0)
{
System.out.println("Legal ISBN");
}
else
{
System.out.println("Illegal ISBN");
}
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
Isbn long Enter the number
Sum Integer To store the number
count Integer To store the number
M Integer To store the number
d Integer To store the number
Output: -
15
Program 11: -
13) Write a menu driven program to accept a number from the user and check whether it is a Prime number
or an Automorphic number.
(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and itself) Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is the number which is contained in the last digit(s) of its
square.)
Example: 25 is an Automorphic number as its square is 625 and 25 is present as the last two digits.
Program: -
import java.util.Scanner;
public class Pr13_PrimeAutomorphic
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();
switch (choice)
{
case 1:
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not Prime");
break;
16
case 2:
int numCopy = num;
int sq = num * num;
int d = 0;
/*
* Count the number of
* digits in num
*/
while(num > 0)
{
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Variable description: -
Output: -
17
Program 12: -
15) write a program to accept a number and check whether the number is palindrome or not.
Program: -
import java.util.Scanner;
public class Pr15_palindrome
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = in.nextInt();
int copyNum = num;
int revNum = 0;
while(copyNum != 0)
{
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
System.out.println("A Palindrome number");
else
System.out.println("Not a Palindrome number");
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
18
Num Integer To store the number
Copynum Integer To store the number
Revnum Integer To store the number
digit Integer To store the number
Output: -
Program 13: -
16) Write a program to input a number. Check and display whether it is a Niven number or not. (A number is
said to be Niven which is divisible by the sum of its digits).
Example: Sample Input 126 Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
Program: -
import java.util.Scanner;
public class Pr16_NivenNumber
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int orgNum = num;
int digitSum = 0;
while (num != 0)
{
int digit = num % 10;
num /= 10;
digitSum += digit;
}
/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number");
19
else
System.out.println(orgNum + " is not a Niven number");
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
Num Integer To store the number
orgnum Integer To store the number
Digitsum Integer To store the number
digit Integer To store the number
Output: -
Program 14: -
17) Write the programs in Java to find the sum of the following series: S = 1 + 1 + 2 + 3 + 5 + ....... to n terms.
Program: -
import java.util.Scanner;
public class Pr17_
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++)
{
int term = a + b;
sum += term;
a = b;
b = term;
}
System.out.println("Sum=" + sum);
}
}
Variable description: -
20
B Integer To store the number
sum Integer To store the number
term Integer To store the number
Output:
Program 15: -
20) Write a program to display all the numbers between 100 and 200 which don't contain zeros at any
position.
For example: 111, 112, 113, ....... , 199
Program: -
import java.util.Scanner;
public class Pr20_NoZero
{
public void main(String args[])
{
int count = 0;
for (int i = 100; i <= 200; i++)
{
boolean isNoZero = true;
int t = i;
while (t > 0)
{
if (t % 10 == 0)
{
isNoZero = false;
break;
}
t /= 10;
}
if (isNoZero)
{
System.out.print(i + " ");
count++;
}
if (count == 10)
{
System.out.println();
21
count = 0;
}}} }
Variable description: -
Variable name Data Type Description
Args[] String To store the number
Count Integer To store the number
I Integer To store the number
t Integer To store the number
Output: -
Program 16: -
21) Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
For example: 11, 101, 131, 151,
Program: -
import java.util.Scanner;
public class Pr21_PrimePalindrome
{
public void main(String args[ ])
{
int count = 0;
for (int i = 10; i <= 1000; i++)
{
int num = i, revNum = 0;
while (num != 0)
{
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == i)
{
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
22
if (isPrime)
{
System.out.print(i + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}}}}} }
Variable description: -
Variable name Data Type Description
Count Integer To store the number
Args[] String To store the number
I Integer To store the number
Num Integer To store the number
Digit Integer To store the number
Revnum Integer To store the number
j Integer To store the number
Output: -
Program 17: -
22) Write a program in java to display the given pattern
1
35
579
7 9 11 13
9 11 13 15 17
Program: -
import java.util.Scanner;
public class Pr22_Pattern
{
public static void main(String args[])
{
for (int i = 1, j = 1; i <= 5; i++, j += 2)
{
for (int k = j, l = 1; l <= i; l++, k += 2)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}
23
Variable description: -
Variable name Data Type Description
I Integer To store the number
Args[] String To store the number
J Integer To store the number
k Integer To store the number
Output: -
Program 18:-
23) A computerised bus charges fare from each of its passengers based on the distance travelled as per the
tariff given below:
As the passenger enters the bus, the computer prompts 'Enter distance you intend to travel'. On entering
the distance, it prints his ticket and the control goes back for the next passenger. At the end of journey, the
computer prints the following:
1. the number of passenger travelled
2. total fare received
Write a program to perform the above task.
[Hint: Perform the task based on user controlled loop].
Program: -
import java.util.Scanner;
public class Pr23_BusTravel
{
public void main(String args[])
{
Scanner in = new Scanner(System.in);
int dist, tf = 0, tp = 0;
System.out.println("Enter distance as -1 to complete the journey");
while (true) {
System.out.print("Enter distance you intend to travel: ");
dist = in.nextInt();
int f = 0;
if (dist == -1) {
break;
24
}
else if (dist <= 5) {
f = 80;
}
else if (dist <= 15) {
f = 80 + ((dist - 5) * 10);
}
else {
f = 80 + 100 + ((dist - 15) * 8);
}
tf += f;
tp++;
System.out.println("Your fare is " + f);
}
System.out.println("Total Passengers: " + tp);
System.out.println("Total Fare: " + tf);
}}
Variable description: -
Output: -
25
Program 19: -
24) You can multiply two numbers 'm' and 'n' by repeated addition method. For example, 5 * 3 = 15 can be
performed by adding 5 three times ⇒ 5 + 5 + 5 = 15
Similarly, successive subtraction of two numbers produces 'Quotient' and 'Remainder' when a number 'a' is
divided by 'b' (a>b).
For example, 5/2 ⇒ Quotient = 2 and Remainder = 1
Follow steps shown below:
Program: -
import java.util.Scanner;
public class Pr24_Arithmetic
{
public void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.println("1. Multiplication");
System.out.println("2. Division");
26
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int rMul = 0;
for (int i = 1; i <= b; i++) {
rMul += a;
}
System.out.println("Multiplication result = " + rMul);
break;
case 2:
int count = 0, t = a;
while (t > b) {
t -= b;
count++;
}
System.out.println("Divison Result");
System.out.println("Quotient = " + count);
System.out.println("Remainder = " + t);
break;
default:
System.out.println("Incorrect Choice");
break;
}}}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
A Integer To store the number
B Integer To store the number
Ch Integer To store the number
I Integer To store the number
Rmul Integer To store the number
count Integer To store the number
t Integer To store the number
Output: -
27
Program 20: -
25)Write a program in java to display the given pattern
97531
9753
975
97
9
Program: -
import java.util.Scanner;
public class Pr25
{
public void main(String args[])
{
int k, h=0, r;
for(k=5; k>=1; k--)
{
System.out.println(" ");
for(h=9,r=1; r<=k ;h=h-2,r++)
{
System.out.print(h+" ");
}
}
}
}
Variable description: -
Variable name Data Type Description
Args[] String To store the number
28
K Integer To store the number
H Integer To store the number
r Integer To store the number
Output: -
29
Acknowledgement
I would like to take this opportunity to express special
gratitude to my computer teacher Ms. Amitha who
gave me the golden opportunity to do this computer
science project. The opportunity to participate in this
project has helped me in improve my research skills
and I am really grateful to them.
I would also like to extend my gratitude to the
Principal sir for providing me with all the facilities that
were required.
30