Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Aditi.K - 9A - Computer Project

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Computer Science Project

[2021-2022]

ADITI KUBER
9-A
ENRNO-4174
INDEX
Program 1 ........................................................................................... 2
Program 2 ........................................................................................... 3
Program 3 ........................................................................................... 4
Program 4 ........................................................................................... 5
Program 5 ........................................................................................... 6
Program 6 ........................................................................................... 8
Program 7 ......................................................................................... 10
Program 8 ......................................................................................... 11
Program 9 ......................................................................................... 12
Program 10 ....................................................................................... 13
Program 11 ....................................................................................... 14
Program 12 ....................................................................................... 15
Program 13 ....................................................................................... 16
Program 14 ....................................................................................... 17
Program 15 ....................................................................................... 19
Acknowledgement ............................................................................ 20
Program 1
Write a program to enter two numbers. If the first number is greater than display square of the
smaller number and cube of the greater number otherwise vice versa. If the numbers are equal,
display the message “both numbers are equal.”

Program: -
import java.util.Scanner;
public class Pr1_2nos
{
public static void main()
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
if(a>b)
{ System.out.println("Cube = "+Math.pow(a,3));
System.out.println("Square = "+Math.pow(b,2)); }
else if(b>a)
{ System.out.println("Cube = "+Math.pow(b,3));
System.out.println("Square = "+Math.pow(a,2)); }
else
System.out.println("Both number are equal");
}
}

Variable description: -
Variable name Data Type Description
a Integer First Number to be entered by the user
b Integer Second Number to be entered by the user

Output: -
Program 2
Write a program to enter three numbers and A character. Find and print sum of the numbers If a
given character is ‘s’ and product of the numbers If a given character is ‘p’. The program displays a
message “invalid character” if the user enters another alphabet.

Program: -
import java.util.Scanner;
public class Pr2_3nos_SP
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter three number:");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
System.out.println("Enter a char (s=sum, p=product):");
char t=sc.next() .charAt(0);
if((t=='s')||(t=='S'))
System.out.println("Sum = "+(a+b+c));
else if((t=='p')||(t=='P'))
System.out.println("Product = "+(a*b*c));
else
System.out.println("Wrong Choice");
}
}

Variable description: -
Variable name Data Type Description
a Integer 1st Number entered by the user
b Integer 2nd Number entered by the user
c Integer 3rd Number entered by the user
t char Character to be entered either “s” or “p”

Output: -
Program 3
Write a program to input three unequal numbers and display the second smallest number.

Program: -
import java.util.Scanner;
public class Pr3_3nos
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter three unequal numbers");
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if ((a==b)||(b==c)||(c==a))
System.out.println("U were asked to enter 3 unequal no's !!!");
else if((b<a)&&(c>a))
System.out.println("2nd smallest number is "+a);
else if((b>a)&&(b<c))
System.out.println("2nd smallest number is "+b);
else if((c>a)&&(c<b))
System.out.println("2nd smallest number is "+c);
else if((a>c)&&(a<b))
System.out.println("2nd smallest number "+a);
}
}

Variable description: -
Variable name Data Type Description
st
a Integer 1 Number entered by the user
b Integer 2nd Number entered by the user
c integer 3rd Number entered by the user

Output: -
Program 4
Write a program to enter three sides of a triangle and check whether the triangle is possible or not.
If possible then display whether it is equilateral, an isosceles or scalene triangle otherwise display
the message “triangle is not possible”.

Program: -
import java.util.Scanner;
public class Pr4_Triangle
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter the 3 sides of the triangle.");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
if(((a+b)>c)&&((b+c)>a)&&((c+a)>b))
{
if((a==b)&(b==c))
System.out.println("Entered Triangle is equilateral.");
else if((a!=b)&&(b!=c)&&(c!=a))
System.out.println("Entered Triangle is Scalene.");
else
System.out.println("Entered Triangle is Isosceles.");
}
else
System.out.println("Triangle Not Possible.");
}}

Variable description: -
Variable name Data Type Description
st
a double 1 side of triangle
b double 2nd side of triangle
c double 3rd side of triangle

Output: -
Program 5
Upto 20000 Ac—5% TV 2.5%
20001-40000 AC – 7.5% TV 5%
40001-60000 AC-10% TV-7%
More than 60000 AC 12% TV 8.5%
Write a program to input the amount of purchase. The customer has to pay 12.5% as sales tax on
price after the discount. Print net amount. Enter ‘1’ for AC and ‘2’ for TV.

