Showing posts with label Mathematical Functions. Show all posts
Showing posts with label Mathematical Functions. Show all posts

Wednesday, March 21, 2012

Java program to print a particular line of the Pascal's Triangle


/*This Java program accepts a number from the user and
 * then prints the particular line of the
 * pascal's triangle onto the screen. More information
 * about pascal's triangle: http://en.wikipedia.org/wiki/Pascal's_triangle
 Sample :
 Enter the line number : 6
 1.0 5.0 10.0 10.0 5.0 1.0
 */

import java.util.*;

public class pascal
{
    public static void main()
    {
         Scanner sc=new Scanner(System.in);
         System.out.print("Enter the line number : ");
         int l=sc.nextInt();
         double a[]=new double[l];
         a[0]=1;
         int j,k;
         for(j=1;j<l;j++)
         {
             a[j]=1;
             for(k=j-1;k>0;k--)
             {
                 a[k]=a[k]+a[k-1];
             }
             a[0]=1;
         }
         for(j=0;j<l;j++)
         {
             System.out.print(a[j]+" ");
         }
    }
}

//Author : Mayank Rajoria

Thursday, December 15, 2011

A Java program to print all prime numbers between 1 and 1000.

/**
 * This Java program to print all prime numbers between 1 and 1000.
 */

class prime1To1000
{
    public static void main()
    {
        int i,k,j;
        for(i=1;i<1000;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                System.out.println(i);
            }
        }
    }
}

//Author : Mayank Rajoria

A Java program to print all prime numbers between 1 and 100.


/**
 * This Java program to print all prime numbers between 1 and 100.
 */
class prime1To100
{
    public static void main()
    {
        int i,k,j;
        for(i=1;i<100;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                System.out.println(i);
            }
        }
    }
}

//Author : Mayank Rajoria
//https://mayanksworkshop.wordpress.com

A Java program to print the sum of all prime numbers between 1 and 1000

/**
 * This Java program calculates and prints the sum of all prime numbers
 * between 1 and 1000.
 */

class prime1To1000
{
    public static void main()
    {
        int i,k,j;
        double sum=0;
        for(i=1;i<1000;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                sum=sum+i;
            }
        }
        System.out.println("Sum = "+sum);
    }
}

//Author : Mayank Rajoria

A Java program to print the sum of all prime numbers between 1 and 100


/*
 * This Java program calculates and prints the sum of all prime numbers
 * between 1 and 100.
 */
class prime1To100
{
    public static void main()
    {
        int sum=0,i,k,j;
        for(i=1;i<100;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                sum=sum+i;
            }
        }
        System.out.println("Sum = "+sum);
    }
}

//Author : Mayank Rajoria

Friday, December 9, 2011

Sum of the series (1!)+(2!)+(3!)+...+(n!) in Java


/*Problem: Write a program in Java to find the sum of the series
 * using function name fact(int) to return the factorial
 * of the number in order to obtain each term.
 * The series :-
 * S=(1!)+(2!)+(3!)+...+(n!)
 */

import java.util.*;
class series3
{
    public static void main()
    {
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner(System.in);
        int n,i;
        n=sc.nextInt();
        double S=0,f=0;
        for(i=1;i<n;i++)
        {
            f=fact(i);
            S=S+f;
        }
        System.out.println("The sum ="+S);
    }
   
    public static double fact(int num)
    {
        int f=1,j;
        for(j=1;j<=num;j++)
        {
            f=f*j;
        }
        return(f);
    }
}

//Author: Mayank Rajoria

Sum of the series (1/1!)+(1/2!)+(1/3!)+...+(1/n!) in Java

/*Problem: Write a program in Java to find the sum of the series
 * using function name fact(int) to return the factorial
 * of the number in order to obtain each term.
 * The series :-
 * S=(1/1!)+(1/2!)+(1/3!)+...+(1/n!)
 */
import java.util.*;
class series2
{
    public static void main()
    {
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner(System.in);
        int n,i;
        n=sc.nextInt();
        double S=0,f=0;
        for(i=1;i<n;i++)
        {
            f=fact(i);
            S=S+(1/f);
        }
        System.out.println("The sum ="+S);
    }
   
    public static double fact(int num)
    {
        int f=1,j;
        for(j=1;j<=num;j++)
        {
            f=f*j;
        }
        return(f);
    }
}

//Author: Mayank Rajoria
//https://mayanksworkshop.wordpress.com

Sum of series (1*2)+(2*3)+(3*4)+...+((n-1)*n) in Java

/*Problem: Write a program in Java to find the sum of the series * using function name product(int,int) to return the product
 * of two numbers in order to obtain each term.
 * The series :-
 * S=(1*2)+(2*3)+(3*4)+...+((n-1)*n)
 */
