Вы находитесь на странице: 1из 16

Java Assignments

Write a Java Program to find the Area of circle.

Import java.util.Scanner;
Public class AreaOfCircle {
Public static void main (String [] args)
{
Scanner IO = new Scanner (System.in);
System.out.print ("Type Radius of Circle: ");
Int radius = IO.nextInt ();
Double pi= 3.14;
Double area = pi*radius*radius;
System.out.print ("Area of Circle is: " + area);
}
}
Write a program to find sum and reverse of three digit number.
import java.util.Scanner;
import java.lang.Math;
import javax.swing.JOptionPane;
public class SumandReverse {
public static void main (String[] args)
{
Scanner IO = new Scanner(System.in);
int reversenum =0, num=0, num2=0,sum=0 , num3=0;
System.out.print("Type A Three Digit Number & Press Enter Key: ");
num = IO.nextInt();
int tmp = num;
while (tmp>0)
{
num2 = tmp%10;
tmp = tmp/10;
num3 = (num2+num3)*10;
sum = sum+num2;
}
num3= num3/10;
System.out.println("Sum of specified number is: "+sum);
}
}

Write a program to swap two numbers without using third variable.

Import java.util.Scanner;
Public class Swapping {
Public static void main (String [] args)
{
Scanner IO = new Scanner (System.in);
System.out.print ("Type First Number: ");
Int fnum = IO.nextInt ();
System.out.print ("Type Second Number: ");
Int Snum = IO.nextInt ();
fnum = fnum + Snum;
Snum = fnum - Snum;
fnum = fnum - Snum;
System.out.print ("First Number: " + fnum + "\n");
System.out.print ("Second Number: " + Snum);
}
}
Write a program to find simple interest.

