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

JAVA PROGRAMS

1) Write a program to accept marks(m1,m2,m3) of a student and


display the total and average

//Program to accept the marks of a student .Display the total


and average
import java.util.Scanner;

public class StudentMarks //Defining a class


{

public static void main(String[] args) //main method


{
int m1,m2,m3,total;//Variable declaration
float avg;
//Scanner class
Scanner sc=new Scanner(System.in);
//Accept the marks subjectwise
System.out.println("Enter the marks of the student..");
System.out.print("Enter Marks in subject1:");
m1=sc.nextInt();
System.out.print("Enter Marks in subject2:");
m2=sc.nextInt();
System.out.print("Enter Marks in subject3:");
m3=sc.nextInt();
//calculate total and average
total=m1+m2+m3;
avg=total/3;
//Display the output
System.out.println("Ouput...");
System.out.println("---------");
System.out.println("Mark1:" + m1);
System.out.println("Mark2:" + m2);
System.out.println("Mark3:" + m3);
System.out.println("Total:" + total);
System.out.println("Average:" + avg);

}
2) Write a Java Program to accept principal,number of years
and rate of interest and Calculate Simple Interest (Use
the formula pnr/100)

//Program to calculate the Simple Interest

import java.util.Scanner;
public class SimpleInterest
{

public static void main(String[] args)


{
//Declare the variables p,r,t
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.println("SIMPLE INTEREST PROGRAM");
//Accept the values ..p,r,t
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
//Declare sis
float si;
//Calculate simple interest
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);

}
3) Write a program to accept perpendicular and base of a right
angled triangle.Calculate and display hypotenuse, area and
parameter of the triangle

//Program to program to accept perpendicular and base of a


right angled triangle.Calculate and display hypotenuse,
area and parameter of the triangle
import java.util.Scanner;
public class Triangle
{
public static void main(String str[])
{
int p,b;
double h,area,per;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the perpendicular:");

p=sc.nextInt();

System.out.println("Enter the base:");


b=sc.nextInt();

h=Math.sqrt(p*p+b*b);
area=1.0/2.0*p*b;
per=(p+b+h);

System.out.println("Hypotenuse of the Trianlge:"+h);


System.out.println("Area of the Trianlge:"+area);
System.out.println("Perimeter of the Trianlge:"+per);
}
}
4)A computer manufacturing company announces a special
offer to their customers on purchasing Laptops and the
Printers accordingly
On Laptop Discount - 15%
On Printers Discount - 10%

Write a program in Java to calculate the discount,if a


customer purchases a Laptop and Printer

//To find the discount on Computer & Printer

import java.util.Scanner;
public class Discount
{
public static void main(String str[])
{
int r1=15,r2=10,c,p;
double d1,d2,m=0,n=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the price of Laptop:");
c=sc.nextInt();
System.out.println("Enter the price of Computer:");
p=sc.nextInt();

d1=(double)r1/100*c;
d2=(double)r2/100*p;
m=c-d1;
n=p-d2;
System.out.println("Price of Laptop after discount:"+m);
System.out.println("Price of Printer after discount:"+n);
}
}
5) Write a program in Java to accept the number of days and
display the result after converting into number of years,
number of months and the remaining number of days

//To convert number of days into years,months,weeks and the


number of days
//To convert days into years,months and the number of days
import java.util.Scanner;

public class Days1


{
public static void main(String str[])
{
int n,b,year, week, day,mon;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of days:");
n = s.nextInt();
year = n / 365;
b = n % 365;
System.out.println("No. of Years:"+year);
mon=b/30;
System.out.println("No. of Months:"+mon);
week = n / 7;
n = n % 7;
System.out.println("No. of Weeks:"+week);
day = n;
System.out.println("No. of Days:"+day);

Note: Accept n(number of days)= 789 and check for the


output
6) Write a class with name employee and basic as its data
member.
Find the gross_pay of an employee for the following
allowances and deduction.
Use meaningful variables
Dearness Allowance=25% of Basic Pay (da)
House Rent Allowance=15% of Basic Pay (hra)
Provident Fund=8.33% of Basic Pay (pf)
Net Pay=Basic Pay + Dearness Allowance + House Rent
Allowance(np)
Gross Pay=Net Pay – Provident Fund (gp)
(Note: Please don’t use lengthy variable names
Caution: Use underscore for separating variable names and
avoid white spaces)

//A Sample Program to calculate Gross Salary using Scanner


Class
import java.util.Scanner;

public class Employee


{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
double basic,hra,da,pf,np,gp;
String name;
System.out.println(“Enter name of the Employee:”);
name = sc.nextLine();
System.out.println(“Enter Basic Salary:”);
basic = sc.nextFloat();
da = basic *25/100;
hra = basic * 15/100;
pf = basic*8.33/100;
gp = basic+hra+da;
np=gp-pf;
System.out.println(“Name of the Employee:”+name);
System.out.println(“Gross Pay:”+gp);
System.out.println(“Net Pay:”+np);
}
}
Programs of if,if..else,if..else..if..else and nested if

7) In an examination, the grades are given to the student


