Computer Project
Computer Project
....Teachers Signature.....
INDEX
Serial Number Programs Page Number
1. Based on Input 1 to 10
2. Based on Calculation 11 to 20
3. Based on If……else 21 to 30
5. Based on Iliteration 41 to 60
5 JAVA PROGRAMS BASED ON INPUT
import java.util.Scanner;
class TriangleSide
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter first side of tiangle");
int a = sc.nextInt();
System.out.print("Enter second side of tiangle");
int b = sc.nextInt();
System.out.print("Enter third side of tiangle");
int c = sc.nextInt();
}
}
Output
import java.util.*;
class number
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
}
}
OUTPUT
import java.util.*;
class age
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter first age- ");
int a= sc.nextInt();
System.out.print("Enter second age- ");
int b= sc.nextInt();
System.out.print("Enter third age- ");
int c= sc.nextInt();
}
}
OUTPUT
}
}
Output
import java.util.Scanner;
class RectanglePerimeter
{
public static void main(String[] args)
{
int p;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter sides of rectangle");
int l=sc.nextInt();
int b=sc.nextInt();
p=(2*l+2*b);
System.out.print("Perimeter of Rectangle is:"+p);
}
}
Output
import java.util.Scanner;
class SquarePerimeter
{
public static void main(String[] args)
{
int p;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter sides of square");
int s=sc.nextInt();
p=(4*s);
System.out.print("Perimeter of Square is:"+p);
}
}
Output
import java.util.Scanner;
class Rectanglearea
{
public static void main(String[] args)
{
int a;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter sides of rectangle");
int l=sc.nextInt();
int b=sc.nextInt();
a=(l*b);
System.out.print("Area of rectangle is:"+a);
}
}
Output
Enter sides of rectangle3
4
Area of rectangle is:12
5. Write a program to calculate simple interest.
import java.util.Scanner;
class SimpleInterest
{
public static void main(String[] args)
{
int I;
Scanner sc=new
Scanner(System.in);
System.out.print("Enter Princilpe");
int p=sc.nextInt();
System.out.print("Enter Rate");
int r=sc.nextInt();
System.out.print("Enter Time");
int t=sc.nextInt();
I=(p*r*t/100);
System.out.print("Simple Interest is:"+I);
}
}
Output
else
System.out.println("Not a valid Triangle");
}
}
Output
Enter three angles
A
B
C
The function will calculate whether
it is valid triangle or not
And we will get the output as
valid triangle
Or
not a valid triangle
5. Wap to input three angels of a triangle and check whether it
is right angle triangle or not.
import java.util.*;
class program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three angels");
a=sc.nextInt();
b= sc.nextInt();
c=sc.nextInt();
if(a==90 || b==90 || c==90)
System.out.println("Right angle triangle");
else
System.out.println("Not a right angle Triangle");
}
}
Output
Enter three angles
A
B
C
The function will calculate whether it is
right angle triangle or not
And we will get the output as
Right angle triangle
Or
Not a right angle triangle
5 Java programs based on switch case
1. Write a program to enter the dys of week and press 1 to 7 to find the
day.
import java.util.*;
class menudriven
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch;
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1:System.out.println("Sunday");
break;
case 2:System.out.println("Monday");
break;
case 3:System.out.println("Tuesday");
break;
case 4:System.out.println("Wednesday");
break;
case 5:System.out.println("Thursday");
break;
case 6:System.out.println("Friday");
break;
case 7:System.out.println("Saturday");
break;
default: System.out.println("wrong choice");
}
}
}
Output
Enter your choice
3
Tuesday
Enter your choice
5
Thursday
2. Write a program to input two numbers press 1 to find sum and press 2
to find product.
import java.util.*;
class menudriven
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch,a,b,c;
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
System.out.println(“Press 1 to print sum”);
System.out.println(“Press 2 to print product”);
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1:c=a+b;
System.out.println("Sum is"+c);
break;
case 2:c=a*b;
System.out.println("Product is"+c);
break;
default:
System.out.println("wrong choice");
}
}
}
Output
Enter two numbers
3
3
Press 1 to print sum
Press 2 to print product
Enter your choice
1
Sum is6
3. Write a menu driven to input two numbers . press 1 to print the greater
number. press 2 to print the smaller number
import java.util.*;
class menudriven
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch,a,b,c;
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
System.out.println(“press 1 to print greater”);
System.out.println(“press 2 to print smaller number”);
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1:if(a>b)
System.out.println("Greater umber is"+a);
else
System.out.println("Greater number is"+b);
break;
case 2:
if(a<b)
System.out.println("Smaller number is"+a);
else
System.out.println("Smaller number is"+b);
break;
default:
System.out.println("wrong choice");
}
}
}
Output
Enter two numbers
5
7
press 1 to print greater
press 2 to print smaller number
Enter your choice
1
Greater number is7
4. Write a program to calculate mathematical functions.
import java.io.*;
public class Mathematics
{
public static void main(String args[])throws IOException
{
int n,ch;
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader in =new BufferedReader(read);
System.out.println("Enter 1 to find the Natural Log");
System.out.println("Enter 2 to find absolute value");
System.out.println("Enter your choice 1 or 2");
ch=Integer.parseInt(in.readLine());
switch (ch)
{
case 1:
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
System.out.println("The Natural Log of" +n + "is "+Math.log(n));
break;
case 2:
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
System.out.println("The absolute value of" +n + "is "+Math.abs(n));
break;
default:
System.out.println("Wrong Choice!!");
}
}
}
Output
Enter 1 to find the Natural Log
Enter 2 to find absolute value
Enter your choice 1 or 2
1
Enter a number
5
The Natural Log of 5 is 1.6094379124341003
5. Write a program to calculate the square root and cube root .
import java.io.*;
public class Square_Cube_Root
{
public static void main(String args[])throws IOException
{
int n,ch;
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader in =new BufferedReader(read);
System.out.println("Enter 1 to find the Square root");
System.out.println("Enter 2 to find cube root");
System.out.println("Enter your choice 1 or 2");
ch=Integer.parseInt(in.readLine());
switch (ch)
{
case 1:
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
System.out.println("The Square root of" +n + "is "+Math.sqrt(n));
break;
case 2:
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
System.out.println("The cube root" +n + "is "+Math.cbrt(n));
break;
default:
System.out.println("Wrong Choice!!");
}
}
}
Output
Enter 1 to find the Square root
Enter 2 to find cube root
Enter your choice 1 or 2
1
Enter a number
4
The Square root of4is 2.0
5 Programs based on Iliteration
1. Write a program to print your name thirty times
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i; for(i=1;i<=30;i++)
{
System.out.println("Rahul");
}
}
}
Output
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
Rahul
And till 30 times
2. Write a program to print all natural numbers from 1 to 10.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i;
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}
Output
1
2
3
4
5
6
7
8
9
10
3. Write a program to print all natural numbers 20 to 1.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i;
for(i=20;i>=1;i--)
{
System.out.println(i);
}
}
}
Output
20 4
19 3
18 2
17 1
16
15
14
13
12
11
10
9
8
7
6
5
4
4. Write a program to print all even numbers from 2 to 20.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i;
for(i=2;i<=20;i+=2)
{
System.out.println(i);
}
}
}
Output
2
4
6
8
10
12
14
16
18
20
5. Write a program to print all even numbers from 20 to 2.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i;
for(i=20;i>=2;i-=2)
{
System.out.println(i);
}
}
}
Output
20
18
16
14
12
10
8
6
4
2
6. Write a program To find the product of first five natural numbers.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,f=1;
for(i=1;i<=5;i++)
{
f=f*i;
}
System.out.println("Product is"+f);
}
}
Output
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,n,f=1;
System.out.println("enter any number");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial is"+f);
}
}
Output
Factorial is6
Factorial is362880
9.Write a program to input any number and print its factors.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,n,f=1;
System.out.println("enter any number");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
System.out.println(i);
}
}
}
Output
1
5
10. Write a program to input any number and find the sum of all factors
of a number.
import java.util.*;
class ARR
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,n,sum=0;
System.out.println("enter any number");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
sum=sum+i;
}
System.out.println("sum of all factors are"+sum);
}
}
Output
enter any number
5
sum of all factors are6