class9thimportantprograms
class9thimportantprograms
ASSIGNMENT FILE
OF
JAVA PROGRAMMING
Session 2024-25
INDEX
Sno. Program description REMARKS
To create a class 'Trip' without any data member but having function 'void show()' to
1
display information like place ,date ,no. of students , name of organizer, trip charges
2 To calculate addition and subtraction of two numbers
To input time in seconds. Display the time after convert them into hours, minutes and
3
seconds
To calculate following equation by using mathematical method 22/square root of 7*x
4
(Taking x as input)
5 To check whether number is even or odd
6 To find greatest number out of two number.(input from user)
7 To find smallest number among two numbers by using ternary operator
8 To find greatest number out of three numbers by using nested if.
9 To input a number and check whether number is buzz or not.
To enter three sides of a triangle and check whether the triangle is possible or not. If
10 possible , then display whether it is an equilateral ,an isosceles or a Scalene triangle,
otherwise display the message 'Triangle is not possible'.
To enter two unequal numbers . If the first number is greater then display Square of the
11 smaller number and cube of the greater number otherwise ,vice versa .If the numbers are
equal ,display the message "Both the numbers are equal".
Without using if -else statement and ternary operators ,accept three unequal numbers and
12
Display the second smallest number.
To displays the result of the following evaluations based on the choice entered by the user:
1.Natural logarithm of the number
2.Absolute value of the number
13
3.Square root of the number
4.Cube root of the number
5 Random number between 0 and 1
Using a switch case statement , write a menu driven program to convert a given
14 temperature from Fahrenheit to Celsius and vice versa .For an incorrect choice, an
appropriate message should be displayed.
To find and display the volume of different solids by taking suitable variables and data
15
types as inputs.
To calculate and display the sum of all odd numbers and even numbers between a range of
16
numbers from m to n(both inclusive)where m<n. Input m and n.
17 To input a number and check whether number is palindrome or not.
18 To input a number and find sum of square of digits of numbers
19 To find and display sum of series 1/a +1/a^2 +1/a^3.....1/a^ n.
To display the following patterns:
5
54
20
543
5432
54321
To display the following patterns:
9
97
21 975
9753
97531
22 To find and display the sum of any ten natural numbers
To display the sum of positive even numbers and negative odd numbers from a list of
23 numbers entered by the user. The list terminates when the user enters zero from the
console.
To input a number. Display the sum of each digit, raised to the power of the number of
24
digits.
25 To find the quotient and remainder without using / and % operator
/*PROGRAM-1*/
/*Write a program to create a class 'Trip' without any data member but having function 'void show()'
to display information like place ,date ,no .of students ,name of organizer, trip charges */ class Trip
{
public void show()
{
System.out.println("Place:Shimla");
System.out.println("Date:June1,2021");
System.out.println("No.of Student:225");
System.out.println("Name of Organizer:Anmoldeep");
System.out.println("Trip Charges:1000 per student");
}}
class
Info {
public static void main(String args[])
{
Trip t1=new Trip();
t1.show();
}
}
OUTPUT(PROGRAM-1)
Place:Shimla
Date:June1,2021
No.of Student:225
Name of Organizer:Anmoldeep
Trip Charges:1000 per student
/*PROGRAM-2*/
// Write a program to calculate addition and subtraction of two numbers
import java.util.Scanner; public class numbers
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int a=in.nextInt();
int b=in.nextInt();
System.out.println("Addition of numbers:"+(a+b));
System.out.println("Subtraction of numbers:"+(a-b));
}
}
OUTPUT(PROGRAM-2)
Enter numbers:
40
34
Addition of numbers:74 Subtraction
of numbers:6
/*PROGRAM-3*/
/*Write a program to input time in seconds. Display the time after convert them into hours, minutes
and seconds*/ import java.io.*; public class Time
{
public static void main(String[]args)throws IOException
{
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader in =new BufferedReader(read);
System.out.println("Enter time:"); int Secs
=Integer.parseInt(in.readLine());
int hrs=Secs/3600;
Secs%=3600; int
mins=Secs/60;
Secs%=60;
System.out.println(hrs+"Hours"+mins+"Minutes"+Secs+"Seconds");
}
}
OUTPUT(PROGRAM-3)
Enter time:
5420
1Hours30Minutes20Seconds
/*PROGRAM-4*/
/*Write a program to calculate following equation by using mathematical method 22/ 7 *x (Taking x
as input)*/ import java.util.Scanner; public class calculation
{
public static void main(String[]args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter number:");
int x=in.nextInt();
double calculation=(double)22/Math.sqrt(7)*x;
System.out.println("Calculation:"+calculation);
}
}
OUTPUT(PROGRAM-4)
Enter number:
2
Calculation:16.630436812405996
/*PROGRAM-5*/
//Write a program to check whether number is even or odd
import java.util.Scanner; public
class EVEN_ODD
{
public static void main(String[]args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter number:");
int num=in.nextInt(); if(num
%2==0)
System.out.println(num+"is even number");
else
System.out.println(num+"is odd number");
}
}
OUTPUT(PROGRAM-5)
Enter number:
42
42is even number
/*PROGRAM-6*/
//Write a program to find greatest number out of two numbers (input from user)
import java.util.Scanner; class great {
public static void main(String[]args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int a=in.nextInt();
int b=in.nextInt();
if(a>b)
System.out.println(a+"is great no.");
else
System.out.println( b+"is great no.");
}
}
OUTPUT(PROGRAM-6)
Enter numbers:
56
78
78is great no.
/*PROGRAM-7*/
//Write a program to find smallest number among two numbers by using ternary operator
import java.util.Scanner;
class sn {
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int a=in.nextInt();
int b=in.nextInt();
int sn=(a<b)?a:b;
System.out.println("Smaller no.="+sn);
}
}
OUTPUT(PROGRAM-7)
Enter numbers:
34
35
Smaller no.=34
/*PROGRAM-8*/
//Write a progam to find greatest number out of three numbers by using nested if. import
java.util.Scanner;
class GN
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number:");
int a=in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
if(a<b)
{ if(b>c)
System.out.println("The Greatest no.="+b);
else
System.out.println("The Greatest no.="+c);
}
else if(b<c)
{ if(c>
a)
System.out.println("The Greatest no.="+c);
else
System.out.println("The Greatest no.="+a);
}
else if(c<a)
{ if(a>
b)
System.out.println("The Greatest no.="+a);
else
System.out.println("The Greatest no.="+b);
}
else
System.out.println();
}
}
OUTPUT(PROGRAM-8)
Enter the number:
56
67
98
The Greatest no.=98
/*PROGRAM-9*/
//Write a program to input a number and check whether number is buzz or not.
import java.util.Scanner; class buzz {
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
System.out.println("Entered value is:");
int num =in.nextInt(); if(num%7==0||num
%10==7)
System.out.println(num+"is a Buzz number");
else
System.out.println(num+"is not a Buzz number");
}
}
OUTPUT(PROGRAM-9)
Entered value is:
47
47is a Buzz number
/*PROGRAM-10*/
/*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 an equilateral ,an isosceles or a Scalene triangle, otherwise
display the message 'Triangle is not possible'.*/ import java.util.Scanner; public class triangle
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter three sides of triangle:");
int a=in.nextInt(); int b=in.nextInt(); int
c=in.nextInt();
if((a+b>c)&&(b+c>a)&&(c+a>b))
{
System.out.println("Triangle is Possible");
if((a==b)&&(b==c)&&(c==a))
{ System.out.println("Equilateral triangle");
System.exit(0);
}
if((a==b)||(b==c)||(c==a))
{ System.out.println("Isosceles triangle");
System.exit(0);
}
if((a!=b)&&(b!=c)&&(c!=a))
System.out.println("Scalene triangle");
}
else
System.out.println("Triangle is not Possible");
}
}
OUTPUT(PROGRAM-10)
Enter three sides of triangle:
4
5
3
Triangle is Possible
Scalene triangle
/*PROGRAM-11*/
/* Write a program to enter two unequal numbers . If the first number is greater then display square
of the smaller number and cube of the greater number otherwise ,vice versa .If the numbers are equal
,display the message "Both the numbers are equal". */
import java.util.Scanner; public class number
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int a=in.nextInt();
int b=in.nextInt();
int c;int s;
if(a!=b)
{ if(a>b)
{ s=b*b;
c=a*a*a;
}
else
{ s=a*a;
c=b*b*b;
}
System.out.println("The square of the no.:"+s);
System.out.println("The cube of the no.:"+c);
}
else
System.out.println("Both the no. are equal");
}
}
OUTPUT(PROGRAM-11)
Enter numbers:
3
4
The square of the no.:9
The cube of the no.:64
/*Without using if -else statement and ternary operators ,accept three unequal numbers and display
the second smallest number. */ import java.util.Scanner; public class small_no
/*PROGRAM-12*/
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int a=in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
int sn=Math.max(a,b);
int sno=Math.min(sn,c);
System.out.println("The second smallest no."+sno);
}
}
OUTPUT(PROGRAM-12)
Enter numbers:
9
5
8
The second smallest no.8
/*PROGRAM-13*/
/* Write a program that displays the result of the following evaluations based on the choice
entered by the user:
1.Natural logarithm of the number 2.Absolute value of the number
3.Square root of the number 4.Cube root of the number
*/
import java.util.Scanner; class
maths
{ public static void main(String[]args)
{ Scanner in=new Scanner(System.in);
System.out.println("Enter 1 to find the Natural Log");
System.out.println("Enter 2 to find the Absolute value");
System.out.println("Enter 3 to find the Square root");
System.out.println("Enter 4 to find the cube root");
System.out.println("Enter your choice 1-4");
int choice=in.nextInt();
int num;
switch(choice)
{ case
1:
System.out.println("Enter a number");
num=in.nextInt();
System.out.println("The Natural Log of the number:"+Math.log(num));
break; case 2:
System.out.println("Enter a number");
num=in.nextInt();
System.out.println("The Absolute value of the number:"+Math.abs(num));
break; case 3:
System.out.println("Enter a number");
num=in.nextInt();
System.out.println("The square root of the number:"+Math.sqrt(num));
break; case 4:
System.out.println("Enter a number");
num=in.nextInt();
System.out.println("The cube root of the number:"+Math.cbrt(num));
break; default:
System.out.println("Wrong choice");
} }}
OUTPUT(PROGRAM-13)
Enter 1 to find the Natural Log
Enter 2 to find the Absolute value
Enter 3 to find the Square root
Enter 4 to find the cube root
Enter your choice 1-4
3
Enter a number
49
The square root of the number:7.0
/*PROGRAM-14*/
/*Using a switch case statement , write a menu driven program to convert a given Temperature from
Fahrenheit to celsius and vice versa.For an incorrect choice,an appropriate message should be
displayed. */ import java.util.Scanner; public class temperature
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter choice:");
System.out.println("Enter 1 to change Fahrenheit to celsius:");
System.out.println("Enter 2 to change celsius to Fahrenheit:");
int o=in.nextInt(); double c; double f;
switch(o)
{
case 1: System.out.println("Enter temperature in Fahrenheit:");
f=in.nextDouble(); c=5.0/9.0*(double)(f-32);
System.out.println("Temperature in Fahrenheit to celsius:"+c);
break;
case 2: System.out.println("Enter temperature in celsius:");
c=in.nextDouble(); f=1.8*c+32;
System.out.println("Temperature in celsius to Fahrenheit:"+f);
break;
default: System.out.println("Incorrect choice");
}
}
}
OUTPUT(PROGRAM-14)
Enter choice:
Enter 1 to change Fahrenheit to celsius:
Enter 2 to change celsius to Fahrenheit:
2
Enter temperature in celsius:
0 Temperature in celsius to
Fahrenheit:32.0
/*PROGRAM-15*/
/*The volume of solids,viz. cuboid,cylinder and cone can be calculated by the formula:
1.volume of cuboid(v= l*b*h)
2.volume of cylinder(v=Math.PI*r*r*h)
3.volume of cone(v=1/3*Math.PI*r*r*h)
Using a switch case statement,write a program to find and display the volume of different
solids by taking suitable variables and data types as inputs. */ import java.util.Scanner;
public class volume
{ public static void main (String[] args) {
Scanner in =new Scanner(System.in);
double v; int l,b,h,r;
System.out.println("Enter 1 to calculate volume of cuboid");
System.out.println("Enter 2 to calculate volume of cylinder");
System.out.println("Enter 3 to calculate volume of cone"); int
c= in.nextInt(); switch(c)
{ case
1:
System.out.println("Enter length and breadth and height:");
l=in.nextInt(); b=in.nextInt(); h=in.nextInt();
v=l*b*h;
System.out.println("volume of cuboid:"+v);
break; case 2:
System.out.println("Enter radius and height:");
r=in.nextInt(); h=in.nextInt();
v=(double)Math.PI*r*r*h;
System.out.println("volume of cylinder:"+v);
break; case 3:
System.out.println("Enter radius and height:");
r=in.nextInt(); h=in.nextInt();
v=(double)1.0/3.0*Math.PI*r*r*h;
System.out.println("volume of cone:"+v);
break; default:
System.out.println();
break;
} }}
OUTPUT(PROGRAM-15)
Enter 1 to calculate volume of cuboid
Enter 2 to calculate volume of cylinder
Enter 3 to calculate volume of cone
1
Enter length and breadth and height:
4
5
4
volume of cuboid:80.0
/*PROGRAM-16*/
/*Write a program to calculate and display the sum of all odd numbers and even numbers between a
range of numbers from m to n(both inclusive)where m<n. Input m and n. */ import
java.util.Scanner; public class range_number
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter numbers:");
int m=in.nextInt();
int n=in.nextInt();
int i;int e=0; int o=0;
for(i=m;i<=n;i++)
{
if(i%2==0)
e=e+i; else
o=o+i;
}
System.out.println("Sum of all even number:"+e);
System.out.println("Sum of all odd number:"+o);
}
}
OUTPUT(PROGRAM-16)
Enter numbers:
3
10
Sum of all even number:28
Number is palindrome
/*PROGRAM-18*/
//Write a program to input a number and find sum of square of digits of numbers
import java.util.Scanner; public class num
{
public static void main(String[]args)
{
int num;
Scanner in=new Scanner(System.in);
System.out.println("Enter value ");
num=in.nextInt();
int r=0;
int n=0;
int i=0;
for(i=num;i>0;i=i/10)
{ r=i
%10;
n=n+r*r;
}
System.out.println("Sum of square of digits of number:"+n);
}
}
OUTPUT(PROGRAM-18)
Enter value
64
Sum of square of digits of number:52
/*PROGRAM-19*/
//Write a program to find and display sum of series 1/a +1/a^2 +1/a^3.....1/a^n
import java.util.Scanner; public class series
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter number:");
int a=in.nextInt();
System.out.println("Enter a number:");
int n=in.nextInt();
int i;
double s=0;
for(i=1;i<=n;i++)
s=s+(double)1/Math.pow(a,i);
System.out.println(s);
}
}
OUTPUT(PROGRAM-19)
Enter number:
2
Enter a number:
8
0.99609375
/*PROGRAM-20*/
/*Write the programs in java to display the following patterns:
5
54
543
5432
54321
*/
public class pattern
{
public static void main(String[]args)
{ int i;int j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT(PROGRAM-20)
5
54
543
5432 5
4321
/*PROGRAM-21*/
/*Write the programs in java to display the following patterns:
9
97
975
9753
97531
*/
public class patterns
{
public static void main(String[]args)
{ int
i;int j;
for(i=9;i>=1;i=i-2)
{
for(j=9;j>=i;j=j-2)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT(PROGRAM-21)
9
97
975
9753 9
7531
/*PROGRAM-22*/
//Write a program to find and display the sum of any ten natural numbers
import java.util.Scanner; public class natural_no
{
public static void main (String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter the natural number:");
int i;int num; int s=0;
for(i=1;i<=10;i++)
{
num=in.nextInt();
s=s+num;
}
System.out.println(" The sum of numbers:"+s);
}
}
OUTPUT(PROGRAM-22)
Enter the numbers:
2
1
3
6
7
4
5
9
8
45
The sum of numbers:90
/*PROGRAM-23*/
/* Write a program to display the sum of positive even numbers and negative odd numbers from a list
of numbers entered by the user. The list terminates when the user enters zero from the console. */
import java.util.Scanner; public class sum
{
public static void main(String[]args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter the numbers and zero to terminate:");
int num;int s=0;int p=0;
while(true)
{
num=in.nextInt();
if(num>0&& num%2==0)
s=s+num;
if(num<0&& num%2!=0)
p=p+num;
if(num==0)
break; }
System.out.println("SUM OF POSITIVE EVEN NUMBERS:"+s);
System.out.println("SUM OF NEGATIVE ODD NUMBERS:"+p);
System.out.println("PROGRAM TERMINATES!");
}
}
OUTPUT(PROGRAM-23)
Enter the natural number and zero to terminate:
4
-3
-7
6
0
SUM OF POSITIVE EVEN NUMBERS:10 SUM
OF NEGATIVE ODD NUMBERS:-10
PROGRAM TERMINATES!
/*PROGRAM-24*/
/* Write a program to input a number. Display the sum of each digit raised to the power of the
number of digits. */ import java.util.Scanner; public class Sum_Digits
{
public static void main(String[]args)
{
Scanner in =new Scanner(System.in);
System.out.println("Enter a number:");
int num=in.nextInt();
int i; int c=0;int d;double s=0;int p;
for(i=num;i>0;i=i/10)
{
c++;
}
System.out.println("Number of Digits:"+c);
for(p=num;p>0;p=p/10)
{
d=p%10;
s=s+Math.pow(d,c);
}
System.out.println("The sum:"+s);
}
}
OUTPUT(PROGRAM-24)
Enter a number:
64
Number of Digits:2
The sum:52.0
/*PROGRAM-25*/
// WAP to input two numbers and calculate quotient and remainder without using / and % operator
import java.util.Scanner; public class div
{
public static void main (String[] args)
{ int q=0;
Scanner in =new Scanner(System.in);
System.out.println("Enter one number:");
int a=in.nextInt();
System.out.println("Enter second number:");
int b=in.nextInt();
while(a>=b)
{
a=a-b;
q++; }
System.out.println(“quotient=”+q);
System.out.println(“remainder=”+a);
}
}
OUTPUT (PROGRAM-25)
Enter one number
20
Enter second number
3
quotient=6
remainder=2