Loading
View RSS Feed

Angad Kumar Shukla's blog

Advanced Iteration Concept Programs in Java

Rating: 2 votes, 3.00 average.
1.Write a program to split a list of numbers based on condition; say even list and odd list.
Code:
import java.io.*;
class EvenOdd
{
  public static void main(String args[]) throws IOException{
	  boolean status=true;
  BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
  int [] arr1=new int[20];
  System.out.println("Enter the limit of mixed odd and even array");
  int size=Integer.parseInt(cr.readLine());
  System.out.println("Enter the values");
  for(int i=0;i<size;i++){
        arr1[i]=Integer.parseInt(cr.readLine());
    }

    do{
        System.out.println();
    System.out.println("1 Display Even");
    System.out.println("2 Display Odd");
    System.out.println("3 Exit");
    System.out.print("Choose Option: ");
    int choice=Integer.parseInt(cr.readLine());
    switch(choice){
    case 1: 
        for(int j=0;j<arr1.length;j++){
        if(arr1[j]%2==0){
            System.out.println(arr1[j]);
            }
        }
        break;
        case 2:
            for(int k=0;k<arr1.length;k++){
            if(arr1[k]%2!=0){
         System.out.println(arr1[k]);
            }
        }
        break;
        case 3:
            status=true;
            System.exit(0);
        break;
        }
    }
    while(!status);
    }
}
2.Write a program to merge two given list into one list.
Code:
import java.io.*;
import java.io.InputStreamReader;


public class MergeList {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		int i=0,j=0;
		int k=0,p=0,n=0;
		int arr1[]=new int[20];
		int arr2[]=new int[20];
		int arr3[]=new int[50];
		
		 BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
		 System.out.println("Enter the number of elements:for 1st List");
		int number_input1=Integer.parseInt(cr.readLine());
		 System.out.println("Enter the number of elements:for 2nd List");
		int number_input2=Integer.parseInt(cr.readLine());
		System.out.println("Enter the value for 1st List");
		for(i=0;i<number_input1;i++)
		{
		
			arr1[i]=Integer.parseInt(cr.readLine());
			
		}
		System.out.println("Enter the value for 2nd List");
		for(j=0;j<number_input2;j++)
		{
		
			arr2[j]=Integer.parseInt(cr.readLine());
			
	 	}
	  System.out.println("Elements after combining");
	  for(p=0,k=0;(p<(number_input1)||k<=(number_input2));p++,k++){
		 if(p<number_input1)
			  arr3[p]=arr1[p];
		
		  	 
		 arr3[p+number_input1]=arr2[k];
		 
		  
	  }
	  for(n=0;n<(number_input1+number_input2);n++)
	  System.out.println(arr3[n]);
		  
	}

}
3.Write a program to find the availability of given number in the list.
Code:
import java.io.*;
public class SearchingElement {


	public static void main(String[] args) throws IOException{

		
			int a[]=new int[20];
			int i,temp=0,pos=0,element;
		    BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Search an Element in Array");
			System.out.println("Enter the number of elements: ");
			int number_input=Integer.parseInt(cr.readLine());
			System.out.println("Enter the value for elements: ");
			for(i=0;i<number_input;i++)
			{
			
				a[i]=Integer.parseInt(cr.readLine());
				
			}
			System.out.println("Enter the searching element: ");
			element=Integer.parseInt(cr.readLine());
			for(i=0;i<number_input;i++)
			{
				if(a[i]==element)
				{
					temp=1;
		                        pos=i;
				}
		   	}
		        if (temp==1)
		        	System.out.println("Element found"+element+"position="+pos);
		        else
			       System.out.println("Element not found");
		}
		
		
	}
4.Make list of Students having name, roll no., age, score. Write a program to order the student in the class based on score.
Code:
import java.io.*;
public class OrderScore {
	public static void bubblesort( int score[], int n ,String name[] ){
		  int i, j,t=0;
		  String name1="";
		  for(i = 0; i < n; i++){
		  for(j = 1; j < (n-i); j++){
		  if(score[j-1] > score[j]){
		  t = score[j-1];
		  name1=name[j-1];
		  score[j-1]=score[j];
		  name[j-1]=name[j];
		  score[j]=t;
		  name[j]=name1;
		  }
		  }
		  }
		  }
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		String name[]=new String[10];
		int age[]=new int[10];
		int roll[]=new int[10];
		int score[]=new int[10];
	
	
		BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the limit of students");
		int len=Integer.parseInt(cr.readLine());
		System.out.println("Enter The records ");
		for(int k=0;k<len;k++)
		{
			System.out.println("Enter the name");
			name[k]=cr.readLine();
			System.out.println("Enter the age");
			age[k]=Integer.parseInt(cr.readLine());
			System.out.println("Enter roll");
			roll[k]=Integer.parseInt(cr.readLine());
			System.out.println("Enter Score");
			score[k]=Integer.parseInt(cr.readLine());
		}
		
		bubblesort(score,len,name);
		for(int p=0;p<len;p++)
			System.out.println(score[p]+" " + "->"+name[p]);
		

	}

}
6.Make list of Students having name, roll no., age, score. Write a program to group the Students based on the average score [0-50],[50-65],[65-80],[80-100].
Code:
import java.io.*;
public class AverageScore {
	
		public static void main(String[] args) throws IOException{
			String name[]=new String[5];
			int roll[]=new int[5];
			int age[]=new int[5];
			int score[]=new int[5];
		    BufferedReader cr=new BufferedReader(new InputStreamReader(System.in));
		    System.out.println(" Enterh the number of elemets to be entered");
		    int num=Integer.parseInt(cr.readLine());
		    for(int k=0;k<num;k++)
		     {
		    	System.out.println("Enter the name");
				name[k]=cr.readLine();
				System.out.println("Enter the age");
				age[k]=Integer.parseInt(cr.readLine());
				System.out.println("Enter roll");
				roll[k]=Integer.parseInt(cr.readLine());
				System.out.println("Enter Score");
				score[k]=Integer.parseInt(cr.readLine());
		     }
	         for(int l=0;l<num;l++)
	         {
	        	  if(score[l]>0&&score[l]<=50)
	        		  System.out.println(" scorer in 0-50 range "+name[l]);
	        	  else if(score[l]>50&&score[l]<=65)
	        		  System.out.println(" scorer in 50-65 range "+name[l]);
	        	  else if(score[l]>65&&score[l]<=80)
	        		  System.out.println(" scorer in 65-80 range "+name[l]);
	        	  else 
	        		  System.out.println(" scorer in 80-100 range "+name[l]);
	         }
		        
		
		
		}   
	}

Submit "Advanced Iteration Concept Programs in Java" to Digg Submit "Advanced Iteration Concept Programs in Java" to del.icio.us Submit "Advanced Iteration Concept Programs in Java" to StumbleUpon Submit "Advanced Iteration Concept Programs in Java" to Google

Tags: class, iteration, java
Categories
Java

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.