Tuesday, December 29, 2015
Saturday, December 19, 2015
- 4:50 AM
- 3 Comments
HCF and LCM of a number:
import java.util.*;
class hcfandlcm
{
public static void main()
{
System.out.println("Enter 2 numbers");
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int h=1;
int p=m*n;
for(int i=2;i<p;i++)
{
if(m%i==0 && n%i==0)
{
h=i;
}
}
int l=p/h;
System.out.println("HCF= "+h+" and LCM= "+l);
}
}
Thank You and make sure to Like and Comment.
- 4:38 AM
- 0 Comments
Factorial of large numbers:
import java.util.Scanner;
import java.math.BigInteger;
class BigFactorial
{
public static void main()
{
int n, c;
BigInteger inc = new BigInteger("1");
BigInteger fact = new BigInteger("1");
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
n = input.nextInt();
for (c = 1; c <= n; c++) {
fact = fact.multiply(inc);
inc = inc.add(BigInteger.ONE);
}
System.out.println(n + "! = " + fact);
}
}
Thank you and make sure to Like and Comment
- 4:22 AM
- 0 Comments
Factorial of a number:
import java.util.Scanner;
class Factorial
{
public static void main()
{
int n, c, fact = 1;
System.out.println("Enter a number");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
Thank you and make sure to like and comment.
- 4:10 AM
- 0 Comments
- 4:02 AM
- 0 Comments
Wednesday, December 16, 2015
- 6:50 AM
- 0 Comments
Perimeter of Circle, Rectangle and Circle:
import java.util.Scanner;
public class Perimeter
{
int r, l, b, s1, s2, s3;
double pi = 3.14,perimeter;
Scanner s = new Scanner(System.in);
void circle()
{
System.out.print("Enter radius of circle:");
r = s.nextInt();
perimeter = 2 * pi * r;
System.out.println("Perimeter of circle:"+perimeter);
}
void rectangle()
{
System.out.print("Enter length of rectangle:");
l = s.nextInt();
System.out.print("Enter breadth of rectangle:");
b = s.nextInt();
perimeter = 2 * (l + b);
System.out.println("Perimeter of rectangle:"+perimeter);
}
void triangle()
{
System.out.print("Enter length of first side of triangle:");
s1 = s.nextInt();
System.out.print("Enter length of second side of triangle:");
s2 = s.nextInt();
System.out.print("Enter length of third side of triangle:");
s3 = s.nextInt();
perimeter = s1 + s2 + s3;
System.out.println("Perimeter of triangle:"+perimeter);
}
public static void main()
{
Perimeter obj = new Perimeter();
obj.circle();
obj.rectangle();
obj.triangle();
}
}
Thank you and make sure to like and comment.
- 6:40 AM
- 0 Comments
Area of Triangle:
import java.util.Scanner;
class AreaTriangle {
public static void main() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width:");
double base = scanner.nextDouble();
System.out.println("Enter the height:");
double height = scanner.nextDouble();
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}
Thank you and make sure to like and comment.
- 6:36 AM
- 0 Comments
Area of Square:
import java.util.Scanner;
class SquareArea {
public static void main ()
{
System.out.println("Enter Side of Square:");
Scanner scanner = new Scanner(System.in);
double side = scanner.nextDouble();
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Thank you and make sure to like and comment.
- 6:32 AM
- 0 Comments
Area and Circumference of a Circle:
import java.util.Scanner;class Circle
{
static Scanner sc = new Scanner(System.in);
public static void main()
{
System.out.print("Enter the radius: ");
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}
Thank you and make sure to like and comment.
- 6:25 AM
- 0 Comments
Swapping the values of two variables without temp variable:
import java.util.*;
public class Swapping
{
public static void main()
{
int x,y;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the numbers you want to swap values of");
x=sc.nextInt();
y=sc.nextInt();
x = x + y;
y = x - y;
x = x - y;
System.out.println("Swapped values are: " +x+", "+y);
}
}
Thank you and make sure to like and comment.
- 6:22 AM
- 0 Comments
Swapping the values of two variables with temp variable:
import java.util.*;
public class Swapping
{
public static void main()
{
int x,y,temp;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the numbers you want to swap values of");
x=sc.nextInt();
y=sc.nextInt();
temp = x;
x = y;
y = temp;
System.out.println("Swapped values are: " +x+", "+y);
}
}
Thank you and make sure to like and comment.
- 6:19 AM
- 0 Comments
Division of two numbers with Scanners:
import java.util.*;
public class Div
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,result;
System.out.println("Please enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a/b;
System.out.println("Division is "+result);
}
}
Thank you and make sure to like and comment.
- 6:16 AM
- 0 Comments
Multiplication of two numbers with Scanners:
import java.util.*;
public class Multi
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,result;
System.out.println("Please enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a*b;
System.out.println("Multiplication is "+result);
}
}
Thank you and make sure to like and comment.
- 6:13 AM
- 0 Comments
Subtraction of two numbers with Scanners:
import java.util.*;
public class Subtract
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,result;
System.out.println("Please enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a-b;
System.out.println("Subtraction is "+result);
}
}
Thank you and make sure to like and comment.
- 6:10 AM
- 0 Comments
Add two numbers with Scanners:
import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,sum;
System.out.println("Please enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
sum=a+b;
System.out.println("Sum is "+sum);
}
}
Thank you and make sure to like and comment.
- 6:03 AM
- 0 Comments
- 6:02 AM
- 0 Comments
- 5:59 AM
- 0 Comments
- 5:38 AM
- 0 Comments