on the basis of average marks obtained.Write a program to
input name and average marks that displays the grade along
with the name accordingly
Marks Grades
80% and above Distinction
60% or more but less than 80% First Division
45% or more but less than 60% Second Division
40% or more but less than 45% Pass
Less than 40% Fail

//To display the grades according to the marks


//Program using if statement(Multiple ifs')

import java.util.Scanner;
public class Grade
{
public static void main(String str[])
{
int m;
String s1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name of the student:");
s1=sc.nextLine();

System.out.println("Enter the average marks obtained:");


m=sc.nextInt();

System.out.println("Student's Name:"+s1);
if(m>=80)
System.out.println("Grade: Distinction");

if(m>=60 && m<80)


System.out.println("Grade:First Division");

if(m>=45 && m<60)


System.out.println("Grade:Second Division");

if(m>=40 && m<=45)


System.out.println("Grade:Pass");

if(m<40)
System.out.println("No Grade");

}
}
8)Write a program to input three numbers(positive or
negative).If they are unequal then display the greatest
number otherwise,display they are equal.
The program also displays whether the numbers entered by
the user are "All Positive","All Negative" or "The
Combination of both"

import java.util.Scanner;
public class CheckNumber
{

public static void main(String str[])

{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter three positive or negative
numbers...");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();

if((a>0)&&(b>0)&&(c>0))
System.out.println("Numbers are Positive");

else if((a<0)&&(b<0)&&(c<0))
System.out.println("Numbers are Negative");

else
System.out.println("Numbers include Positive and Negative
numbers");

//To print the greatest number


if((a!=b)&&(b!=c)&&(c!=a))\
{
if((a>b)&&(a>c))
System.out.println(a+"is the greatest Number..");

else if((b>a)&&(b>c))
System.out.println(b+"is the greatest Number..");

else if((c>a)&&(c>b))
System.out.println(c+"is the greatest Number..");
}
else

System.out.println("Numbers are equal..");


}
}
9) The standard form of quadratic equation is given by
ax2+bx+c=0, where d=b2-4ac,is known as determinant that
determines the nature of the roots of the equation as:

Condition Nature
If d>=0 Roots are real
If d<0 Roots are imaginary

//Program to determine the roots of quadratic equation


import java.util.Scanner;

public class Quadratic


{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
double d,r1=0,r2=0;
System.out.println("Enter the values of a,b,c”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=Math.sqrt(b*b-4*a*c);
if((d>0)||(d==0))
{
r1=(-b+d)/(2*a);
r2=(-b+d)/(2*a);
System.out.println(“The roots are:” +r1+” and “+r2);
}
else
{
System.out.println(“The Roots are imaginary”);
}
}

}
Program on Switch..Case

10) Write a program to input length and breadth of a


rectangle.Calculate and display the area,perimeter and
diagonal of the rectangle as per the user’s choice

import java.util.Scanner;
public class Menu
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
int a,b,n,ar,per;
double d;
System.out.println("Enter the length of a rectangle");
a=sc.nextInt();
System.out.println("Enter the breadth of a rectangle");
b=sc.nextInt();

System.out.println("Enter 1 for area, 2 for perimeter, 3 for


diagonal:");
n=sc.nextInt();
switch(n)
{
case 1:
ar=a*b;
System.out.println("Area of rectangle:"+ar);
break;

case 2:
per=2*(a*b);
System.out.println("Perimeter of rectangle:"+per);
break;

case 3:
d=Math.sqrt(a*a+b*b);
System.out.println("Diagonal of rectangle:"+d);
break;

default:
System.out.println("Wrong Choice...");
}
}
}
Programs on Loops (for,while and do..while)

11) Write a program to accept two numbers and find the


Greatest Common Divisor(G.C.D) of two numbers
Sample Input: 25,45
Sample Output: The greatest Divisor :5

//To calculate G.C.D of two numbers

import java.util.Scanner;
public class GCD
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
int a,b,i,p,gcd=0;
System.out.println("Enter two numbers:");
a=sc.nextInt();
b=sc.nextInt();
p=a*b;
for(i=1;i<p;i++)
{
if(a%i==0 && b%i==0)
{
gcd=i;
}

System.out.println("The GCD of two numbers:"+gcd);


}
}
12) Write a program to accept 10 different numbers.
Display the greatest and the smallest numbers from a set
of numbers entered by the user

import java.util.Scanner;

public class Max_Min

public static void main(String str[])

Scanner sc=new Scanner(System.in);

int i,m,n,min=0,max=0;

System.out.println(“Enter the first number:”);

n=sc.nextInt();

min=n;

max=n;

System.out.println(“Enter the remaining numbers..”);

for(i=1;i<10;i++)

m=sc.nextInt();

if(m<min)

min=m;

if(m>max)

max=m;

System.out.println(“The greatest Number:”+max);

System.out.println(“The smallest Number:”+min);

}
13)In a game of tossing a coin, you want to know the number of
times getting ‘Head’ and ‘Tail’.

You keep the record as ‘1’(one) for getting ‘Head’ and ‘0’ for
getting ‘Tail’.

Write a program to perform the above task

Suppose, you tossed a coin for 20 times in this game

//Program to find the frequency of head and tail

