Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

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

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

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