Java Programs
Java Programs
}
2) Write a Java Program to accept principal,number of years
and rate of interest and Calculate Simple Interest (Use
the formula pnr/100)
import java.util.Scanner;
public class SimpleInterest
{
}
3) Write a program to accept perpendicular and base of a right
angled triangle.Calculate and display hypotenuse, area and
parameter of the triangle
p=sc.nextInt();
h=Math.sqrt(p*p+b*b);
area=1.0/2.0*p*b;
per=(p+b+h);
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
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("Student's Name:"+s1);
if(m>=80)
System.out.println("Grade: Distinction");
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
{
{
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");
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
Condition Nature
If d>=0 Roots are real
If d<0 Roots are imaginary
}
Program on Switch..Case
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();
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)
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;
}
import java.util.Scanner;
int i,m,n,min=0,max=0;
n=sc.nextInt();
min=n;
max=n;
for(i=1;i<10;i++)
m=sc.nextInt();
if(m<min)
min=m;
if(m>max)
max=m;
}
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’.
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
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
import java.util.Scanner;
int n,s1=0,s2=0;
n=sc.nextInt();
while(n!=0)
s1=s1+n;
s1=s1+n;
s2=s2+n;
n=sc.nextInt();
}
Programs on Arrays
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).
import java.util.*;
int i,k=0,n;
for(i=0;i<5;i++)
System.out.println("Enter:");
a[i]=sc.nextInt();
n=sc.nextInt();
for(i=0;i<5;i++)
if(a[i]==n)
k=1;
if(k==1)
System.out.println("Element found...");
else
}
19)Write a program to accept 10 different numbers in a Single
import java.util.*;
int i,max,min;
for(i=0;i<10;i++)
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];
}
20)Write a program to accept 10 different numbers in a Single
Dimensional Array(SDA).
import java.util.*;
int i,j,t,min;
for(i=0;i<10;i++)
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]);
} }