Wednesday, December 21, 2011

A Java program to print the largest and the smallest number among the 10 numbers entered

/*
 * This Java program accepts 10 integers and stores them in an
 * array. Then it prints the biggest and the smallest number
 * entered.
 */

import java.util.*;
class largest_smallest
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 10 numbers");
        int a[]=new int[10];
        int i;
        int small=0,larg=0;
        for(i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
            if(i==0)
            {
                larg=a[0];
                small=a[0];
            }
           
            if(a[i]<small)
            {
                small=a[i];
            }
            if(a[i]>larg)
            {
                larg=a[i];
            }
        }
        System.out.println("Largest term = "+larg);
        System.out.println("Smallest term = "+small);
    }
}

//Author : Mayank Rajoria

Java program to print the initial letters of the words present in the string entered

/* This Java program accepts a String from the
 * user containing multiple words and then prints
 * a line containing all the initial letters of the
 * words present in the string separated by a dot.
 *
 * Sample Input: My name is Mayank
 * Sample Output: M.n.i.M.
 */

import java.util.*;
import java.lang.Object;
class initialLetter
{
    public static void main()
    {
        String s;
        System.out.println("Enter a String");
        Scanner sc=new Scanner(System.in);
        s=sc.nextLine();
        s=" "+s;
        int l=s.length();
        String ans="";
        char c='n';
        int i;
        for(i=0;i<l;i++)
        {
            c=s.charAt(i);
            if(c==' ')
            {
                ans=ans+s.charAt(i+1)+".";
            }
        }
        System.out.println("The final String: "+ans);
    }
}

//Author : Mayank Rajoria

Thursday, December 15, 2011

A Java program to print the biggest and the smallest integer from the 10 integers entered. (Java)

/**
 * This Java program accepts 10 integers and stores them in an
 * array. Then it prints the biggest and the smallest number
 * entered.
 */

import java.util.*;
class lergestSmallest
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 10 numbers");
        int a[]=new int[10];
        int i;
        int small=0,larg=0;
        for(i=0;i<10;i++)
        {
            a[i]=sc.nextInt();
            if(i==0)
            {
                larg=a[0];
                small=a[0];
            }
           
            if(a[i]<small)
            {
                small=a[i];
            }
            if(a[i]>larg)
            {
                larg=a[i];
            }
        }
        System.out.println("Largest term = "+larg);
        System.out.println("Smallest term = "+small);
    }
}

//Author : Mayank Rajoria

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