0% found this document useful (0 votes)
2 views

Computers

This document is a computer science project by Prakarsh Jhajharia, a class 11 student, detailing various programming tasks. It includes programs for date manipulation, number system conversion, binary operations, a Tic Tac Toe game, and string manipulation. Each program is accompanied by code, output examples, and variable descriptions.

Uploaded by

prakujhajhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computers

This document is a computer science project by Prakarsh Jhajharia, a class 11 student, detailing various programming tasks. It includes programs for date manipulation, number system conversion, binary operations, a Tic Tac Toe game, and string manipulation. Each program is accompanied by code, output examples, and variable descriptions.

Uploaded by

prakujhajhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

COMPUTER

SCIENCE PROJECT

NAME: PRAKARSH JHAJHARIA

CLASS: 11

SECTION: S2

ROLL NUMBER: B/96/10

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 Write a program to input a number in 6


decimal system and convert to a number
system as per user’s choice.

3 Write a program to input two binary numbers 8


and perform an operation as per the user’s
choice.

4 Write a program to make a 2 player Tic Tac 13


Toe game using 2D Array.

5 Write a program to input a name and print 17


only the initials.

6 Write a program to rotate a given square 19


matrix by 90 degrees in clockwise
direction. (Using 1 array only)

7 Write a program to take input a square 22


matrix and switch its diagonals. (Using 1
array only)

8 Write a program to enter a string and sort 25


the characters based on their ASCII values.

2
9 Write a program to fill a 2D array with 27
numbers in the Fibonacci series.

10 Write a program to encrypt a sentence using 30


Cipher Encryption.

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:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
d int Stores the date input by the
user
m int Stores the month input by the
user
y int Stores the year input by the
user
days int Stores the number to be added
to the original date
arr[] int Stores the number of days in
each month of the calendar year

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:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
num int Stores the number input by the
user
str String Stores the number system to be
converted to
str1 String Stores the number in the desired
number system
a int Stores the base of the number
system

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

Variabl Data Purpose


e Name Type
n1 int Stores the 1st number input by
the user
n2 int Stores the 2nd number input by
the user
str String Stores the operation to be
performed on the numbers
ans String Stores the answer of the
operation
carry int Stores the carry over value for
addition and multiplication
temp int Temporary variable to
interchange values
sign String Stores the sign for subtraction

k int Used as a counter for the loop

i int Used as a counter for the loop

arr[] int Stores the numbers to be added


in multiplication
h int Stores the length of the
numbers
sum int Stores sum of carry over

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

Variabl Data Purpose


e Name Type
arr[] int Double Dimensional Integer array
to store the moves of both
18
players
name String Stores the player whose turn it
is
won int Checks if a player has won the
game
a int Stores the player number to be
input on the board
turn int Stores the turn played by the
user
row int Stores the row of the player’s
turn
column int Stores the column of the
player’s turn
i int Stores the counter for the for
loop
k int Stores the counter for the for
loop

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();

// Initializing the other variables


int i = 0;

// Finding and printing the initials


System.out.print(name.charAt(0)+". ");
for (i = 0; i < name.length()-1; i++)
{
if (name.charAt(i) == ' ')

System.out.print(name.charAt(i+1)+". ");
}
}
}

Output:

Set 1:

20
Set 2:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
name String Stores the name input by the
user
i int Stores the counter for the for
loop

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;

// Initialising the class name and main method

class Program6 {
void main() {

// Taking the elements of the matrix as


input

Scanner sc = new Scanner(System.in);


System.out.print("Enter the value of n
(number of rows / columns): ");
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:");
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();

// Rotating the Matrix by 90 degrees

for (int i = 0; i <n; i++)


{
for(int k = i;k<n;k++)
{
int temp = arr[i][k];
arr[i][k] = arr[k][i];
arr[k][i] = temp;
}
}
for (int i = 0; i <n; i++)
{
for(int k = 0;k<n/2;k++)
{
int temp = arr[i][k];
arr[i][k] = arr[i][n-k-1];
arr[i][n-k-1] = temp;
}
}

// Printing the Rotate Matrix

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:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
arr[][] int Double Dimensional Integer array
to store the elements of the
matrix
i int Stores the counter for the for
loop
k int Stores the counter for the for
loop
j int Stores the counter for the for
loop

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

Variabl Data Purpose


e Name Type
arr[][] int Double Dimensional Integer array
to store the elements of the
matrix
i int Stores the counter for the for
loop
j int Stores the counter for the for
loop
n int Used to store number of rows /
columns of the matrix
temp int Used as temporary variable to
store certain values

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:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
arr[] char Character array to store each
character of the inputted string
str String Stores the string input by the
user
ans String Stores the sorted string

i int Stores the counter for the for


loop
j int Stores the counter for the for
loop
temp int Used as temporary variable to
switch values

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:

VARIABLE DESCRIPTION TABLE

Variabl Data Purpose


e Name Type
n int Stores the number of rows /
columns enter by the user

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

Variabl Data Purpose


e Name Type
str String Stores the string input by user

str1 String Stores the new encrypted string

i int Stores the counter for the for


loop
n int Stores the value of the shift

ch char Stores each character of the


string input

36

You might also like