import java.util.Scanner;
public class HeadTail
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
int i,c,h=1,t=0;
double d;
for(i=1;i<=20;i++)
{
d=Math.random()*2;
c=(int)d;
if(c==1)
h=h+1;
else
t=t+1;
}
System.out.println("Number of times Head Obtained:"+h);
System.out.println("Number of times Tail Obtained:"+t);
}
}
14)Write a program to display the given pattern
1
2 3
4 5 6
7 8 9 10

//Program to display the pattern

public class Pattern1


{
public static void main(String str[])
{

int i,j,p=0;
for(i=1;i<=5;i++)
{
for(j=1;j<i;j++)
{
p+=1;
System.out.print(p+" ");
}
System.out.println();
}
}

}
15)Write a program to accept a number and check whether the
number is Neon or not.
A number is said to be Neon if the sum of the digits of
square of a number is equal to the number itself
Sample Input: 9
Sample Input: 9*9=81,8+1=9 :9 is a Neon Number
(Use do..while)
//Program to check whether a number is Neon or not

import java.util.Scanner;
public class Neon1
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number");
int n=sc.nextInt();
int p,s=0,d;
p=n*n;
do
{
d=p%10;
s=s+d;
p=p/10;
}
while(p!=0);
if(s==n)
System.out.println("Neon Number...");
else
System.out.println("Not a Neon Number...");
}
}
16) You want to calculate the sum of all positive even
numbers and the sum of all negative odd numbers from a
set of numbers.
You can enter 0(zero) to quit the program and thus it
displays the result.
Write a program to perform the above task

//Program to find the sum as per user’s choice

import java.util.Scanner;

public class Max_Min2

public static void main(String str[])

Scanner sc=new Scanner(System.in);

int n,s1=0,s2=0;

System.out.println(“Enter a number or a zero to quit..”);

n=sc.nextInt();

while(n!=0)

if(n>0 && n%2==0)

s1=s1+n;

if(n<0 && n%2!=0)

s1=s1+n;

s2=s2+n;

System.out.println(“Do you want to Continue..”);

System.out.println(“Enter a number to Continue or zero to


quit..”);

n=sc.nextInt();

System.out.println(“The sum of positive even numbers:”+s1);

System.out.println(“The sum of negative odd numbers:”+s2);

}
Programs on Arrays

17) Write a program to accept 10 different numbers in a


Single Dimensional Array (SDA).
Display the sum of all the numbers which are divisible by
either
3 or 5

//Program to display the sum of numbers which are divisible


by 3 or 5

import java.util.Scanner;
public class Max_Min3
{
public static void main(String str[])
{
Scanner sc=new Scanner(System.in);
int i,s=0;
int a[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter the number in the cell..");
a[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
if((a[i]%3==0)||(a[i]%5==0))
s=s+a[i];
}
System.out.println("The Sum of Numbers:"+s);
}
}
18)Write a program to accept 10 different numbers in a Single
Dimensional Array(SDA).

Now, enter a number and search whether the number is present


or not in the list of array elements by using ‘Linear Search’
technique

//Program to search a number in SDA by using Linear Search


Technique

import java.util.*;

public class Linear1

public static void main(String str[])

Scanner sc=new Scanner(System.in);

int i,k=0,n;

int a[]=new int[10];

for(i=0;i<5;i++)

System.out.println("Enter:");

a[i]=sc.nextInt();

System.out.println("Enter the search element..");

n=sc.nextInt();

for(i=0;i<5;i++)

if(a[i]==n)

k=1;

if(k==1)

System.out.println("Element found...");

else

System.out.println("Element Not Found...");

}
19)Write a program to accept 10 different numbers in a Single

Dimensional Array (SDA).

Display the greatest and smallest numbers of the array elements

//Program to display the greatest and smallest numbers in SDA

import java.util.*;

public class BigSmallNo

public static void main(String str[])

Scanner sc=new Scanner(System.in);

int i,max,min;

int a[]=new int[10];

for(i=0;i<10;i++)

System.out.println(“Enter the no in the cell:”);

a[i]=sc.nextInt();

max=a[0];

min=a[0];

for(i=0;i<10;i++)

if(a[i]>max)

max=a[i];

if(a[i]<min)

min=a[i];

System.out.println(“The greatest of array elements:”+max);

System.out.println(“The smallest of array elements:”+min);

}
20)Write a program to accept 10 different numbers in a Single
Dimensional Array(SDA).

Arrange the numbers in ascending order by using “Selection Sort”


technique and display them

//To arrange the numbers by using Selection Sort Technique

import java.util.*;

public class SelectionSort

public static void main(String str[])

Scanner sc=new Scanner(System.in);

int i,j,t,min;

int a[]=new int[10];

for(i=0;i<10;i++)

System.out.println(“Enter the number in the cell:”);

a[i]=sc.nextInt();

for(i=0;i<9;i++)

min=i;

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

If(a[j]<a[min])

min=j;

t=a[i];

a[i]=a[min];

a[min]=t;

}
System.out.println(“The numbers in the Ascending Order are...”);

for(i=0;i<10;i++)

System.out.println(a[i]);

} }

You might also like