You are on page 1of 20

Computer

Project
2021-22
Name: Rishabh Sanghai
Grade: X - D
Roll No: xx

1
Rishabh Sanghai
ACKNOWLEDGEMENT

I would like to thank Ms. Sarala Mam for her valuable

guidance and help. She clarified all my queries about the

project without which I would not have been able to

complete the project. I would also like to thank my family

for providing all the necessary materials and my friends for

helping me with this project.

2
Rishabh Sanghai
INDEX
S.No. Programs Pg.No.
1. For Loop Pattern Program using switch-case 1-4
2. For Loop Series program 4-9
3. Number Programs (Special Number & Harshad 9 - 13
Number )
4. Function (FruitJuice – using Instance variable) 13 - 17
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

3
Rishabh Sanghai
Question 1:

Write a program to generate a pattern of numbers in the form of a


triangle or in the form of an inverted triangle depending upon User’s
choice. For an incorrect choice, an appropriate error message should
be displayed.

Sample Input: Enter your choice 1 Enter your choice 2

1 A A A A A

2 3 C C C C

4 5 6 E E E

7 8 9 10 G G

Program 1:

import java.util.Scanner;

public class program1

public static void main(String[] args)

Rishabh Sanghai 1
int ch;

Scanner sc = new Scanner(System.in);

System.out.println("SAMPLE INPUT: ");

System.out.print("Enter your choice: ");

ch = sc.nextInt();

System.out.println("SAMPLE OUTPUT: ");

switch(ch)

case 1:

for(int i = 1; i <= 5; i++)

for(int j = 1; j <= i; j++)

System.out.print(j + " ");

System.out.println();

break;

case 2:

char alph = 'A';

for(int i = 5; i >= 1; i--)

for(int j = 1; j <= i; j++)

Rishabh Sanghai 2
{

System.out.print(alph + " ");

alph += 2;

System.out.println();

break;

default:

System.out.println("Please enter a valid option");

Variable Description Table:

Variable Name Data type Purpose


ch int Storing input for pattern number
i int Iteration for loop
j int Iteration for loop
alph char Storing the alphabet for pattern 2

Output:

Rishabh Sanghai 3
Question 2:

Write a program to calculate and print the sum of each of the


following series within one class:

(i) S = a^1 / 2! - a^4/ 4! + a^7 / 6! - a^10 / 8! ………..upto n terms

(ii) S = 0 + 1 + 1 + 2 + 3 + 5 + 8 +……...upto n terms

Rishabh Sanghai 4
Program2:

import java.util.Scanner;

public class program2

void series_1()

int a, n, num;

int facto = 2;

Scanner sc = new Scanner(System.in);

System.out.println("SAMPLE INPUT:");

System.out.print("Enter a number: ");

a = sc.nextInt();

System.out.print("Enter number of times u want loop to run for: ");

n = sc.nextInt();

num = 1;

double sum = 0;

System.out.println("SAMPLE OUTPUT:");

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

int fact = factorial(facto);

Rishabh Sanghai 5
if(i % 2 == 0)

sum -= Math.pow(a, num) / fact;

else

sum += Math.pow(a, num) / fact;

num += 3;

facto += 2;

System.out.println("The Sum is: " + sum);

public int factorial(int input)

int fact = 1;

for(; input > 0; input--)

fact *= input;

return fact;

void fibonacciSeries()

Rishabh Sanghai 6
{

int num1, num2, sum, num3, n;

num1 = 0;

num2 = 1;

sum = 0;

num3 = 0;

n = 0;

Scanner sc = new Scanner(System.in);

System.out.println("SAMPLE INPUT:");

System.out.print("Enter fibonacci sequence length: ");

n = sc.nextInt();

System.out.println("SAMPLE OUTPUT:");

for(; n > 0; n--)

sum += num1;

num3 = num1 + num2;

num1 = num2;

num2 = num3;

System.out.println("The sum of the fibonacci sequence is: " + sum);

public static void main(String[] args)

Rishabh Sanghai 7
program2 obj = new program2();

obj.series_1();

obj.fibonacciSeries();

Variable Description Table:

Variable Name Data type Purpose


a int Stores number for pattern
n int Stores number of times loop runs
num int Storing value of power for a
facto int Storing factorial number for pattern
sum double Sum of pattern
i int Iteration for loop
fact int Storing value of factorial
input int Storing input for function
num1 int Used for calculating Fibonacci
num2 int Used for calculating Fibonacci
Sum int Sum of sequence
num3 int Used for calculating Fibonacci
n1 int Stores sequence length

Output:

Rishabh Sanghai 8
Question 3:

Write a menu driven program to accept a number from the user and
check whether it is a Special number or a Harshad number.

a) A number is said to be a special number , if the sum of the factorial


of each digits of the number is same as the original number.

Eg.1! + 4! + 5! = 1 + 24 + 120 = 145

b) A harshad number is an integer that is divisible by the sum of its