Program: -
import java.util.Scanner;
public class Pr5_TV_AC
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Item purchased(AC-1 and TV-2):-");
int ch = in.nextInt();
System.out.println("Please enter the amount purchased for...");
double cp = in.nextDouble();
double disc = 0, sp = 0, amt=0, tx=0;
switch (ch)
{
case 1 :
if (cp>60000) disc = (12.0/100.0)*cp;
else if (cp>4000) disc = (10.0/100.0)*cp;
else if (cp>2000) disc = (7.5/100)*cp;
else disc = (5.0/100)*cp;
sp = (cp-disc);
tx = sp*(12.5/100);
amt= sp+tx;
System.out.println("Disc = "+disc+" Sp before= "+sp);
System.out.println("SalesTax ="+tx+" SP after= "+amt);
break;
case 2:
if (cp>60000) disc = (8.5/100.0)*cp;
else if (cp>40000) disc = (7.0/100.0)*cp;
else if (cp>2000) disc =(5.0/100.0)*cp;
else disc = (2.5/100)*cp;
sp = (cp-disc);
tx = sp*(12.5/100);
amt = sp+tx;
System.out.println("Disc = "+disc+" SP before= "+sp);
System.out.println("SalesTax = "+tx+" SP after= "+amt);
break;
default:
System.out.println("Invalid Input");
}
}
}
Variable description: -
Variable name Data Type Description
ch Integer Choice 1 for AC and 2 for TV
cp Double Purchasing amount to be entered
Disc Double To display discount
Sp Double To display salesprice
Amt Double To display the final price ( Sales tax+ Salesprice)
tx Double To display sales tax

Output: -
Program 6
Write a program to accept length and breadth of a rectangle. Calculate and display the area,
perimeter or diagonal of the rectangle as per the user’s choice.

Program: -
import java.util.Scanner;
public class Pr6_Rectangle
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double l, b;
int ch;
System.out.print("Enter the length of the rectangle: ");
l=sc.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
b=sc.nextDouble();
System.out.println("1. Calculate Area.");
System.out.println("2. Calculate Perimeter.");
System.out.println("3. Calculate Diagonal.");
System.out.print("Enter your choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Area of the rectangle is: "+l*b);
break;
case 2:
double p=2*(l+b);
System.out.println("Perimeter of the rectangle is: "+p);
break;
case 3:
double d=Math.sqrt(l*l+b*b);
System.out.println("Diagonal of the rectangle is: "+d);
break;
default: System.out.println("Invalid Choice");
}
}
}
Variable description: -

Variable name Data Type Description


l double Enter length of rectangle
b double Enter breadth of rectangle

Output: -
Program 7
Write a program that displays the result of the following evaluations based on the number entered
by the user.
a) absolute value of a number
b) square root of a number
c) cube root of a number

Program: -
import java.util.Scanner;
public class Pr7_Math
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
double n = in.nextDouble();
System.out.println("Absolute value = " + Math.abs(n));
System.out.println("Square root = " + Math.sqrt(n));
System.out.println("Cube root= " + Math.cbrt(n));
}
}

Variable description: -

Variable name Data Type Description


n Double Enter a number

Output: -
Program 8
Write a program to accept a number and check whether the number is divisible by 3 as well as 5.
Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.

Program: -
import java.util.Scanner;
public class Pr8_divby_3_5
{
public static void main()
{
{ int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
a=sc.nextInt();
if((a%3==0)&&(a%5==0))
System.out.println("Number div by 3 and 5");
else if((a%3==0)&&(a%5!=0))
System.out.println("Number div by 3 and not div by 5");
else if((a%3!=0)&&(a%5==0))
System.out.println("Number not div by 3 and div by 5");
else
System.out.println("Number not div by 3 and not by 5");
}}}

Variable description: -
Variable name Data Type Description
a Integer Enter a number

Output: -
Program 9
Write a program to calculate the value of the given expression:
1/a2 + 2/b2 + 3/c2
Take the values of a, b and c as input from the console. Finally, display the result of the expression to
its nearest whole number.

Program: -
import java.util.Scanner;
public class Pr9_expr
{
public static void main()
{
double a,b,c,sum;
Scanner scn = new Scanner(System.in);
System.out.println("Enter 3 numbers:-");
a = scn.nextDouble();
b = scn.nextDouble();
c = scn.nextDouble();
sum=(1/(a*a))+(2/(b*b))+(3/(c*c));
System.out.println("Sum="+sum+"="+Math.round(sum));
}
}

Variable description: -
Variable name Data Type Description
a double Enter 1st number
b double Enter 2nd number
c double Enter 3rd number

Output: -
Program 10
Write a program to enter a number. If the number is positive even number display three succeeding
even numbers. If the number is negative odd, display three succeeding odd numbers, otherwise
display number is neither positive even or negative odd.
Sample input: -21
Output: -23, -25, -27