Import java.util.Scanner;
Public class SimpleInterest {
Public static void main (String [] args)
{
Scanner IO = new Scanner (System.in);
System.out.print ("Type Amount: ");
Int amount = IO.nextInt ();
System.out.print ("Type Time: ");
Int time = IO.nextInt ();
System.out.print ("Type Rate: ");
Float rate = IO.nextFloat ();
Double SI = amount*time*rate/100;
System.out.print ("Simple Interest is: " + SI );
}
}
Write a program to find list of prime numbers.
Import java.util.Scanner;
Public class PrimeNumber {
Public static void main (String [] args)
{
Int num=0;
String primeNumbers="";
Scanner IO = new Scanner (System.in);
System.out.println ("Type number till you want prime number");
Int range = IO.nextInt ();
For (int i = 1; i <= range; i++)
{
Int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println ("Prime numbers from 0 to” + range + " are :");
System.out.println (primeNumbers);
}
}
Write a program to find sequence of n numbers which are divisible by 3
and 5 both.
import java.util.Scanner;
class Divisible {
public static void main(String[] args)
{
Scanner IO = new Scanner(System.in);
System.out.print("Type range of number: ");
int range = IO.nextInt();
for (int index=0;index<=range;index++)
{
if((index % 3 == 0) && (index % 5 == 0))
{
System.out.print(index + " ");
}
}
}
}
Write a program to find the roots of a Quadratic Equation.
import java.util.Scanner;
class QuadraticEquation {
public static void main (String[] args)
{
double a=0,x=0,b=0,c=0;
double eqn=(a*x*x)+(b*x)+c;
System.out.print("The Quadratic Equation of ax*x + bx+ c" + eqn);
}
}
Write a program to find the factorial of n Number.
import java.util.Scanner;
public class Factorial {
public static void main (String[] args)
{
Scanner IO = new Scanner(System.in);
System.out.print("Type a number whose factorial has to find: ");
int range = IO.nextInt();
int Fact=1;
for(int index= 1; index<=range;index++)
{
Fact= Fact*index;
}
System.out.print("Factorial of given number is: "+ Fact + "\n\n\n");

}
}

Write a program to find the sequence of Fibonacci series up to n


terms.
public class FibonacciSeries {
public static void main (String[] args)
{
int FirstNumber=0,SecondNumber=1,Result;
Scanner IO = new Scanner(System.in);
System.out.print("Type Range till where want to print Series: ");
int range = IO.nextInt();
System.out.print(FirstNumber + " " + SecondNumber + " ");
for(int index=0; index<=range;index++)
{
Result= FirstNumber+ SecondNumber;
FirstNumber = SecondNumber;
SecondNumber = Result;
System.out.print(Result + " ");
}
}

Write a program to check whether given number is palindrome or not.


import java.util.Scanner;
public class Palidrome {
public static void main(String args[]){
int r,sum=0,temp;
System.out.print("Type Number: ");
Scanner IO = new Scanner(System.in);
int num= IO.nextInt();

temp=num;
while(num>0){
r=num%10;
sum=(sum*10)+r;
num=num/10;
}
if(temp==sum)
System.out.println ("palindrome number ");
else
System.out.println ("not palindrome");
}
}
Write a program to find HCF of two numbers.
import java.util.Scanner;
public class clshcf {
public static void main(String args[]) {

int num1,num2,hcf=1;
Scanner obj=new Scanner(System.in);
System.out.println("Enter first number: ");
num1 = obj.nextInt();
System.out.println("Enter second number: ");
num2 = obj.nextInt();

for(int i = 1; i < num1 | i < num2 ; i++) {

if(num1%i==0 && num2%i==0) {


hcf=i;
}
}

System.out.println("The hcf of two numbers is: " + hcf);


}
}

Write a Java Program that will display the sum of 1+1/2+1/3…..+1/n.


import java.util.Scanner;

public class Sumoffact {

public static void main(String[] args)


{
int total;
float num=0;

Scanner obj = new Scanner(System.in);


System.out.println("Enter the nth number: ");
total= obj.nextInt();
for (int i = 2; i <= total; i++) {

num=num+(float)1/i;

}
System.out.println(num);
}
}
Write a Java Program that will print the following outputs:
Pattern 1:
public class Pattern {

public static void main(String args[]) {

for (int row = 1; row <= 5; row++)


{
for (int col = row; col > 0; col--)
{
System.out.print(row);
}
System.out.println("");
}

for (int row = 4; row > 0 ; row--)


{
for (int col = row; col > 0; col--)
{
System.out.print(row);
}
System.out.println("");;
}
}
}

Pattern 2:
public class Pattern{
public static void main(String args[]) {

int x=4,n=1;

for (int row = 5 ; row > 0; row--)


{
for (int space = row-1; space > 0; space--)
{
System.out.print(" ");

}
for(int num=5-x; num > 0 ; num--){
System.out.print(n);
System.out.print(" ");
}

x--;
n++;

for (int space = row-1; space > 0; space--)


{
System.out.print(" ");

System.out.println();
}
}
}

Patter 3:
public static void main(String args[]) {

for (int row = 1; row <= 5; row++)


{
for (int col = row; col > 0; col--)
{
System.out.print("$");
}
System.out.println("");
}
}
Write a Java Program to find product of two matrices.
Public class Product2Matrix {
Public static void main (String [] args)
{
int max1[][] = {{1,2,3},{2,3,4},{5,6,7}};
int max2[][] = {{8,9,10},{5,7,8},{31,5,3}};
Int max 3[] [] = new int [3] [3];
For (int inIndex = 0; inIndex<3; inIndex++)
{
For (int outIndex=0; outIndex<3; outIndex++)
{
max3 [inIndex] [outIndex] =0;
max3 [inIndex] [outIndex] = max1 [inIndex] [outIndex] *
max2 [inIndex] [outIndex];
System.out.print (max3 [inIndex] [outIndex] + " ");
}
System.out.println ();
}
}
}
Write a Java Program to find sum and subtraction of two matrices.
Sum of two matrices:
Public class SumMatrix {
Public static void main (String [] args)
{
int max1[][] = {{1,2,3},{2,3,4},{5,6,7}};
int max2[][] = {{8,9,10},{5,7,8},{31,5,3}};
Int max3 [] [] = new int [3] [3];
For (int inIndex = 0; inIndex<3; inIndex++)
{
For (int outIndex=0; outIndex<3; outIndex++)
{
max3 [inIndex] [outIndex] =0;
max3 [inIndex] [outIndex] = max1 [inIndex] [outIndex] +
max2 [inIndex] [outIndex];
System.out.print (max3 [inIndex] [outIndex] + " ");
}
System.out.println ();
}
}
}
Subtraction of two matrices:
Public class SubMatrix {
Public static void main (String [] args)
{
int max1[][] = {{1,2,3},{2,3,4},{5,6,7}};
int max2[][] = {{8,9,10},{5,7,8},{31,5,3}};
Int max3 [][]= new int [3][3];
For (int inIndex = 0; inIndex<3; inIndex++)
{
For (int outIndex=0; outIndex<3; outIndex++)
{
max3 [inIndex] [outIndex] =0;
max3 [inIndex] [outIndex] = max1 [inIndex] [outIndex] -
max2 [inIndex] [outIndex];
System.out.print (max3 [inIndex] [outIndex] + " ");
}
System.out.println ();
}
}
}
Write a Java Program to sort the list in ascending Order.
import java.util.Scanner;

public class sort {

public static void main(String[] args)


{
int size,temp;
Scanner obj = new Scanner(System.in);

System.out.println("Enter number of elements in an array : ");


size = obj.nextInt();

int [] list = new int[size];

System.out.println("Enter " + size + " elements in the array : ");


for (int i = 0; i < size; i++) {

list[i] = obj.nextInt();
}

for (int j = size-1; j < 0; j++) {

for (int k = size-1; k < 0; k++) {

if(list[k-1] > list[k]) {


temp=list[k];
list[k]=list[k-1];
list[k-1]=temp;
}
}

}
System.out.println("List in ascending order is: " + list);
}
}
Write a Java Program to convert decimal into binary number.
Import java.util.Scanner;
Public class DecimalintoBinary {
Public static void main (String [] args)
{
Int num, count = 0, a;
String x = "";
Scanner s = new Scanner (System.in);
System.out.print ("Enter any decimal number :");
Num = s.nextInt ();
while(num > 0)
{
a = num % 2;
if(a == 1)
{
count++;
}
x = x + "" + a;
num = num / 2;
}
System.out.println ("Binary number:"+x);
}
}
Write a Java Program to find largest and smallest of n numbers.
import java.util.Scanner;

public class clsfind {

public static void main(String[] args)


{
int size, greatest, smallest;
Scanner obj = new Scanner(System.in);

System.out.println("Enter number of elements in an array : ");


size = obj.nextInt();

int [] list = new int[size];

System.out.println("Enter " + size + " elements in the array : ");


for (int i = 0; i < size; i++) {

list[i] = obj.nextInt();
}
greatest = list[0];
smallest = list[0];

for(int j=size-1; j>0; j--){

if( greatest < list[j] )


{
greatest = list[j];
}

if( smallest > list[j] )


{
smallest = list[j];
}

System.out.println("The greatest number is: " + greatest);


System.out.println("The smallest number is: " + smallest);

}
}
Write a java program which shows the application of constructors.
Public class AppofConstructor {
Int x;

Public AppofConstructor (int y) {


x = y;
}
Public static void main (String [] args) {
AppofConstructor myObj = new AppofConstructor (5);
System.out.println (myObj.x);
}
}
Write a java program to find the electricity bill using inheritance.
The details are as follow:
Units Bill Rate
1-100 Rs 2 per unit
101-300 Rs 5 per unit
301-500 Rs 6 per unit
Above 500 Rs 8 per unit

Вам также может понравиться