digits.

Rishabh Sanghai 9
Eg.171

Sum of digits = 1 + 7 + 1 =9

171 is divisible by 9

Program3:

import java.util.Scanner;
public class program3
{
void specialNumber()
{
int n, num, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("SAMPLE INPUT");
System.out.print("Enter a number: ");
num = sc.nextInt();
n = num;
System.out.println("SAMPLE OUTPUT");
while (num > 0)
{
digit = num % 10;
int fact = 1;
for(int i = 1; i <= digit; i++)
{
fact=fact * i;
}
sum = sum + fact;

Rishabh Sanghai 10
num = num / 10;
}
if(n == sum)
{
System.out.println(n+" is a special number.");
}
else
{
System.out.println(n+" is not a special number.");
}
}
void harshadNumber()
{
int n,num,digit,sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("SAMPLE INPUT");
System.out.print("Enter a number: ");
n = sc.nextInt();
num = n;
System.out.println("SAMPLE OUTPUT");
while(n > 0)
{
digit = n % 10;
sum = sum + digit;
n = n/10;
}
if(num % sum == 0)
{
System.out.println(num+ " is a harshad number");

Rishabh Sanghai 11
}
else
{
System.out.println(num+ " is not a harshad number");
}
}
public static void main(String[]args)
{
program3 obj1 = new program3();
obj1.specialNumber();
obj1.harshadNumber();
}
}

Variable Description Table:

Variable Name Data type Purpose


n int Storing value of num
num int Storing input number from user
digit int Storing digit while calculating
sum int Holding sum for pattern
fact int Getting Factorial and storing it
i int Iteration for loop

Output:

Rishabh Sanghai 12
Question 4:

Define a class named FruitJuice with the following description:

Instance variables/data members:

int productCode: stores the product code number.

String flavour: stores the flavour of the juice (e.g. orange, apple, etc.).

Rishabh Sanghai 13
String packType: stores the type of packaging (e.g. tetra-pack, PET
bottle, etc.).

int packSize: stores package size (e.g. 200, 400, etc.).

int productPrice: stores the price of the product.

Member functions:

(i) void input(): to input and store the productCode, flavour, packType,
packSize and productPrice.

(ii) void discount(): to reduce the product price by 10%.

(iii) void display(): to display the productCode, flavour, packType,


packSize and productPrice.

Write a main method to create an object of the class and call the
above member methods.

Program4:

import java.util.Scanner;
public class FruitJuice
{
int productCode,packSize,productPrice;
String flavour,packType;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("SAMPLE INPUT");
System.out.print("Enter the product code: ");
productCode = sc.nextInt();

Rishabh Sanghai 14
System.out.print("Enter the flavour: ");
flavour = sc.next();
System.out.print("Enter the packType: ");
packType = sc.next();
System.out.print("Enter the packSize: ");
packSize = sc.nextInt();
System.out.print("Enter the product price: ");
productPrice = sc.nextInt();
System.out.println("SAMPLE OUTPUT");
}
void discount()
{
productPrice = productPrice - productPrice * 10/100;
}
void display()
{

System.out.println("The product Code is: "+productCode);


System.out.println("The flavour is: "+flavour);
System.out.println("The pack size is: "+packSize);
System.out.println("The pack type is: "+packType);
System.out.println("The product price is: "+productPrice);
}
public static void main(String[] args)
{
FruitJuice obj1 = new FruitJuice();
obj1.input();
obj1.discount();
obj1.display();

Rishabh Sanghai 15
}
}

Variable Description Table:

Variable Name Data type Purpose


productCode int Storing Product Code
packSize int Storing Pack Size
productPrice int Storing Product Price
flavour String Storing Product Flavour
packType String Storing Product Pack Type

Output:

Rishabh Sanghai 16
Rishabh Sanghai 17

You might also like