Program: -
import java.util.Scanner;
public class Pr10_evn_odd
{
public static void main()
{
int a;
Scanner scn=new Scanner(System.in);
System.out.println("Enter a number:-");
a=scn.nextInt();
if(a>0 && a%2==0)
System.out.println((a+2)+","+(a+4)+","+(a+6));
else if(a<0 && a%2!=0)
System.out.println((a-2)+","+(a-4)+";"+(a-6));
else
System.out.println("Not positive even/negative odd");
}
}

Variable description: -

Variable name Data Type Description


a Integer Enter a number

Output: -
Program 11
Write a program that converts degree Fahrenheit (F) input by the user to degree Celsius (C) using
the formula C = (F − 32)/1.8

Program: -
import java.util.Scanner;
public class Pr11_Fahrenhiet_To_Celsius
{
public static void main()
{
Scanner sc = new Scanner(System.in) ;
double Fahrenheit, Celsius;
System.out.println("Enter Temperature in Fahrenheit");
Fahrenheit = sc.nextDouble();
Celsius = (Fahrenheit-32)/1.8;
System.out.println("Temperature in Celsius ..."+Celsius);
}
}

Variable description: -

Variable name Data Type Description


Fahrenheit Double Enter temperature in Fahrenheit

Output: -
Program 12
Write a program to input two numbers. Display the numbers after swapping them by using a third
variable.
Input a=60, b=90;
Output: a=90, b=60;

Program: -
import java.util.Scanner;
public class Pr12_swap
{
public static void main()
{
int a,b, Temp;
Scanner scn = new Scanner(System.in);
System.out.println("Enter 2 numbers:-");
a=scn.nextInt();
b=scn.nextInt();
System.out.println("Before Swap a= "+a+"b="+b);
Temp=a;
a=b;
b=Temp;
System.out.println("After Swap a= "+a+" b= "+b);
}
}

Variable description: -

Variable name Data Type Description


a Integer Enter 1st number to be swapped
b Integer Enter 2nd number to be swapped
temp integer Temporary variable for swapping

Output: -
Program 13
Write a program to input the time in seconds.
Display the time after converting them into hours, minutes and seconds.
Sample Input: Time in seconds 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds

Program: -
import java.util.Scanner;
public class Pr13_Time
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Time in seconds: ");
int inputTime = in.nextInt();
int hrs = inputTime / 3600;
int mins = (inputTime % 3600) / 60;
int secs = (inputTime % 3600) % 60;
System.out.println(hrs
+ " Hours "
+ mins
+ " Minutes "
+ secs
+ " Seconds");
}
}

Variable description: -
Variable name Data Type Description
Time Integer Time to be entered in sec

Output: -
Program 14
The relative velocity of two trains travelling in opposite directions is calculated by adding their
velocities. In case, the trains are travelling in the same direction, the relative velocity is the
difference between their velocities. Write a program to input the velocities and length of the trains.
Write a menu driven program to calculate the relative velocities and the time taken to cross each
other.

Program: -
import java.util.Scanner;
public class Pr14_Train
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("1.Trains travelling in same direction");
System.out.println("2.Trains travelling in opposite direction");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter first train velocity: ");
double speed1 = in.nextDouble();
System.out.print("Enter first train length: ");
double len1 = in.nextDouble();
System.out.print("Enter second train velocity: ");
double speed2 = in.nextDouble();
System.out.print("Enter second train length: ");
double len2 = in.nextDouble();
double rSpeed = 0.0;
switch(choice)
{
case 1:
rSpeed = Math.abs(speed1 - speed2);
break;
case 2:
rSpeed = speed1 + speed2;
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2.");
}
double time = (len1 + len2) / rSpeed;
System.out.println("Relative Velocity = " + rSpeed);
System.out.println("Time taken to cross = " + time);
}
}
Variable description: -

Variable name Data Type Description


Choice Integer To enter the choice of Train’s direction
Speed1 double Enter train1 speed
Speed2 double Enter train2 speed
Len1 double Enter train1 length
Len2 double Enter train2 length

Output: -
Program 15
Write a program to input a year and check whether it is a) a leap year b) a century leap year c) a
century year but not a leap year.
Sample input 2000
Sample output: it is a century leap year.

Program: -
import java.util.Scanner;
public class Pr15_LeapYear
{
public static void main()
{
int yr;
Scanner scn = new Scanner(System.in);
System.out.print("Enter the year as in YYYY:- ");
yr =scn.nextInt();
if (yr % 4 == 0 && yr % 100 != 0)
System.out.println("It is a Leap Year");
else if (yr % 400 == 0)
System.out.println("It is a Century Leap Year");
else if (yr % 100 == 0)
System.out.println("It is a Century Year but not a Leap Year");
else
System.out.println("It is neither a Century Year nor a Leap Year");
}
}

Variable description: -
Variable name Data Type Description
yr Integer Enter year

Output: -
Acknowledgement
I would like to take this opportunity to express
special gratitude to my history 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.

You might also like