Computers
Computers
SCIENCE PROJECT
CLASS: 11
SECTION: S2
1
INDEX
Sl
Pag
.
Programs e
No
No.
.
1 Write a program to form a new date by 3
adding certain number of days to an
original date.
2
9 Write a program to fill a 2D array with 27
numbers in the Fibonacci series.
PROGRAM I
Write a program to form a new date by
adding certain number of days to an
original date.
Code:
// Importing
import java.util.*;
// Initialising the class name
class Program1
{
static void main()
{
// Taking the original date as input
Scanner sc = new Scanner(System.in);
System.out.print("Enter the date: ");
int d = sc.nextInt();
System.out.print("Enter the month: ");
int m = sc.nextInt();
System.out.print("Enter the year: ");
int y = sc.nextInt();
System.out.print("Enter the number of
days: ");
int days = sc.nextInt();
int arr[] =
{31,28,31,30,31,30,31,31,30,31,30,31};
// Checking if it is a leap year
if(y%4 == 0 && y% 100 != 0 || y % 400 ==
0)
arr[1] = 29;
3
// Add the days to the original date
while (true)
{
if (days>(arr[m-1]-d))
{
days-=arr[m-1]-d;
d = 0;
if (m==12)
{
m = 0;
y++;
if(y%4 == 0 && y% 100 != 0 || y %
400 == 0)
arr[1] = 29;
else
arr[1] = 28;
}
m++;
}
else
{
d += days;
break;
}
}
// Printing the new date
System.out.println("dd/mm/yyyy =
"+d+"/"+m+"/"+y);
}
}
Output:
Set 1:
4
Set 2:
5
6
PROGRAM 2
Write a program to input a number in
decimal system and convert to a number
system as per user’s choice.
Code:
// Importing
import java.util.*;
// Initialising the class name
class Program2
{
void main()
{
// Taking number and choice as input
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = sc.nextInt();
System.out.print("Enter the number
system: ");
String str = sc.next();
int a = 0;
if (str.equals("Binary"))
a = 2;
if (str.equals("Octal"))
a = 8;
if (str.equals("Hexadecimal"))
a = 16;
String str1 = "";
int b = 0;
// Converting number into the desired
number system
while (num>0){
b = num%a;
if (b>9)
str1 = (char)(55+b) + str1;
else
7
str1 = b + str1;
num/=a;
}
// Printint new number
System.out.println(str1);
}
}
Output:
Set 1:
Set 2:
8
b int Stores each digit of the number
input by the user
9
PROGRAM 3
Write a program to input two binary
numbers and perform a specific operation
as per user’s choice.
Code:
// Importing
import java.util.*;
// Initialising the class name
class Program3{
void main(){
// Taking the two numbers and operation
to be performed as input
Scanner sc = new Scanner(System.in);
System.out.println("Enter the two numbers
and the operation to be performed: ");
int n1 = sc.nextInt();
int n2 = sc.nextInt();
String str = sc.next();
String ans = "";
// Addition
if(str.equals("+")){
int carry = 0;
while (n1>0 || n2>0){
if (n1%10 + n2%10 + carry == 2){
carry = 1;
ans = "0" + ans;
}
else if (n1%10 + n2%10 + carry ==
3){
carry = 1;
ans = "1" + ans;
}
else
{
10
ans = String.valueOf(n1%10 +
n2%10+carry)+ans;
carry = 0;
}
n1/=10;
n2/=10;
}
if (carry == 1)
ans = "1"+ans;
}
// Subtraction
else if(str.equals("-")){
String sign = "";
if(n2 > n1)
{
int temp = n1;
n1 = n2;
n2 = temp;
sign = "-";
}
while (n1>0){
if(n1%10 == 1 && n2%10 == 1)
ans = "0"+ans;
if(n1%10 == 0 && n2%10 == 0)
ans = "0"+ans;
if(n1%10 == 1 && n2%10 == 0)
ans = "1"+ans;
if(n1%10 == 0 && n2%10 == 1){
ans = "1"+ans;
int k = -1;
while (n1>0){
n1/=10;
k++;
if(n1%10 == 1){
n1-=1;
while (k>0){
n1 = n1*10 + 1;
k--;
}
11
break;
}
}
}
else
n1/=10;
n2/=10;
}
ans = sign+ans;
}
// Multiplication
else if(str.equals("x")){
int carry = 0;
int k = -1;
int arr[] = new
int[String.valueOf(n2).length()];
int h = 0;
while (n2>0){
k++;
if(n2%10 == 1)
{
arr[k] =
n1*(int)Math.pow(10,k);
if(String.valueOf(n1*(int)Math.pow(10,k)).length(
)>h)
h =
String.valueOf(n1*(int)Math.pow(10,k)).length();
}
n2/=10;
}
int sum = 0;
while (h>=0){
for (int i = 0; i < arr.length;i+
+){
if(arr[i]>0){
sum += arr[i]%10;
arr[i]/=10;}
}
12
if(sum%2==0)
ans = "0" + ans;
else
ans = "1" + ans;
sum/=2;
h--;
}
}
// Printing the answer
System.out.println("Answer is: "+ans);
}
}
Output:
Set 1:
Set 2:
Set 3:
13
VARIABLE DESCRIPTION TABLE
14
PROGRAM 4
Write a program to make a 2 player Tic
Tac Toe game using 2D Array.
Code:
// Importing
import java.util.*;
// Initialising the class name
class Program4{
void main(){
System.out.println(" 1 | 2 | 3 ");
System.out.println("-----------");
System.out.println(" 4 | 5 | 6 ");
System.out.println("-----------");
System.out.println(" 7 | 8 | 9 ");
System.out.println("");
int arr[][] = new int[3][3];
Scanner sc = new Scanner(System.in);
int won = 0, a = 0, turn = 0, row = 0,
column = 0;
String name = "";
for(int i = 0; i<9; i++){
if(i%2==0){
name = "Player 1";
a = 1;
}
else{
name = "Player 2";
a = 2;
}
// Taking the player's input
System.out.print(name+ "'s turn: ");
turn = sc.nextInt();
row = (turn-1)/3;
column = (turn-1)%3;
15
// Checking of the spot is already
taken
if (arr[row][column]!=0){
System.out.println("Invalid
choice");
i--;
continue;
}
// Adiing the player's choice to the
board
arr[row][column] = a;
// Printing the new board
System.out.println("");
System.out.println(" "+arr[0][0]+" |
"+arr[0][1]+" | "+arr[0][2]+" ");
System.out.println("-----------");
System.out.println(" "+arr[1][0]+" |
"+arr[1][1]+" | "+arr[1][2]+" ");
System.out.println("-----------");
System.out.println(" "+arr[2][0]+" |
"+arr[2][1]+" | "+arr[2][2]+" ");
System.out.println("");
// Checking if someone won the game
for (int k = 0; k<3; k++){
if(arr[k][0] == a && arr[k][1] ==
a && arr[k][2] == a)
won = 1;
if(arr[0][k] == a && arr[1][k] ==
a && arr[2][k] == a)
won = 1;
}
if (arr[0][0] == a && arr[1][1] == a
&& arr[2][2] == a)
won = 1;
if(arr[0][2] == a && arr[1][1] == a
&& arr[2][0] == a)
won = 1;
if(won == 1){
won = 1;
16
System.out.println(name + "
won");
break;
}
}
// Checking if the game was a draw
if (won == 0)
System.out.println("It was a draw");
}
}
Output:
Set 1: Set 2:
17
VARIABLE DESCRIPTION TABLE
PROGRAM 5
Write a program to input a name and print
only the initials.
Code:
19
// Importing
import java.util.*;
// Initialising the class name
class Program5
{
void main()
{
// Taking the word as input
Scanner sc=new Scanner(System.in);
System.out.print("Enter the name : ");
String name = sc.nextLine();
name = name.toUpperCase();
System.out.print(name.charAt(i+1)+". ");
}
}
}
Output:
Set 1:
20
Set 2:
21
PROGRAM 6
Write a program to rotate a given square
matrix by 90 degrees in clockwise
direction. (Using 1 array only)
Code:
// Importing
import java.util.Scanner;
class Program6 {
void main() {
System.out.println("Original Matrix:");
22
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "
");
}
System.out.println();
}
System.out.println();
System.out.println("Rotated Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "
");
}
System.out.println();
23
}
}
}
Output:
Set 1: Set 2:
24
n int Used to store number of rows /
columns of the matrix
temp int Used as temporary variable to
store certain values
25
PROGRAM 7
Write a program to take input a square
matrix and switch its diagonals. (Using 1
array only)
Code:
// Importing
import java.util.Scanner;
// Initialising the class name and main method
class Program7 {
void main() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n:
");
int n = sc.nextInt();
System.out.println();
int arr[][] = new int[n][n];
System.out.println("Enter the elements of
the matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println();
// Printing the Original Matrix
System.out.println("Original Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "
");
}
System.out.println();
}
System.out.println();
// Switching the diagonals
26
for (int i = 0; i <n; i++)
{
int a = n-i-1;
int temp = arr[i][i];
arr[i][i] = arr[i][a];
arr[i][a] = temp;
}
// Printing the New Matrix
System.out.println("New Matrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "
");
}
System.out.println();
}
}
}
Output:
Set 1: Set 2:
27
VARIABLE DESCRIPTION TABLE
28
PROGRAM 8
Write a program to enter a string and
sort the characters based on their ASCII
values.
Code:
// Importing
import java.util.Scanner;
// Initialising the class name and main method
class Program8 {
static void main() {
// Taking the string as input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String ans = "";
// Converting string to character array
char arr[] = new char[str.length()];
for (int i = 0; i < str.length(); i++)
arr[i] = str.charAt(i);
// Using Bubble Sort to sort characters
of the string based on ASCII values
for (int i = 0; i < arr.length - 1; i++)
{
for (int j = 0; j < arr.length - i -
1; j++) {
if (arr[j] > arr[j + 1]) {
char temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Printing the sorted string
for (int i = 0; i < arr.length; i++)
ans+=arr[i];
29
System.out.println("Sorted string: " +
ans);
}
}
Output:
Set 1:
Set 2:
30
PROGRAM 9
Write a program to fill a 2D array with
numbers in the Fibonacci series.
Code:
// Importing
import java.util.Scanner;
// Initialising the class name and main method
class Program9 {
void main() {
//Taking input the size of the array
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n:
");
int n = sc.nextInt();
int arr[][] = new int[n][n];
int first = 0, second = 1,sum = 0;
// Assigning Fibonacci numbers to each
element
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = first;
sum = first + second;
first = second;
second = sum;
}
}
// Printing the array
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\
t");
31
}
System.out.println();
}
}
}
Output:
Set 1:
Set 2:
32
i int Stores the counter for the for
loop
j int Stores the counter for the for
loop
arr[][] int Double Dimensional Integer array
to store the Fibonacci numbers
first int Stores the 1st number / the
number to be assigned
second int Stores the number to be added to
the first number
sum int Stores the sum of the first and
second number
33
PROGRAM 10
Write a program to encrypt a sentence
using Cipher Encryption.
Code:
import java.util.*;
class Program10 {
String str;
int n;
// Taking input from user
void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter text: ");
str = sc.nextLine();
System.out.println("Enter shift: ");
n = sc.nextInt();
}
// Returning encrypted string
String shifted(){
String str1 = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Checking for letter and shifting
accordingly
if (ch >= 'a' && ch <= 'z') {
ch += n;
if (ch > 'z')
ch = (char) ('a' + ((int) ch
- (int) 'z') -1); }
else if (ch >= 'A' && ch <= 'Z') { ch
+= n;
if (ch > 'z')
ch = (char) ('A' + ((int) ch
- (int) 'Z') - 1);
}
34
str1 = str1 + ch;
}
return str1;
}
// Printing encrypted string
void display(){
System.out.println("Encrypted Word: " +
shifted());
}
void main() {
cipher obj = new cipher(); obj.input();
obj.display();
}
}
Output:
Set 1:
Set 2:
35
VARIABLE DESCRIPTION TABLE
36