Showing posts with label ICSE Programs. Show all posts
Showing posts with label ICSE Programs. 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

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

Wednesday, November 30, 2011

A Java program to find the length of the longest non decreasing subsequence from a group of integers (Java)

//This Java program first accepts a number from the user
//and then accepts that many integers. The program then, using
//dynamic programming, finds the length of the longest
//subsequence of integers such that they are not in
//decreasing order.
//SEQUENCE : A subsequence is a sequence that can
//be derived from another sequence by deleting some
//elements without changing the order of the remaining
//elements. For example, the sequence <1,4,6> is a
//subsequence of <2,1,5,6,4,7,6>.

//Sample Output :-
/*Enter number of integers : 8
Enter the integers :-

-2
5
3
4
10
7
9
12

Longest non-decreasing sequence is of length : 6
*/

import java.util.*;
class longestNonDecreasingSequence
{
public static void main()
{
    int n;
    System.out.print("Enter number of integers : ");
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    int a[]=new int[n];
    int i;
    System.out.println("Enter the integers :-\n");
    for(i=0;i<n;i++)a[i]=sc.nextInt();
    int s[]=new int[n];
    for(i=0;i<n;i++)s[i]=1;
   
    int j,k=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(a[i]>=a[j])
            {
                k=s[j]+1;
                if(s[i]<=k)
                {
                    s[i]=k;
                }
            }
        }
    }
    int ans=a[0];;
    for(i=0;i<n;i++)
    {
        if(s[i]>=ans)
        {
            ans=s[i];
        }
    }
    System.out.print("\nLongest non-decreasing sequence is of length : "+ans);
}
}

//Author : Mayank Rajoria

Saturday, November 12, 2011

Using Selection Sort to sort an array (Java)

//This Java program depicts the use of Selection
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class selectionSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=i;j<10;j++)
        {
            if(a[i]>a[j])
            {
                k=a[i];
                a[i]=a[j];
                a[j]=k;
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]);
    }
}
}

//Author : Mayank Rajoria

Using Bubble Sort for an array (Java)

//This Java program depicts the use of Bubble
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class bubbleSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10 numbers :-");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();   //Accepting numbers
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=0;j<9-i;j++)
        {
            if(a[j]>a[j+1])
            {
                k=a[j];           
                a[j]=a[j+1];   
                a[j+1]=k;      
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]); //Printing the numbers
    }
}
}

//Author : Mayank Rajoria

Tuesday, October 25, 2011

Printing a pyramid pattern of digits #9

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 7

Sample Output :
            7
          767
        76567
      7654567
    765434567
  76543234567
7654321234567 */
//For a left aligned version of this pattern
//visit: Java Programs: Printing a pyramid pattern of digits 8 (Java)

import java.util.*;
class pattern
{
public static void main()
{
    System.out.print("Enter the number of lines you want : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j,k=n,l=1;
    for(i=1;i<=n;i++)
    {
        for(j=n;j>=i+1;j--)
        {
            System.out.print(" ");
        }
        k=n;
        for(j=1;j<=i;j++,k--)
        {
            System.out.print(k);
        }
        k=k+2;
        for(j=1;j<=i-1;j++,k++)
        {
            System.out.print(k);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Monday, October 24, 2011

Printing a pyramid pattern of digits #7

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6

Sample Output :
          1
        121
      12321
    1234321
  123454321
12345654321 */

import java.util.*;
class pattern10

{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        for(k=i;k<n;k++)
        {
            System.out.print(" ");
        }
        for(j=1;j<=i;j++)
        {
            System.out.print(j);
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria

Printing a pyramid pattern of digits #5

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed and then prints
//the pattern of the given number of lines.
/*Sample Input : 6
Sample Output :
1
121
12321
1234321
123454321
12345654321 */

import java.util.*;
class pattern
{
public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number of lines you want : ");
    int n=sc.nextInt();
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");  //If you want you may remove the '+" "' part.
        }
        for(j=i-1;j>=1;j--)
        {
            System.out.print(j+" "); //If you want you may remove the '+" "' part.
        }
        System.out.println();
    }
}
}
//Author : Mayank Rajoria