import java.util.*;
class series1
{
    public static void main()
    {
        int n,i;
        System.out.print("Enter the value of n : ");
        Scanner sc=new Scanner (System.in);
        n=sc.nextInt();
        int S=0,pro=0;
        for(i=1;i<n;i++)
        {
            pro=product(i,i+1);
            S=S+pro;
        }
        System.out.println("The sum is "+S);
    }
   
    public static int product(int n1,int n2)
    {
        int mul;
        mul=n1*n2;
        return(mul);
    }
}
/*Output:
 Enter the value of n : 11
 The sum is 440
 */

//Author : Mayank Rajoria

Finding the factroial of a number using recursion (Java)

/* This  Java program uses 2 functions : main and fact. The main
 * function accepts a number from the user, calls the
 * fact function and prints the value(the answer) which it
 * recieves from the fact function.
 *
 * The fact function at first reciaves a number from the
 * main function and the keeps calling itself with a number,
 * 1 less then the number it first recieved.
 */

/*Output wndow :-
Enter a number : 10
The factorial of 10 = 3628800.0
/*
import java.util.*;
class factorialRecursion
{
    public static void main()
    {
        System.out.print("Enter a number : ");
        int n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        double ans=fact(n);
        System.out.println("The factorial of "+n+" = "+ans);
    }
  
    public static double fact(int num)
    {
        if(num==1)
        {
            return(1);
        }
        else
        {
            return(num*(fact(num-1))); //Calling itself again with a different value.
        }
    }
}
//Author : Mayank Rajoria

Sunday, October 23, 2011

Convert a decimal (base 10) number to any base (Java)

//This Java program accepts a decimal number
//i.e. base 10 number from the user and then
//accepts another number from the user and then
//converts the first number to a number whose base
//is the numerical value of the second number.
//NOTE : The second number should be
//between 2 and 10 only.
import java.util.*;
class convertToBase
{
public static void main()
{
System.out.print("Enter the decimal number : ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.print("Enter the base you would like to convert the number to : ");
int b=sc.nextInt();
int q=n,r;
String ans="";
while(q!=0)
{
r=q%b;
ans=r+ans;
q=q/b;
}
System.out.println(ans);
}
}
//Author : Mayank Rajoria

Finding quotient and remainder of two numbers (Java)

//This Java program accepts two numbers
//from the user and then  prints the quotient
//obtained by dividing the first number by the
//second and also prints the remainder.
import java.util.*;
class remainderAndQuotient
{
static void main()
{
int n,q,a,k=1,r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers");
n=sc.nextInt();
a=sc.nextInt();
r=n%a;
q=n/a;
System.out.println("Quotient : "+q);
System.out.println("Remainder : "+r);
}
}
//Author : Mayank Rajoria

Saturday, September 24, 2011

Finding HCF and LCM of two numbers (Java)

//This java program accepts two  from
//the user and prints their HCF and LCM
import java.util.*;
class hcfandlcm
{
    public static void main(String Args[])
    {
        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);
    }
}
  

Finding HCF of two numbers

//This Java program accepts 2 numbers from
//the user and prints their HCF.
import java.util.*;
class hcf
{
    public static void main(String Args[])
    {
        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;
            }
        }
        System.out.println("HCF="+h);
    }
}
//Author : Mayank Rajoria

Friday, September 23, 2011

Check whether a number is Prime or not

//This java program accepts a number from the user
// and checks whether it is a prime number or not.
import java.util.*;
class primeNumber
{
    public static void main(String Args[])
    {
        System.out.println("Enter a number");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ans=check(n);
        if(ans==1)
        {
            System.out.println("The number is a Prime number");
        }
        else if(ans==0)
        {
            System.out.println("The number is not a Prime number");
        }
    }
   
    public static int check(int a)
    {
        int i,k=1;
        for(i=2;i<a;i++)
        {
            if(a%i==0)
            {
                k=0;
                break;
            }
        }
        return(k);
    }
}
//Author : Mayank Rajoria

A menu driven arithmetic calculator

//This program is a menu based arithmetic calculator
//which is capable of accepting input from
//user.
import java.util.*;
class calculator
{
public static void main(String Args[])
{
int a;double n1,n2;
double f;
System.out.println("*********************Calculator*********************");
System.out.println("\n\n\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n5. Exit");
System.out.print("\nEnter the serial no. of the operation you would like to perform : ");
Scanner ob=new Scanner(System.in);
a=ob.nextInt();
switch(a)
{
case 1:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1+n2;
    System.out.println("Final Answer is : "+f);
    break;
case 2:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1-n2;
    System.out.print("Final Answer is : "+f);
    break;
case 4:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1*n2;
    System.out.print("The product is : "+f);
    break;
case 3:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1/n2;
    System.out.print("The Quotient is : "+f);
    break;
case 5:
    break;
default:
    System.out.println("Enter the correct no.");
    break;
}
}
}
//Author : Mayank Rajoria